Lint Plugins
Note
The lint plugin API is available in Ende 2.2.0 and later, with no unstable
flag required. The API is still evolving and may change in a future release.
The built-in linter can be extended with plugins to add custom lint rules.
Whilst Ende ships with many lint rules out of the box, there are cases where you need a custom rule tailored particularly to your project - whether to catch a context-specific problem or enforce company-wide conventions.
This is where the lint plugin API comes into play.
The lint plugin API is intentionally modeled after the ESLint API. While this API doesn't provide 100% compatibility, the existing knowledge of authoring ESLint plugins can be mostly reused if you happen to have written custom ESLint rules in the past.
Plugins are loaded via the lint.plugins setting in ende.json.
The value is an array of plugin specifiers. These can be paths or jsr:
specifiers.
{
"lint": {
"plugins": ["./my-plugin.ts"]
}
}Example plugin
A plugin always has the same shape. It has a default export which is your plugin object.
Note
Ende provides type declarations for the lint plugins API.
All the typings are available under the Ende.lint
namespace.
const plugin: Ende.lint.Plugin = {
// The name of your plugin. Will be shown in error output
name: "my-plugin",
// Object with rules. The property name is the rule name and
// will be shown in the error output as well.
rules: {
"my-rule": {
// Inside the `create(context)` method is where you'll put your logic.
// It's called when a file is being linted.
create(context) {
// Return an AST visitor object
return {
// Here in this example we forbid any identifiers being named `_a`
Identifier(node) {
if (node.name === "_a") {
// Report a lint error with a custom message
context.report({
node,
message: "should be _b",
// Optional: Provide a fix, which can be applied when
// the user runs `ende lint --fix`
fix(fixer) {
return fixer.replaceText(node, "_b");
}
});
}
}
};
}
}
}
};
export default plugin;Permissions
Ende NEXT_RELEASE
Earlier releases ran lint plugins with read and environment access, and loaded local plugins and their imports from anywhere. Upstream Deno still does.
Lint plugins run with no permissions, in ende lint and in the language server:
no read, write, env, net, run, sys or ffi access, and they can't create workers.
A plugin gets the name and source text of the file being linted from the linter
(context.filename and context.sourceCode), and nothing else. A call such as
Ende.readTextFile() or Ende.env.get() in a plugin fails with NotCapable.
ende lint runs the plugins that a project's ende.json names, so linting a
checkout you don't trust, such as a pull request in CI, runs its code. Without
permissions, a plugin can't read your files or environment variables and print
them in its diagnostics.
A local plugin must be inside the project folder: the folder of the root
ende.json when it holds the folder you lint from, otherwise that folder.
ende lint refuses a plugin outside it before loading it:
$ ende lint
error: Lint plugin "/work/shared/plugin.ts" is outside the project folder "/work/repo". Lint plugins have no read access, so they load only from the project folder: move the plugin into it, or use a jsr: or https: plugin.
jsr: and https: plugins still work. The CLI downloads them, but the plugin
itself has no network access.
A plugin can import local files only as code, by extension: .ts, .tsx,
.mts, .js, .jsx or .mjs, and inside the project folder. Ende refuses
other local imports before reading the file: text, bytes, JSON and CSS imports,
and files with no extension or another one, such as .git/config or .env:
error: Lint plugins can't import local files as data, nor as code unless the extension says so: "/work/repo/credentials" isn't a .ts, .tsx, .mts, .js, .jsx or .mjs file. Lint plugins have no read access; they get the file's text and AST from the linter.
at file:///work/repo/my-plugin.ts:1:8
Warning
A plugin can still import the project's own code files, and it sees the text
of every file it lints. Don't keep secrets in code files inside the project,
such as a gitignored config.local.js: a plugin could print them in its
diagnostics, which end up in CI logs.
Using selectors to match nodes
Writing code to match a specific node can sometimes become a bit tedious if you write it in plain JavaScript. Sometimes this matching logic would be easier to express via a selector, similar to CSS selectors. By using a string as the property name in the returned visitor object, we can specify a custom selector.
const plugin: Ende.lint.Plugin = {
name: "my-plugin",
rules: {
"my-rule": {
create(context) {
return {
// Selectors can be used too. Here we check for
// `require("...") calls.
'CallExpression[callee.name="require"]'(node) {
context.report({
node,
message: "Don't use require() calls to load modules"
});
}
};
}
}
}
};
export default plugin;Note, that we can always refine our match further in JavaScript if the matching logic is too complex to be expressed as a selector alone. The full list of the supported syntax for selectors is:
| Syntax | Description |
|---|---|
Foo + Foo |
Next sibling selector |
Foo > Bar |
Child combinator |
Foo ~ Bar |
Subsequent sibling combinator |
Foo Bar |
Descendant combinator |
Foo[attr] |
Attribute existence |
Foo[attr.length < 2] |
Attribute value comparison |
Foo[attr=/(foo|bar)*/] |
Attribute value regex check |
:first-child |
First child pseudo-class |
:last-child |
Last child pseudo-class |
:nth-child(2n + 1) |
Nth-child pseudo-class |
:not(> Bar) |
Not pseudo-class |
:is(> Bar) |
Is pseudo-class |
:where(> Bar) |
Where pseudo-class (same as :is()) |
:matches(> Bar) |
Matches pseudo-class (same as :is()) |
:has(> Bar) |
Has pseudo-class |
IfStatement.test |
Field selector .<field> |
There is also the :exit pseudo that is only valid at the end of the whole
selector. When it's present, Ende will call the function while going up the
tree instead of when going down.
Tip
We highly recommend using the typescript-eslint playground when developing lint rules. It allows you to inspect code and the resulting AST format. This makes it easier to see which selectors match which node.
Applying fixes
A custom lint rule can supply a function to apply a fix when reporting a
problem. The optional fix() method is called when running ende lint --fix or
applying a fix from inside your editor through the Ende LSP.
The fix() method receives a fixer instance which contains helper methods to
make creating a fix easier. A fix consists of a start position, an end position
and the new text that should be put in this range.
context.report({
node,
message: "should be _b",
fix(fixer) {
return fixer.replaceText(node, "_b");
}
});
The fixer object has the following methods:
insertTextAfter(node, text): Insert text after the given node.insertTextAfterRange(range, text): Insert text after the given range.insertTextBefore(node, text): Insert text before the given node.insertTextBeforeRange(range, text): Insert text before the given range.remove(node): Remove the given node.removeRange(range): Remove text in the given range.replaceText(node, text): Replace the text in the given node.replaceTextRange(range, text): Replace the text in the given range.
The fix() method can also return an array of fixes or yield multiple fixes if
it's a generator function.
Sometimes the original source text of a node is needed to create a fix. To get
the source code of any node use context.sourceCode.getText():
context.report({
node,
message: "should be _b",
fix(fixer) {
const original = context.sourceCode.getText(node);
const newText = `{ ${original} }`;
return fixer.replaceText(node, newText);
}
});
Running cleanup code
If your plugin requires running cleanup code after a file has been linted, you
can hook into the linter via the destroy() hook. It is called after a file has
been linted and just before the plugin context is destroyed.
const plugin: Ende.lint.Plugin = {
name: "my-plugin",
rules: {
"my-rule": {
create(context) {
// ...
},
// Optional: Run code after a linting for a file is completed
// and each rule context is destroyed.
destroy() {
// do some cleanup stuff if you need to
}
}
}
};
export default plugin;Caution
It is not safe to assume that your plugin code will be executed again for each of the files linted.
Prefer not to keep a global state, and do cleanup in the destroy hook, in
case ende lint decides to reuse the existing plugin instance.
Excluding custom rules
Similar to built-in rules, you can disable custom rules provided by a plugin. To
do so, add it to the lint.rules.exclude key in ende.json. The format of a
custom lint rule is always <plugin-name>/<rule-name>.
{
"lint": {
"plugins": ["./my-plugin.ts"],
"rules": {
"exclude": ["my-plugin/my-rule"]
}
}
}Ignoring custom lint reports
Sometimes you want to disable a reported lint error for a particular place in your code. Instead of disabling the custom lint rule entirely, you can disable a reported location by placing a code comment before it.
// ende-lint-ignore my-custom-plugin/no-console
console.log("hey");
This will disable the lint rule from a lint plugin for this particular line.
The syntax for the ignore comment is:
// ende-lint-ignore <my-plugin>/<my-rule>
Testing plugins
The Ende.lint.runPlugin API provides a
convenient way to test your plugins. It allows you to assert that the plugin
produces expected diagnostics given the particular input.
Let's use the example plugin, defined above:
import { assertEquals } from "jsr:@ende/assert";
import myPlugin from "./my-plugin.ts";
Ende.test("my-plugin", () => {
const diagnostics = Ende.lint.runPlugin(
myPlugin,
"main.ts", // Dummy filename, file doesn't need to exist.
"const _a = 'a';"
);
assertEquals(diagnostics.length, 1);
const d = diagnostics[0];
assertEquals(d.id, "my-plugin/my-rule");
assertEquals(d.message, "should be _b");
assertEquals(d.fix, [{ range: [6, 8], text: "_b" }]);
});Note
The Ende.lint.runPlugin API is only
available in the ende test and ende bench subcommands.
Trying to use it with any other subcommand will throw an error.
The plugin runs with the test's permissions there, not with none as in
ende lint, so a plugin that reads files can pass its tests and still fail
when linting.
Consult the API reference for more information on
Ende.lint.runPlugin and
Ende.lint.Diagnostic.