Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions napi/oxlint2/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,9 @@ class Linter {
visitors.push(rule.create(createContext(ruleId)));
}

// TODO: Combine visitors for multiple rules
const visitor = new Visitor(visitors[0]);
const visitor = new Visitor(
visitors.length === 1 ? visitors[0] : combineVisitors(visitors),
);

// Visit AST
const programPos = buffer.uint32[DATA_POINTER_POS_32],
Expand All @@ -143,6 +144,25 @@ class Linter {
}
}

function combineVisitors(visitors) {
const combinedVisitor = {};
for (const visitor of visitors) {
for (const nodeType of Object.keys(visitor)) {
if (!(nodeType in combinedVisitor)) {
combinedVisitor[nodeType] = function(node) {
for (const v of visitors) {
if (typeof v[nodeType] === 'function') {
v[nodeType](node);
}
}
};
}
}
}

return combinedVisitor;
}

async function main() {
const linter = new Linter();

Expand Down
35 changes: 35 additions & 0 deletions napi/oxlint2/test/__snapshots__/e2e.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,38 @@ exports[`oxlint2 CLI > should report the correct severity when using a custom pl
Found 2 warnings and 0 errors.
Finished in Xms on 1 file using X threads."
`;

exports[`oxlint2 CLI > should work with multiple rules 1`] = `
"
! ]8;;https://oxc.rs/docs/guide/usage/linter/rules/eslint/no-debugger.html\\eslint(no-debugger)]8;;\\: \`debugger\` statement is not allowed
,-[index.js:1:1]
1 | debugger;
: ^^^^^^^^^
2 |
\`----
help: Remove the debugger statement

x basic-custom-plugin(no-debugger): Unexpected Debugger Statement
,-[index.js:1:1]
1 | debugger;
: ^^^^^^^^^
2 |
\`----

x basic-custom-plugin(no-debugger-2): Unexpected Debugger Statement
,-[index.js:1:1]
1 | debugger;
: ^^^^^^^^^
2 |
\`----

x basic-custom-plugin(no-ident-references-named-foo): Unexpected Identifier Reference named foo
,-[index.js:3:1]
2 |
3 | foo;
: ^^^
\`----

Found 1 warning and 3 errors.
Finished in Xms on 1 file using X threads."
`;
9 changes: 9 additions & 0 deletions napi/oxlint2/test/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,13 @@ describe('oxlint2 CLI', () => {
expect(exitCode).toBe(0);
expect(normalizeOutput(stdout)).toMatchSnapshot();
});

it('should work with multiple rules', async () => {
const { stdout, exitCode } = await runOxlint(
'test/fixtures/basic_custom_plugin_multiple_rules',
);

expect(exitCode).toBe(1);
expect(normalizeOutput(stdout)).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"plugins": ["./test_plugin"],
"rules": {
"basic-custom-plugin/no-debugger": "error",
"basic-custom-plugin/no-debugger-2": "error",
"basic-custom-plugin/no-ident-references-named-foo": "error"
},
"ignorePatterns": ["test_plugin"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
debugger;

foo;
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
export default {
meta: {
name: "basic-custom-plugin",
},
rules: {
"no-debugger": {
create(context) {
return {
DebuggerStatement(debuggerStatement) {
context.report({
message: "Unexpected Debugger Statement",
node: debuggerStatement,
});
},
};
},
},
"no-debugger-2": {
create(context) {
return {
DebuggerStatement(debuggerStatement) {
context.report({
message: "Unexpected Debugger Statement",
node: debuggerStatement,
});
},
};
},
},
"no-ident-references-named-foo": {
create(context) {
return {
IdentifierReference(identifierReference) {
if (identifierReference.name == "foo") {
context.report({
message: "Unexpected Identifier Reference named foo",
node: identifierReference,
});
}
},
};
},
},
},
};
Loading