mirror of
https://github.com/oxc-project/oxc.git
synced 2026-09-14 19:36:11 +08:00
9de9385fd1
Part of #18610. Our intent is to move `definePlugin`, `defineRule`, and `eslintCompatPlugin` to a separate package `@oxlint/plugins` (plural to avoid confusion with naming convention for plugins `oxlint-plugin-xxx`). So rename the current entry point which exports these functions from `oxlint/plugin` to `oxlint/plugins` (plural).
56 lines
1.4 KiB
TypeScript
56 lines
1.4 KiB
TypeScript
import type { Plugin, Rule } from "#oxlint/plugins";
|
|
|
|
const rule: Rule = {
|
|
create(context) {
|
|
const { sourceCode } = context;
|
|
|
|
// Check no error calling before AST or source code is accessed
|
|
sourceCode.getNodeByRangeIndex(0);
|
|
|
|
// Get nodes:
|
|
// * At start of file
|
|
// * Before, at start of, inside, and at end of every token
|
|
const { tokens } = sourceCode.ast;
|
|
|
|
const indexes = new Set([0]);
|
|
for (const token of tokens) {
|
|
const [start, end] = token.range;
|
|
indexes.add(start - 1);
|
|
indexes.add(start);
|
|
if (end - start > 1) indexes.add(end - 1);
|
|
indexes.add(end);
|
|
}
|
|
const sourceTextLen = sourceCode.text.length;
|
|
indexes.add(sourceTextLen - 1);
|
|
indexes.add(sourceTextLen);
|
|
|
|
for (const index of indexes) {
|
|
const node = sourceCode.getNodeByRangeIndex(index);
|
|
context.report({
|
|
message: `type: ${node === null ? null : node.type}`,
|
|
node: { range: [index, index] },
|
|
});
|
|
}
|
|
|
|
// Get node after end of file
|
|
const node = sourceCode.getNodeByRangeIndex(sourceTextLen + 1);
|
|
context.report({
|
|
message: `type: ${node === null ? null : node.type}`,
|
|
node: { range: [sourceTextLen, sourceTextLen] },
|
|
});
|
|
|
|
return {};
|
|
},
|
|
};
|
|
|
|
const plugin: Plugin = {
|
|
meta: {
|
|
name: "getNode-plugin",
|
|
},
|
|
rules: {
|
|
getNode: rule,
|
|
},
|
|
};
|
|
|
|
export default plugin;
|