Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lint): add JavaScript plugin support #27203

Merged
merged 188 commits into from
Feb 5, 2025

Conversation

bartlomieju
Copy link
Member

@bartlomieju bartlomieju commented Dec 3, 2024

This commit adds an unstable lint plugin API.

Plugins are specified in the deno.json file under
lint.plugins option like so:

{
  "lint": {
    "plugins": [
      "./plugins/my-plugin.ts",
      "jsr:@deno/lint-plugin1",
      "npm:@deno/lint-plugin2"
    ]
  }
}

The API is considered unstable and might be subject
to changes in the future.

Plugin API was modelled after ESLint API for the
most part, but there are no guarantees for compatibility.
The AST format exposed to plugins is closely modelled
after the AST that typescript-eslint uses.

Lint plugins use the visitor pattern and can add
diagnostics like so:

export default {
  name: "lint-plugin",
  rules: {
    "plugin-rule": {
      create(context) {
        return {
          Identifier(node) {
            if (node.name === "a") {
              context.report({
                node,
                message: "should be b",
                fix(fixer) {
                  return fixer.replaceText(node, "_b");
                },
              });
            }
          },
        };
      },
    },
  },
} satisfies Deno.lint.Plugin;

Besides reporting errors (diagnostics) plugins can provide
automatic fixes that use text replacement to apply changes.

cli/args/mod.rs Outdated Show resolved Hide resolved
cli/ops/lint.rs Outdated Show resolved Hide resolved
cli/ops/lint.rs Outdated Show resolved Hide resolved
cli/tools/lint/linter.rs Outdated Show resolved Hide resolved
cli/tools/lint/linter.rs Outdated Show resolved Hide resolved
cli/tools/lint/linter.rs Outdated Show resolved Hide resolved
cli/tools/lint/linter.rs Outdated Show resolved Hide resolved
runtime/js/99_main.js Outdated Show resolved Hide resolved
cli/ops/lint.rs Outdated Show resolved Hide resolved
cli/tools/lint/plugins.rs Outdated Show resolved Hide resolved
@bartlomieju bartlomieju added this to the 2.2.0 milestone Feb 3, 2025
cli/js/40_lint.js Outdated Show resolved Hide resolved
Copy link
Member

@dsherret dsherret left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@bartlomieju bartlomieju merged commit f08ca64 into denoland:main Feb 5, 2025
18 checks passed
@bartlomieju bartlomieju deleted the lint_plugins branch February 5, 2025 15:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants