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

Fix a problem that 3rd-party plugins could not be loaded with legacy config #366

Merged
merged 8 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
61 changes: 44 additions & 17 deletions src/__snapshots__/core.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ exports[`Core > disablePerLine 1`] = `
let a = 1;"
`;

exports[`Core > fixes problems with legacy config and 3rd-party plugins 1`] = `
"// eslint-disable-next-line test/ban-nullish-coalescing-operator
// eslint-disable-next-line test/ban-nullish-coalescing-operator
// eslint-disable-next-line test/ban-nullish-coalescing-operator
// eslint-disable-next-line test/ban-nullish-coalescing-operator
// eslint-disable-next-line test/ban-nullish-coalescing-operator
// eslint-disable-next-line test/ban-nullish-coalescing-operator
// eslint-disable-next-line test/ban-nullish-coalescing-operator
// eslint-disable-next-line test/ban-nullish-coalescing-operator
// eslint-disable-next-line test/ban-nullish-coalescing-operator
// eslint-disable-next-line test/ban-nullish-coalescing-operator
Copy link
Owner Author

Choose a reason for hiding this comment

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

I was expecting only one disable comment to be added, but multiple ones were added.

To investigate this problem, I added the following code for the debug log.

$ pnpm run test -t "fixes problems" src/core.test.ts
...
unfilteredMessages [
  {
    ruleId: 'test/ban-nullish-coalescing-operator',
    message: "Definition for rule 'test/ban-nullish-coalescing-operator' was not found.",
    line: 1,
    column: 1,
    endLine: 1,
    endColumn: 2,
    severity: 2,
    nodeType: null
  }
]

It seems that the plugin is failing to load.

Copy link
Owner Author

Choose a reason for hiding this comment

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

core.lint() only uses the ESLint class. On the other hand, core.disablePerLine() uses both the ESLint class and the Linter class.

The ESLint class seems to be able to load 3rd-party plugins, but the Linter class cannot.

As a result, it seems that although the lint results can be output, the problems cannot be fixed.

2 ?? 2;"
`;

