Skip to content

Linter system implementation  #1996

Description

From design #1785 and proposal https://gist.github.com/timotheeguerin/95d4eea064ccfa0db64d9bf90e1597a6

  1. Add config for enable/disable + update engine to take this into account
  2. Move linter code in compiler
  3. Figure out if a type is in library
  4. Update all the rules to have the diagnostic message inline instead of in $lib
  5. Rulesets

1. Update the rule declaration to take

export const noNullableRule = createRule({
  name: "no-nullable",
  // (+) same value the diagnostic entry can take(warning or error)
  severity: "warning",
  // (+) this is the same structure as the diagnostic declaration.
  messages: {
    default: "Can be a fixed string",
    model: paramMessage`Can be parameterized ${"foo"}`,
  },
  create(context: LintContext) {
    return {
      modelProperty: (property: ModelProperty) => {
        // Report error
        context.reportDiagnostic(program, {
          // (-) no code, use the linting rule name
          // code: "no-nullable",
          target: property,
        });
      },
    };
  },
});
interface RuleDefinition {
  name: string;
  severity: "warning"; // Cannot be a anything but a warning for now. Error should be left to onValidate or onEmit
  messages: DiagnosticMessages;
  navigationOptions: {
    includeTemplateDeclaration?: boolean; // @default false
    includeTemplateInstance?: boolean; // @default true
  };
  create: (context: LintContext): Listener;
}

2. Register the rules and rulesets

import { noInlineModelsRule } from "./rules/no-inline-models.rule.js";
import { reservedNamesRule } from "./rules/no-inline-models.rule.js";
import { casingRule } from "./rules/casing.rule.js";
import { noUnknownRule } from "./rules/no-unknown.rule.js";
import { noFooRule } from "./rules/no-foo.rule.js";

export const $lib = createTypeSpecLibrary({
  // name of the library
  name: "my-library-with-linter",
  diagnostics: {
    // Example diagnostic. Those stay as they are
    "invalid-extension-key": {
      severity: "error",
      messages: {
        default: paramMessage`OpenAPI extension must start with 'x-' but was '${"value"}'`,
      },
    },
  },
  // Add a new optional entry which define the property of the linter
  linter: {
    // List of the rules provided
    rules: [
      noInlineModelsRule,
      reservedNamesRule,
      casingRule,
      noUnknownRule,
      noNullableRule,
    ],
  },
  ruleSets: {
    recommended: [
      "my-library-with-linter:no-nullabe",
      "some-other-lib:no-foo",
      ["some-other-lib:no-bar", "error"], // Set severity
    ],
    // Some more
    all: [""],
  },
});

3. Add enable/disable to tspconfig.yaml

4. Include a way in the compiler to resolve where type is defined(user code vs library)

This probably needs to figure if a sourcefile is in a user spec or library and we can trace back from there

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions