From 5515c4a409474ea162ad2b5be1562e08b93df959 Mon Sep 17 00:00:00 2001 From: Bryce Osterhaus Date: Mon, 24 Jun 2024 09:24:43 +0400 Subject: [PATCH] feat(eslint-plugin): update deprecated methods --- .../rules/general/lib/common/imports.js | 2 +- .../rules/general/lib/rules/group-imports.js | 10 +++--- .../rules/general/lib/rules/imports-first.js | 5 +-- .../lib/rules/no-length-jsx-expression.js | 6 ++-- .../general/lib/rules/padded-test-blocks.js | 4 +-- .../general/lib/rules/ref-name-suffix.js | 3 +- .../lib/rules/use-state-naming-pattern.js | 4 ++- .../lib/rules/no-react-dom-create-portal.js | 34 +++++++++---------- .../portal/lib/rules/no-react-dom-render.js | 33 +++++++++--------- 9 files changed, 53 insertions(+), 48 deletions(-) diff --git a/projects/eslint-plugin/rules/general/lib/common/imports.js b/projects/eslint-plugin/rules/general/lib/common/imports.js index c578136f97..d8ac45c467 100644 --- a/projects/eslint-plugin/rules/general/lib/common/imports.js +++ b/projects/eslint-plugin/rules/general/lib/common/imports.js @@ -43,7 +43,7 @@ function getLeadingComments(node, context) { // // something(); // I'm a trailing comment. - const tokenBefore = context.getTokenBefore(comment, { + const tokenBefore = code.getTokenBefore(comment, { includeComments: true, }); diff --git a/projects/eslint-plugin/rules/general/lib/rules/group-imports.js b/projects/eslint-plugin/rules/general/lib/rules/group-imports.js index 47b44d3d0a..db578690d5 100644 --- a/projects/eslint-plugin/rules/general/lib/rules/group-imports.js +++ b/projects/eslint-plugin/rules/general/lib/rules/group-imports.js @@ -17,6 +17,8 @@ const DESCRIPTION = 'imports must be grouped'; module.exports = { create(context) { + const source = context.getSourceCode(); + const imports = []; const {scope, visitors} = withScope(); @@ -24,13 +26,11 @@ module.exports = { function expectBlankLines(node, count = 1) { const comments = getLeadingComments(node, context); const initial = comments[0] || node; - const token = context.getTokenBefore(initial, { + const token = source.getTokenBefore(initial, { includeComments: true, }); if (token) { - const source = context.getSourceCode(); - const start = token.range[1]; const end = initial.range[0]; @@ -155,9 +155,9 @@ module.exports = { continue; } - const token = context.getTokenBefore(current); + const token = source.getTokenBefore(current); - const last = context.getNodeByRangeIndex(token.range[0]); + const last = source.getNodeByRangeIndex(token.range[0]); if (last !== previous) { expectBlankLines(current); diff --git a/projects/eslint-plugin/rules/general/lib/rules/imports-first.js b/projects/eslint-plugin/rules/general/lib/rules/imports-first.js index 97bf775b3a..4812d69412 100644 --- a/projects/eslint-plugin/rules/general/lib/rules/imports-first.js +++ b/projects/eslint-plugin/rules/general/lib/rules/imports-first.js @@ -12,6 +12,7 @@ const { module.exports = { create(context) { + const source = context.getSourceCode(); const {scope, visitors} = withScope(); let lastImportIndex = -1; @@ -49,13 +50,13 @@ module.exports = { // Haven't seen a non-import yet, so must search. - const token = context.getTokenBefore(current); + const token = source.getTokenBefore(current); if (!token) { break; } - current = context.getNodeByRangeIndex(token.range[0]); + current = source.getNodeByRangeIndex(token.range[0]); if (isDirective(current)) { diff --git a/projects/eslint-plugin/rules/general/lib/rules/no-length-jsx-expression.js b/projects/eslint-plugin/rules/general/lib/rules/no-length-jsx-expression.js index f7ac804541..8e0eb336cb 100644 --- a/projects/eslint-plugin/rules/general/lib/rules/no-length-jsx-expression.js +++ b/projects/eslint-plugin/rules/general/lib/rules/no-length-jsx-expression.js @@ -8,6 +8,8 @@ const message = module.exports = { create(context) { + const source = context.getSourceCode(); + return { LogicalExpression(node) { const leftSideLength = @@ -26,8 +28,8 @@ module.exports = { } if (rightSideLength || leftSideLength) { - const jsxExpressionScope = context - .getAncestors() + const jsxExpressionScope = source + .getAncestors(node) .find((node) => node.type === 'JSXExpressionContainer'); if (jsxExpressionScope) { diff --git a/projects/eslint-plugin/rules/general/lib/rules/padded-test-blocks.js b/projects/eslint-plugin/rules/general/lib/rules/padded-test-blocks.js index f4343c02e9..d096b423d2 100644 --- a/projects/eslint-plugin/rules/general/lib/rules/padded-test-blocks.js +++ b/projects/eslint-plugin/rules/general/lib/rules/padded-test-blocks.js @@ -106,10 +106,10 @@ module.exports = { CallExpression(node) { for (const isMatch of CALL_EXPRESSIONS) { if (isMatch(node)) { - const token = context.getTokenBefore(node); + const token = source.getTokenBefore(node); if (token) { - const previous = context.getNodeByRangeIndex( + const previous = source.getNodeByRangeIndex( token.range[0] ); diff --git a/projects/eslint-plugin/rules/general/lib/rules/ref-name-suffix.js b/projects/eslint-plugin/rules/general/lib/rules/ref-name-suffix.js index 7f2d54f6b0..4bab3bc139 100644 --- a/projects/eslint-plugin/rules/general/lib/rules/ref-name-suffix.js +++ b/projects/eslint-plugin/rules/general/lib/rules/ref-name-suffix.js @@ -9,6 +9,7 @@ const NAME_PATTERN = /.*Ref/; module.exports = { create(context) { + const source = context.getSourceCode(); return { VariableDeclarator(node) { if ( @@ -25,7 +26,7 @@ module.exports = { variableName !== 'ref' && !variableName.match(NAME_PATTERN) ) { - const [variable] = context.getDeclaredVariables(node); + const [variable] = source.getDeclaredVariables(node); const newVariableName = variable.name + 'Ref'; for (const reference of variable.references) { diff --git a/projects/eslint-plugin/rules/general/lib/rules/use-state-naming-pattern.js b/projects/eslint-plugin/rules/general/lib/rules/use-state-naming-pattern.js index f4e323e3bc..8c0dc944c2 100644 --- a/projects/eslint-plugin/rules/general/lib/rules/use-state-naming-pattern.js +++ b/projects/eslint-plugin/rules/general/lib/rules/use-state-naming-pattern.js @@ -7,6 +7,8 @@ const DESCRIPTION = 'useState must follow naming pattern `const [* , set*] =`'; module.exports = { create(context) { + const source = context.getSourceCode(); + return { CallExpression(node) { const reactUseState = @@ -61,7 +63,7 @@ module.exports = { return; } - const variable = context + const variable = source .getDeclaredVariables(node.parent) .find((item) => item.name === setterVariableName); diff --git a/projects/eslint-plugin/rules/portal/lib/rules/no-react-dom-create-portal.js b/projects/eslint-plugin/rules/portal/lib/rules/no-react-dom-create-portal.js index 0cde3555a7..850c53fe79 100644 --- a/projects/eslint-plugin/rules/portal/lib/rules/no-react-dom-create-portal.js +++ b/projects/eslint-plugin/rules/portal/lib/rules/no-react-dom-create-portal.js @@ -10,8 +10,10 @@ const DESCRIPTION = module.exports = { create(context) { + const source = context.getSourceCode(); + const isReactDOMImport = (node) => { - const ancestors = context.getAncestors(node); + const ancestors = source.getAncestors(node); const parent = ancestors[ancestors.length - 1]; @@ -27,7 +29,7 @@ module.exports = { const isSame = (identifier, variable) => { const name = variable.name; - let scope = context.getScope(identifier); + let scope = source.getScope(identifier); while (scope) { for (let i = 0; i < scope.variables.length; i++) { @@ -111,7 +113,7 @@ module.exports = { */ ImportDefaultSpecifier(node) { if (isReactDOMImport(node)) { - add(foundNamespaces, context.getDeclaredVariables(node)); + add(foundNamespaces, source.getDeclaredVariables(node)); } }, @@ -122,7 +124,7 @@ module.exports = { */ ImportNamespaceSpecifier(node) { if (isReactDOMImport(node)) { - add(foundNamespaces, context.getDeclaredVariables(node)); + add(foundNamespaces, source.getDeclaredVariables(node)); } }, @@ -136,17 +138,15 @@ module.exports = { if (isReactDOMImport(node)) { add( foundBindings, - context - .getDeclaredVariables(node) - .filter((variable) => { - return ( - variable.defs[0] && - variable.defs[0].node && - variable.defs[0].node.imported && - variable.defs[0].node.imported.name === - 'createPortal' - ); - }) + source.getDeclaredVariables(node).filter((variable) => { + return ( + variable.defs[0] && + variable.defs[0].node && + variable.defs[0].node.imported && + variable.defs[0].node.imported.name === + 'createPortal' + ); + }) ); } }, @@ -161,7 +161,7 @@ module.exports = { node.init.arguments[0].type === 'Literal' && node.init.arguments[0].value === 'react-dom' ) { - const variables = context.getDeclaredVariables(node); + const variables = source.getDeclaredVariables(node); if (node.id.type === 'Identifier') { @@ -176,7 +176,7 @@ module.exports = { add( foundBindings, - context + source .getDeclaredVariables(node) .filter((variable) => { return ( diff --git a/projects/eslint-plugin/rules/portal/lib/rules/no-react-dom-render.js b/projects/eslint-plugin/rules/portal/lib/rules/no-react-dom-render.js index 2423ea7907..54de330cc8 100644 --- a/projects/eslint-plugin/rules/portal/lib/rules/no-react-dom-render.js +++ b/projects/eslint-plugin/rules/portal/lib/rules/no-react-dom-render.js @@ -10,8 +10,10 @@ const DESCRIPTION = module.exports = { create(context) { + const source = context.getSourceCode(); + const isReactDOMImport = (node) => { - const ancestors = context.getAncestors(node); + const ancestors = source.getAncestors(node); const parent = ancestors[ancestors.length - 1]; @@ -27,7 +29,7 @@ module.exports = { const isSame = (identifier, variable) => { const name = variable.name; - let scope = context.getScope(identifier); + let scope = source.getScope(identifier); while (scope) { for (let i = 0; i < scope.variables.length; i++) { @@ -111,7 +113,7 @@ module.exports = { */ ImportDefaultSpecifier(node) { if (isReactDOMImport(node)) { - add(foundNamespaces, context.getDeclaredVariables(node)); + add(foundNamespaces, source.getDeclaredVariables(node)); } }, @@ -122,7 +124,7 @@ module.exports = { */ ImportNamespaceSpecifier(node) { if (isReactDOMImport(node)) { - add(foundNamespaces, context.getDeclaredVariables(node)); + add(foundNamespaces, source.getDeclaredVariables(node)); } }, @@ -136,17 +138,14 @@ module.exports = { if (isReactDOMImport(node)) { add( foundBindings, - context - .getDeclaredVariables(node) - .filter((variable) => { - return ( - variable.defs[0] && - variable.defs[0].node && - variable.defs[0].node.imported && - variable.defs[0].node.imported.name === - 'render' - ); - }) + source.getDeclaredVariables(node).filter((variable) => { + return ( + variable.defs[0] && + variable.defs[0].node && + variable.defs[0].node.imported && + variable.defs[0].node.imported.name === 'render' + ); + }) ); } }, @@ -161,7 +160,7 @@ module.exports = { node.init.arguments[0].type === 'Literal' && node.init.arguments[0].value === 'react-dom' ) { - const variables = context.getDeclaredVariables(node); + const variables = source.getDeclaredVariables(node); if (node.id.type === 'Identifier') { @@ -176,7 +175,7 @@ module.exports = { add( foundBindings, - context + source .getDeclaredVariables(node) .filter((variable) => { return (