exports[`Core > flat config 1`] = `
[
{
Expand Down Expand Up @@ -194,6 +208,17 @@ exports[`Core > lint > returns lint results 1`] = `
],
"warningCount": 0,
},
{
"errorCount": 1,
"filePath": "<fixture>/src/ban-nullish-coalescing-operator.js",
"messages": [
{
"ruleId": "test/ban-nullish-coalescing-operator",
"severity": 2,
},
],
"warningCount": 0,
},
{
"errorCount": 1,
"filePath": "<fixture>/src/import-order.js",
Expand Down Expand Up @@ -270,22 +295,24 @@ exports[`Core > printDetailsOfResults 1`] = `
`;

exports[`Core > printSummaryOfResults 1`] = `
"- 7 files (0 file passed, 7 files failed) checked.
- 18 problems (17 errors, 1 warning) found.
╔═════════════════════════════╤═══════╤═════════╤════════════╤═════════════════╗
║ Rule │ Error │ Warning │ is fixable │ has suggestions ║
╟─────────────────────────────┼───────┼─────────┼────────────┼─────────────────╢
║ arrow-body-style │ 12 │ 0 │ 12 │ 0 ║
╟─────────────────────────────┼───────┼─────────┼────────────┼─────────────────╢
║ ban-exponentiation-operator │ 1 │ 0 │ 0 │ 0 ║
╟─────────────────────────────┼───────┼─────────┼────────────┼─────────────────╢
║ import/order │ 1 │ 0 │ 0 │ 0 ║
╟─────────────────────────────┼───────┼─────────┼────────────┼─────────────────╢
║ no-unsafe-negation │ 1 │ 0 │ 0 │ 1 ║
╟─────────────────────────────┼───────┼─────────┼────────────┼─────────────────╢
║ no-unused-vars │ 1 │ 0 │ 0 │ 0 ║
╟─────────────────────────────┼───────┼─────────┼────────────┼─────────────────╢
║ ]8;;https://example.comprefer-const]8;; │ 1 │ 1 │ 2 │ 0 ║
╚═════════════════════════════╧═══════╧═════════╧════════════╧═════════════════╝
"- 8 files (0 file passed, 8 files failed) checked.
- 19 problems (18 errors, 1 warning) found.
╔══════════════════════════════════════╤═══════╤═════════╤════════════╤═════════════════╗
║ Rule │ Error │ Warning │ is fixable │ has suggestions ║
╟──────────────────────────────────────┼───────┼─────────┼────────────┼─────────────────╢
║ arrow-body-style │ 12 │ 0 │ 12 │ 0 ║
╟──────────────────────────────────────┼───────┼─────────┼────────────┼─────────────────╢
║ ban-exponentiation-operator │ 1 │ 0 │ 0 │ 0 ║
╟──────────────────────────────────────┼───────┼─────────┼────────────┼─────────────────╢
║ test/ban-nullish-coalescing-operator │ 1 │ 0 │ 0 │ 0 ║
╟──────────────────────────────────────┼───────┼─────────┼────────────┼─────────────────╢
║ import/order │ 1 │ 0 │ 0 │ 0 ║
╟──────────────────────────────────────┼───────┼─────────┼────────────┼─────────────────╢
║ no-unsafe-negation │ 1 │ 0 │ 0 │ 1 ║
╟──────────────────────────────────────┼───────┼─────────┼────────────┼─────────────────╢
║ no-unused-vars │ 1 │ 0 │ 0 │ 0 ║
╟──────────────────────────────────────┼───────┼─────────┼────────────┼─────────────────╢
║ ]8;;https://example.comprefer-const]8;; │ 1 │ 1 │ 2 │ 0 ║
╚══════════════════════════════════════╧═══════╧═════════╧════════════╧═════════════════╝
"
`;
37 changes: 37 additions & 0 deletions src/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ const iff = await createIFF({
'src/ban-exponentiation-operator.js': dedent`
2 ** 2;
`,
'src/ban-nullish-coalescing-operator.js': dedent`
2 ?? 2;
`,
'src/no-unused-vars.js': dedent`
const a = 1;
`,
Expand All @@ -91,18 +94,47 @@ const iff = await createIFF({
'src/no-unsafe-negation.js': dedent`
if (!key in object) {}
`,
'node_modules/eslint-plugin-test/package.json': dedent`
{
"name": "eslint-plugin-test",
"version": "1.0.0",
"main": "index.js"
}
`,
'node_modules/eslint-plugin-test/index.js': dedent`
module.exports = {
rules: {
'ban-nullish-coalescing-operator': {
create(context) {
return {
LogicalExpression: (node) => {
if (node.operator === '??') {
context.report({
node,
message: 'Ban nullish coalescing operator',
});
}
},
};
},
},
},
};
`,
'.eslintrc.js': dedent`
module.exports = {
root: true,
parserOptions: {
ecmaVersion: 2021,
sourceType: 'module',
},
plugins: ['test'],
overrides: [
{ files: ['prefer-const.js'], rules: { 'prefer-const': 'error' } },
{ files: ['arrow-body-style.js'], rules: { 'arrow-body-style': ['error', 'always'] } },
{ files: ['import-order.js'], rules: { 'import/order': 'error' } },
{ files: ['ban-exponentiation-operator.js'], rules: { 'ban-exponentiation-operator': 'error' } },
{ files: ['ban-nullish-coalescing-operator.js'], rules: { 'test/ban-nullish-coalescing-operator': 'error' } },
{
files: ['no-unused-vars.js'],
rules: { 'no-unused-vars': ['error', { varsIgnorePattern: '^_' }] },
Expand Down Expand Up @@ -312,4 +344,9 @@ describe('Core', () => {
expect(await readFile(iff.paths['src/index.jsx'], 'utf-8')).toMatchSnapshot();
expect(await readFile(iff.paths['src/.index.js'], 'utf-8')).toMatchSnapshot();
});
test('fixes problems with legacy config and 3rd-party plugins', async () => {
const results = await core.lint();
await core.disablePerLine(results, ['test/ban-nullish-coalescing-operator']);
expect(await readFile(iff.paths['src/ban-nullish-coalescing-operator.js'], 'utf-8')).toMatchSnapshot();
});
});
6 changes: 3 additions & 3 deletions src/eslint/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ export function verifyAndFix(
do {
passNumber++;

const messages = linter
.verify(currentText, config, filePath)
.filter((message) => message.ruleId && ruleIds.includes(message.ruleId));
const unfilteredMessages = linter.verify(currentText, config, filePath);
if (passNumber === 1) console.log('unfilteredMessages', unfilteredMessages);
const messages = unfilteredMessages.filter((message) => message.ruleId && ruleIds.includes(message.ruleId));
const sourceCode = linter.getSourceCode();

// Create `Rule.Fix[]`
Expand Down
Loading