Skip to content

Commit 6f978f7

Browse files
fix: eslint 10 support
1 parent 8ba81b7 commit 6f978f7

File tree

4 files changed

+50
-48
lines changed

4 files changed

+50
-48
lines changed

src/index.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,21 @@ function patch(module) {
5757
}
5858
Linter[LINTER_ISPATCHED_PROPERTY_NAME] = true
5959

60-
const verifyMethodName = Linter.prototype._verifyWithoutProcessors
61-
? "_verifyWithoutProcessors" // ESLint 6+
62-
: "verify" // ESLint 5-
63-
const verify = Linter.prototype[verifyMethodName]
64-
Linter.prototype[verifyMethodName] = createVerifyPatch(verify)
60+
// ESLint >= 8.4.0
61+
if (Linter.prototype._verifyWithFlatConfigArrayAndWithoutProcessors) {
62+
Linter.prototype._verifyWithFlatConfigArrayAndWithoutProcessors =
63+
createVerifyWithFlatConfigPatch(
64+
module,
65+
Linter.prototype._verifyWithFlatConfigArrayAndWithoutProcessors
66+
)
67+
}
6568

66-
const verifyWithFlatConfig =
67-
Linter.prototype._verifyWithFlatConfigArrayAndWithoutProcessors
68-
Linter.prototype._verifyWithFlatConfigArrayAndWithoutProcessors =
69-
createVerifyWithFlatConfigPatch(module, verifyWithFlatConfig)
69+
// ESLint >= 4.7.0 .. < 10.0
70+
if (Linter.prototype._verifyWithoutProcessors) {
71+
Linter.prototype._verifyWithoutProcessors = createVerifyPatch(
72+
Linter.prototype._verifyWithoutProcessors
73+
)
74+
}
7075
}
7176

7277
function getLinterFromModule(module) {

src/test/plugin.js

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -892,42 +892,36 @@ ifVersion(">= 5", describe, "compatibility with external HTML plugins", () => {
892892
"@html-eslint/require-img-alt": ["error"],
893893
},
894894
})
895-
assert.deepStrictEqual(
896-
messages.map((message) => ({
897-
...message,
898-
899-
// ESLint v8.54.0 adds suggestions for the no-console rule. As we are running tests on older
900-
// versions of ESLint, we need to ignore these suggestions.
901-
suggestions: "(ignored)",
902-
})),
903-
[
904-
{
905-
column: 1,
906-
endColumn: 13,
907-
endLine: 1,
908-
line: 1,
909-
message: "Missing `alt` attribute at `<img>` tag",
910-
messageId: "missingAlt",
911-
nodeType: null,
912-
ruleId: "@html-eslint/require-img-alt",
913-
severity: 2,
914-
suggestions: "(ignored)",
915-
},
916-
{
917-
column: 3,
918-
endColumn: 14,
919-
endLine: 3,
920-
line: 3,
921-
message: "Unexpected console statement.",
922-
messageId: "unexpected",
923-
nodeType: "MemberExpression",
924-
ruleId: "no-console",
925-
severity: 2,
926-
source: ' console.log("toto")',
927-
suggestions: "(ignored)",
928-
},
929-
]
930-
)
895+
for (const message of messages) {
896+
// ESLint v8.54.0 adds suggestions for the no-console rule. As we are running tests on older
897+
// versions of ESLint, we need to ignore these suggestions.
898+
delete message.suggestions
899+
// ESLint v10.0 removed the nodeType property
900+
delete message.nodeType
901+
}
902+
assert.deepStrictEqual(messages, [
903+
{
904+
column: 1,
905+
endColumn: 13,
906+
endLine: 1,
907+
line: 1,
908+
message: "Missing `alt` attribute at `<img>` tag",
909+
messageId: "missingAlt",
910+
ruleId: "@html-eslint/require-img-alt",
911+
severity: 2,
912+
},
913+
{
914+
column: 3,
915+
endColumn: 14,
916+
endLine: 3,
917+
line: 3,
918+
message: "Unexpected console statement.",
919+
messageId: "unexpected",
920+
ruleId: "no-console",
921+
severity: 2,
922+
source: ' console.log("toto")',
923+
},
924+
])
931925
})
932926

933927
it("fix", async () => {

src/verifyWithFlatConfigPatch.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ function createVerifyWithFlatConfigPatch(eslintModule, verifyWithFlatConfig) {
6363
rules: {
6464
[PREPARE_RULE_NAME]: {
6565
create(context) {
66-
sourceCodes.set(codePart, context.getSourceCode())
66+
sourceCodes.set(
67+
codePart,
68+
context.sourceCode || context.getSourceCode()
69+
)
6770
return {
6871
Program(program) {
6972
if (prepare) {

src/verifyWithSharedScopes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function verifyWithSharedScopes(codeParts, verifyCodePart, parserOptions) {
6262
}
6363

6464
function markGlobalVariableAsUsed(context, program, name) {
65-
const sourceCode = context.getSourceCode()
65+
const sourceCode = context.sourceCode || context.getSourceCode()
6666

6767
if (sourceCode.markVariableAsUsed) {
6868
sourceCode.markVariableAsUsed(name, program)
@@ -72,7 +72,7 @@ function markGlobalVariableAsUsed(context, program, name) {
7272
}
7373

7474
function getGlobalScope(context, program) {
75-
const sourceCode = context.getSourceCode()
75+
const sourceCode = context.sourceCode || context.getSourceCode()
7676
if (sourceCode.getScope) {
7777
// eslint 9+
7878
return sourceCode.getScope(program)

0 commit comments

Comments
 (0)