Files
oxc-project__oxc/apps/oxlint/test/fixtures/getNodeByRangeIndex/plugin.ts
overlookmotel 9de9385fd1 refactor(linter/plugins): rename oxlint/plugin to oxlint/plugins (#18792)
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).
2026-02-01 13:23:14 +00:00

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;