From 31b1a49f652cac710b3ce838f5864cabc6ac8309 Mon Sep 17 00:00:00 2001 From: SegaraRai Date: Thu, 22 Aug 2024 23:42:11 +0900 Subject: [PATCH] fix: compatibility issue with ESLint v9 (#137) * fix: use `context.sourceCode.getScope` for compatibility with ESLint v9 * chore: stop using deprecated APIs --- package.json | 2 +- src/rules/attach-shadow-constructor.ts | 2 +- src/rules/define-tag-after-class-definition.ts | 2 +- src/rules/expose-class-on-global.ts | 2 +- src/rules/file-name-matches-element.ts | 4 ++-- src/rules/guard-super-call.ts | 2 +- src/rules/max-elements-per-file.ts | 2 +- src/rules/no-child-traversal-in-attributechangedcallback.ts | 2 +- src/rules/no-child-traversal-in-connectedcallback.ts | 4 ++-- src/rules/no-constructor-attributes.ts | 2 +- src/rules/no-constructor-params.ts | 2 +- src/rules/no-constructor.ts | 2 +- src/rules/no-customized-built-in-elements.ts | 2 +- src/rules/no-exports-with-element.ts | 2 +- src/rules/no-invalid-extends.ts | 2 +- src/rules/no-method-prefixed-with-on.ts | 4 ++-- src/rules/no-self-class.ts | 2 +- src/rules/no-typos.ts | 2 +- src/rules/require-listener-teardown.ts | 6 +++--- src/util/ast.ts | 3 +-- 20 files changed, 25 insertions(+), 26 deletions(-) diff --git a/package.json b/package.json index 9b481bc..b416d7e 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,7 @@ "js-levenshtein-esm": "^1.2.0" }, "peerDependencies": { - "eslint": ">=5" + "eslint": ">=8.40.0" }, "devDependencies": { "@types/chai": "^4.3.5", diff --git a/src/rules/attach-shadow-constructor.ts b/src/rules/attach-shadow-constructor.ts index 5f2c8b7..e468aeb 100644 --- a/src/rules/attach-shadow-constructor.ts +++ b/src/rules/attach-shadow-constructor.ts @@ -27,7 +27,7 @@ const rule: Rule.RuleModule = { // variables should be defined here let insideNonConstructor = false; let insideElement = false; - const source = context.getSourceCode(); + const source = context.sourceCode; //---------------------------------------------------------------------- // Helpers diff --git a/src/rules/define-tag-after-class-definition.ts b/src/rules/define-tag-after-class-definition.ts index 735fce9..ae622ac 100644 --- a/src/rules/define-tag-after-class-definition.ts +++ b/src/rules/define-tag-after-class-definition.ts @@ -35,7 +35,7 @@ const rule: Rule.RuleModule = { create(context): Rule.RuleListener { const seenClasses = new Set(); - const source = context.getSourceCode(); + const source = context.sourceCode; //---------------------------------------------------------------------- // Helpers diff --git a/src/rules/expose-class-on-global.ts b/src/rules/expose-class-on-global.ts index c90f2dd..2cbcc4d 100644 --- a/src/rules/expose-class-on-global.ts +++ b/src/rules/expose-class-on-global.ts @@ -33,7 +33,7 @@ const rule: Rule.RuleModule = { create(context): Rule.RuleListener { const seenClasses = new Set(); - const source = context.getSourceCode(); + const source = context.sourceCode; //---------------------------------------------------------------------- // Helpers diff --git a/src/rules/file-name-matches-element.ts b/src/rules/file-name-matches-element.ts index 0a3be87..eb1bfeb 100644 --- a/src/rules/file-name-matches-element.ts +++ b/src/rules/file-name-matches-element.ts @@ -116,14 +116,14 @@ const rule: Rule.RuleModule = { }, create(context): Rule.RuleListener { - const source = context.getSourceCode(); + const source = context.sourceCode; const options = context.options?.[0] ?? {}; const userSuffixes = coerceArray(options.suffix ?? []); const userPrefixes = coerceArray(options.prefix ?? []); const transforms = coerceArray(options.transform ?? ['kebab', 'camel']); const matchDirectory = options.matchDirectory === true; - const filename = context.getFilename(); + const filename = context.filename; // We don't want to match stdin and what not if (!filename || filename === '' || filename === '') { diff --git a/src/rules/guard-super-call.ts b/src/rules/guard-super-call.ts index 64af374..c4f011c 100644 --- a/src/rules/guard-super-call.ts +++ b/src/rules/guard-super-call.ts @@ -27,7 +27,7 @@ const rule: Rule.RuleModule = { create(context): Rule.RuleListener { let insideNonNativeElement = false; let errNode = null; - const source = context.getSourceCode(); + const source = context.sourceCode; const nativeHooks = [ 'connectedCallback', diff --git a/src/rules/max-elements-per-file.ts b/src/rules/max-elements-per-file.ts index ce18f4f..108b423 100644 --- a/src/rules/max-elements-per-file.ts +++ b/src/rules/max-elements-per-file.ts @@ -36,7 +36,7 @@ const rule: Rule.RuleModule = { create(context): Rule.RuleListener { // variables should be defined here - const source = context.getSourceCode(); + const source = context.sourceCode; const maxElements = context.options[0]?.max ?? 1; let elementCount = 0; diff --git a/src/rules/no-child-traversal-in-attributechangedcallback.ts b/src/rules/no-child-traversal-in-attributechangedcallback.ts index b3f8bba..3599ac8 100644 --- a/src/rules/no-child-traversal-in-attributechangedcallback.ts +++ b/src/rules/no-child-traversal-in-attributechangedcallback.ts @@ -40,7 +40,7 @@ const rule: Rule.RuleModule = { // variables should be defined here let insideCallback = false; let insideElement = false; - const source = context.getSourceCode(); + const source = context.sourceCode; //---------------------------------------------------------------------- // Helpers diff --git a/src/rules/no-child-traversal-in-connectedcallback.ts b/src/rules/no-child-traversal-in-connectedcallback.ts index c0cb832..60cf87d 100644 --- a/src/rules/no-child-traversal-in-connectedcallback.ts +++ b/src/rules/no-child-traversal-in-connectedcallback.ts @@ -85,7 +85,7 @@ const rule: Rule.RuleModule = { // variables should be defined here let insideCallback = false; let insideElement = false; - const source = context.getSourceCode(); + const source = context.sourceCode; //---------------------------------------------------------------------- // Helpers @@ -136,7 +136,7 @@ const rule: Rule.RuleModule = { return; } - const scope = context.getScope(); + const scope = context.sourceCode.getScope(node); const name = node.property.name; if (isAllowedScope(scope)) { diff --git a/src/rules/no-constructor-attributes.ts b/src/rules/no-constructor-attributes.ts index ea5778f..ca43186 100644 --- a/src/rules/no-constructor-attributes.ts +++ b/src/rules/no-constructor-attributes.ts @@ -72,7 +72,7 @@ const rule: Rule.RuleModule = { 'title', 'translate' ]; - const source = context.getSourceCode(); + const source = context.sourceCode; //---------------------------------------------------------------------- // Helpers diff --git a/src/rules/no-constructor-params.ts b/src/rules/no-constructor-params.ts index e2d4fe4..66c9caf 100644 --- a/src/rules/no-constructor-params.ts +++ b/src/rules/no-constructor-params.ts @@ -29,7 +29,7 @@ const rule: Rule.RuleModule = { 'ClassBody > MethodDefinition[kind="constructor"]' + '[value.params.length > 0]'; let insideElement = false; - const source = context.getSourceCode(); + const source = context.sourceCode; //---------------------------------------------------------------------- // Helpers diff --git a/src/rules/no-constructor.ts b/src/rules/no-constructor.ts index fafc301..8382931 100644 --- a/src/rules/no-constructor.ts +++ b/src/rules/no-constructor.ts @@ -28,7 +28,7 @@ const rule: Rule.RuleModule = { create(context): Rule.RuleListener { // variables should be defined here let insideElement = false; - const source = context.getSourceCode(); + const source = context.sourceCode; //---------------------------------------------------------------------- // Helpers diff --git a/src/rules/no-customized-built-in-elements.ts b/src/rules/no-customized-built-in-elements.ts index 6b4ebf6..460cd29 100644 --- a/src/rules/no-customized-built-in-elements.ts +++ b/src/rules/no-customized-built-in-elements.ts @@ -28,7 +28,7 @@ const rule: Rule.RuleModule = { create(context): Rule.RuleListener { // variables should be defined here - const source = context.getSourceCode(); + const source = context.sourceCode; //---------------------------------------------------------------------- // Helpers diff --git a/src/rules/no-exports-with-element.ts b/src/rules/no-exports-with-element.ts index 8ea70b6..0ac6346 100644 --- a/src/rules/no-exports-with-element.ts +++ b/src/rules/no-exports-with-element.ts @@ -30,7 +30,7 @@ const rule: Rule.RuleModule = { const seenClasses = new Set(); const exportedNodes = new Set(); let hasElement = false; - const source = context.getSourceCode(); + const source = context.sourceCode; return { 'ClassDeclaration,ClassExpression': (node: ESTree.Class): void => { diff --git a/src/rules/no-invalid-extends.ts b/src/rules/no-invalid-extends.ts index 366e110..8c5270b 100644 --- a/src/rules/no-invalid-extends.ts +++ b/src/rules/no-invalid-extends.ts @@ -96,7 +96,7 @@ const rule: Rule.RuleModule = { }, create(context): Rule.RuleListener { - const source = context.getSourceCode(); + const source = context.sourceCode; const elementClasses = new Set(); const userAllowedSuperNames = context.options[0]?.allowedSuperNames ?? []; const elementBaseClasses = getElementBaseClasses(context); diff --git a/src/rules/no-method-prefixed-with-on.ts b/src/rules/no-method-prefixed-with-on.ts index 6cf1327..a3d8696 100644 --- a/src/rules/no-method-prefixed-with-on.ts +++ b/src/rules/no-method-prefixed-with-on.ts @@ -29,7 +29,7 @@ const rule: Rule.RuleModule = { create(context): Rule.RuleListener { // variables should be defined here let insideElement = false; - const source = context.getSourceCode(); + const source = context.sourceCode; //---------------------------------------------------------------------- // Helpers @@ -52,7 +52,7 @@ const rule: Rule.RuleModule = { if (insideElement) { const name = getMethodName(node); - if (name && name.startsWith('on')) { + if (name?.startsWith('on')) { context.report({ node, messageId: 'noPrefix' diff --git a/src/rules/no-self-class.ts b/src/rules/no-self-class.ts index 6f0a082..14e0a77 100644 --- a/src/rules/no-self-class.ts +++ b/src/rules/no-self-class.ts @@ -28,7 +28,7 @@ const rule: Rule.RuleModule = { // variables should be defined here let insideElement = false; const bannedClassListMethods = ['add', 'remove', 'toggle', 'replace']; - const source = context.getSourceCode(); + const source = context.sourceCode; //---------------------------------------------------------------------- // Helpers diff --git a/src/rules/no-typos.ts b/src/rules/no-typos.ts index cfb4839..66a088f 100644 --- a/src/rules/no-typos.ts +++ b/src/rules/no-typos.ts @@ -29,7 +29,7 @@ const rule: Rule.RuleModule = { create(context): Rule.RuleListener { // variables should be defined here let insideElement = false; - const source = context.getSourceCode(); + const source = context.sourceCode; const lifecycleMethods = [ 'connectedCallback', 'disconnectedCallback', diff --git a/src/rules/require-listener-teardown.ts b/src/rules/require-listener-teardown.ts index 2bba2c4..9588f63 100644 --- a/src/rules/require-listener-teardown.ts +++ b/src/rules/require-listener-teardown.ts @@ -60,7 +60,7 @@ const rule: Rule.RuleModule = { // Helpers //---------------------------------------------------------------------- const shouldTrackListener = (node: ESTree.MemberExpression): boolean => { - const source = context.getSourceCode(); + const source = context.sourceCode; const text = source.getText(node.object); @@ -82,7 +82,7 @@ const rule: Rule.RuleModule = { node.callee.type === 'MemberExpression' && shouldTrackListener(node.callee) ) { - const source = context.getSourceCode(); + const source = context.sourceCode; const calleeText = source.getText(node.callee.object); const [arg0, arg1] = node.arguments; @@ -104,7 +104,7 @@ const rule: Rule.RuleModule = { node.callee.type === 'MemberExpression' && shouldTrackListener(node.callee) ) { - const source = context.getSourceCode(); + const source = context.sourceCode; const calleeText = source.getText(node.callee.object); const [arg0, arg1] = node.arguments; diff --git a/src/util/ast.ts b/src/util/ast.ts index 3bdf51d..a026942 100644 --- a/src/util/ast.ts +++ b/src/util/ast.ts @@ -35,8 +35,7 @@ export function resolveReference( return node; } - const ref = context - .getSourceCode() + const ref = context.sourceCode .getScope(node) .references.find((r) => r.identifier.name === node.name);