From 8f9a88cd63c432e2fdac384e7f9b3d0f91402aa0 Mon Sep 17 00:00:00 2001 From: Manzoor Wani Date: Sun, 15 Mar 2026 00:35:33 +0530 Subject: [PATCH] Modernize eslint-plugin rule APIs for ESLint v10 compatibility MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Update all custom rules in @wordpress/eslint-plugin to use modern ESLint APIs, preparing for the v10 upgrade: - context.getScope() → context.sourceCode.getScope(node) - context.getFilename() → context.filename - context.getSourceCode() → context.sourceCode - context.getDeclaredVariables() → context.sourceCode.getDeclaredVariables() - context.getAncestors() → context.sourceCode.getAncestors(node) - context.getCommentsBefore() → sourceCode.getCommentsBefore() Also: - Add missing meta.schema to i18n-translator-comments rule - Remove type: from error assertions in 3 test files (no-i18n-in-save, no-unsafe-wp-apis, use-recommended-components) — removed in v10 RuleTester All changes are backward-compatible with ESLint v8.40+. --- .../rules/__tests__/no-i18n-in-save.js | 14 -------------- .../rules/__tests__/no-unsafe-wp-apis.js | 5 ----- .../rules/__tests__/use-recommended-components.js | 5 ----- .../rules/data-no-store-string-literals.js | 13 ++++++++----- packages/eslint-plugin/rules/dependency-group.js | 7 +++---- .../rules/i18n-translator-comments.js | 8 ++++++-- packages/eslint-plugin/rules/no-i18n-in-save.js | 2 +- .../rules/no-unused-vars-before-return.js | 13 +++++++------ .../eslint-plugin/rules/react-no-unsafe-timeout.js | 2 +- 9 files changed, 26 insertions(+), 43 deletions(-) diff --git a/packages/eslint-plugin/rules/__tests__/no-i18n-in-save.js b/packages/eslint-plugin/rules/__tests__/no-i18n-in-save.js index 33dd38b9d88bb7..5abbf4e5e2bf25 100644 --- a/packages/eslint-plugin/rules/__tests__/no-i18n-in-save.js +++ b/packages/eslint-plugin/rules/__tests__/no-i18n-in-save.js @@ -90,7 +90,6 @@ function render() { errors: [ { messageId: 'noI18nInSave', - type: 'CallExpression', }, ], }, @@ -103,7 +102,6 @@ function save() { errors: [ { messageId: 'noI18nInSave', - type: 'CallExpression', }, ], }, @@ -116,7 +114,6 @@ const save = () => { errors: [ { messageId: 'noI18nInSave', - type: 'CallExpression', }, ], }, @@ -129,7 +126,6 @@ const save = function() { errors: [ { messageId: 'noI18nInSave', - type: 'CallExpression', }, ], }, @@ -142,7 +138,6 @@ export default function save() { errors: [ { messageId: 'noI18nInSave', - type: 'CallExpression', }, ], }, @@ -157,7 +152,6 @@ const settings = { errors: [ { messageId: 'noI18nInSave', - type: 'CallExpression', }, ], }, @@ -170,7 +164,6 @@ const settings = { errors: [ { messageId: 'noI18nInSave', - type: 'CallExpression', }, ], }, @@ -183,7 +176,6 @@ function save() { errors: [ { messageId: 'noI18nInSave', - type: 'CallExpression', }, ], }, @@ -197,7 +189,6 @@ function save() { errors: [ { messageId: 'noI18nInSave', - type: 'CallExpression', }, ], }, @@ -211,7 +202,6 @@ function save() { errors: [ { messageId: 'noI18nInSave', - type: 'CallExpression', }, ], }, @@ -228,7 +218,6 @@ function save() { errors: [ { messageId: 'noI18nInSave', - type: 'CallExpression', }, ], }, @@ -243,11 +232,9 @@ function save() { errors: [ { messageId: 'noI18nInSave', - type: 'CallExpression', }, { messageId: 'noI18nInSave', - type: 'CallExpression', }, ], }, @@ -262,7 +249,6 @@ function save() { errors: [ { messageId: 'noI18nInSave', - type: 'CallExpression', }, ], }, diff --git a/packages/eslint-plugin/rules/__tests__/no-unsafe-wp-apis.js b/packages/eslint-plugin/rules/__tests__/no-unsafe-wp-apis.js index 946bf3cbd1c44e..24345d07363b61 100644 --- a/packages/eslint-plugin/rules/__tests__/no-unsafe-wp-apis.js +++ b/packages/eslint-plugin/rules/__tests__/no-unsafe-wp-apis.js @@ -64,7 +64,6 @@ ruleTester.run( 'no-unsafe-wp-apis', rule, { { message: `Usage of \`__experimentalUnsafe\` from \`@wordpress/package\` is not allowed. See https://developer.wordpress.org/block-editor/contributors/develop/coding-guidelines/#experimental-and-unstable-apis for details.`, - type: 'ImportSpecifier', }, ], }, @@ -75,7 +74,6 @@ See https://developer.wordpress.org/block-editor/contributors/develop/coding-gui { message: `Usage of \`__experimentalSafe\` from \`@wordpress/unsafe\` is not allowed. See https://developer.wordpress.org/block-editor/contributors/develop/coding-guidelines/#experimental-and-unstable-apis for details.`, - type: 'ImportSpecifier', }, ], }, @@ -86,7 +84,6 @@ See https://developer.wordpress.org/block-editor/contributors/develop/coding-gui { message: `Usage of \`__experimentalSafe\` from \`@wordpress/unsafe\` is not allowed. See https://developer.wordpress.org/block-editor/contributors/develop/coding-guidelines/#experimental-and-unstable-apis for details.`, - type: 'ImportSpecifier', }, ], }, @@ -97,7 +94,6 @@ See https://developer.wordpress.org/block-editor/contributors/develop/coding-gui { message: `Usage of \`__experimentalUnsafe\` from \`@wordpress/package\` is not allowed. See https://developer.wordpress.org/block-editor/contributors/develop/coding-guidelines/#experimental-and-unstable-apis for details.`, - type: 'ImportSpecifier', }, ], }, @@ -108,7 +104,6 @@ See https://developer.wordpress.org/block-editor/contributors/develop/coding-gui { message: `Usage of \`__unstableFeature\` from \`@wordpress/package\` is not allowed. See https://developer.wordpress.org/block-editor/contributors/develop/coding-guidelines/#experimental-and-unstable-apis for details.`, - type: 'ImportSpecifier', }, ], }, diff --git a/packages/eslint-plugin/rules/__tests__/use-recommended-components.js b/packages/eslint-plugin/rules/__tests__/use-recommended-components.js index bc3db1c6d6181c..c146f297db659e 100644 --- a/packages/eslint-plugin/rules/__tests__/use-recommended-components.js +++ b/packages/eslint-plugin/rules/__tests__/use-recommended-components.js @@ -41,7 +41,6 @@ ruleTester.run( 'use-recommended-components', rule, { { message: '`SomeComponent` from `@wordpress/ui` is not yet recommended for use in a WordPress environment.', - type: 'ImportSpecifier', }, ], }, @@ -51,12 +50,10 @@ ruleTester.run( 'use-recommended-components', rule, { { message: '`Foo` from `@wordpress/ui` is not yet recommended for use in a WordPress environment.', - type: 'ImportSpecifier', }, { message: '`Bar` from `@wordpress/ui` is not yet recommended for use in a WordPress environment.', - type: 'ImportSpecifier', }, ], }, @@ -67,7 +64,6 @@ ruleTester.run( 'use-recommended-components', rule, { { message: '__experimentalZStack is planned for deprecation. Write your own CSS instead.', - type: 'ImportSpecifier', }, ], }, @@ -77,7 +73,6 @@ ruleTester.run( 'use-recommended-components', rule, { { message: '__experimentalZStack is planned for deprecation. Write your own CSS instead.', - type: 'ImportSpecifier', }, ], }, diff --git a/packages/eslint-plugin/rules/data-no-store-string-literals.js b/packages/eslint-plugin/rules/data-no-store-string-literals.js index 1b977ed278c194..4e63f13d4f8f8e 100644 --- a/packages/eslint-plugin/rules/data-no-store-string-literals.js +++ b/packages/eslint-plugin/rules/data-no-store-string-literals.js @@ -31,7 +31,7 @@ function arrayLast( array ) { function getReferences( context, specifiers ) { const variables = specifiers.reduce( ( acc, specifier ) => - acc.concat( context.getDeclaredVariables( specifier ) ), + acc.concat( context.sourceCode.getDeclaredVariables( specifier ) ), [] ); const references = variables.reduce( @@ -58,7 +58,9 @@ function collectAllNodesFromCallbackFunctions( context, node ) { ( acc, { identifier: { parent } } ) => parent && parent.arguments && parent.arguments.length > 0 ? acc.concat( - context.getDeclaredVariables( parent.arguments[ 0 ] ) + context.sourceCode.getDeclaredVariables( + parent.arguments[ 0 ] + ) ) : acc, [] @@ -153,9 +155,10 @@ function getFixes( fixer, context, callNode ) { fixer.replaceText( callNode.arguments[ 0 ], variableName ), ]; - const imports = context - .getAncestors()[ 0 ] - .body.filter( ( node ) => node.type === 'ImportDeclaration' ); + const ancestors = context.sourceCode.getAncestors( callNode ); + const imports = ancestors[ 0 ].body.filter( + ( node ) => node.type === 'ImportDeclaration' + ); const packageImports = imports.filter( ( node ) => node.source.value === importName ); diff --git a/packages/eslint-plugin/rules/dependency-group.js b/packages/eslint-plugin/rules/dependency-group.js index dcb309fe73cdb8..3715461c7c7203 100644 --- a/packages/eslint-plugin/rules/dependency-group.js +++ b/packages/eslint-plugin/rules/dependency-group.js @@ -22,7 +22,8 @@ module.exports = { }, create( context ) { const mode = context.options[ 0 ] || 'always'; - const comments = context.getSourceCode().getAllComments(); + const sourceCode = context.sourceCode; + const comments = sourceCode.getAllComments(); /** * Locality classification of an import, one of "External", @@ -194,9 +195,7 @@ module.exports = { return null; } - const text = context - .getSourceCode() - .getText(); + const text = sourceCode.getText(); // Trim preceding and trailing newlines. let [ start, end ] = comment.range; diff --git a/packages/eslint-plugin/rules/i18n-translator-comments.js b/packages/eslint-plugin/rules/i18n-translator-comments.js index b5b3302142364c..6bcacbccf0d54c 100644 --- a/packages/eslint-plugin/rules/i18n-translator-comments.js +++ b/packages/eslint-plugin/rules/i18n-translator-comments.js @@ -61,6 +61,7 @@ function extractTranslatorKeys( commentText ) { module.exports = { meta: { type: 'problem', + schema: [], messages: { missing: 'Translation function with placeholders is missing preceding translator comment', @@ -71,6 +72,7 @@ module.exports = { }, }, create( context ) { + const sourceCode = context.sourceCode; return { CallExpression( node ) { const { @@ -107,7 +109,7 @@ module.exports = { return; } - const comments = context.getCommentsBefore( node ).slice(); + const comments = sourceCode.getCommentsBefore( node ).slice(); let parentNode = parent; @@ -123,7 +125,9 @@ module.exports = { parentNode.type !== 'Program' && Math.abs( parentNode.loc.start.line - currentLine ) <= 1 ) { - comments.push( ...context.getCommentsBefore( parentNode ) ); + comments.push( + ...sourceCode.getCommentsBefore( parentNode ) + ); parentNode = parentNode.parent; } diff --git a/packages/eslint-plugin/rules/no-i18n-in-save.js b/packages/eslint-plugin/rules/no-i18n-in-save.js index 282e730ea941b7..1061527e935fdb 100644 --- a/packages/eslint-plugin/rules/no-i18n-in-save.js +++ b/packages/eslint-plugin/rules/no-i18n-in-save.js @@ -22,7 +22,7 @@ module.exports = { }, create( context ) { let saveFunctionDepth = 0; - const filename = context.getFilename(); + const filename = context.filename; // Skip deprecated files as they preserve old behavior including translation functions const normalizedFilename = filename.replace( /\\/g, '/' ); diff --git a/packages/eslint-plugin/rules/no-unused-vars-before-return.js b/packages/eslint-plugin/rules/no-unused-vars-before-return.js index f9139969245b46..ca6429d200d7cd 100644 --- a/packages/eslint-plugin/rules/no-unused-vars-before-return.js +++ b/packages/eslint-plugin/rules/no-unused-vars-before-return.js @@ -11,15 +11,16 @@ const FUNCTION_SCOPE_JSX_IDENTIFIERS = new WeakMap(); /** - * Returns the closest function scope for the current ESLint context object, or - * undefined if it cannot be determined. + * Returns the closest function scope for the given node, or undefined if it + * cannot be determined. * * @param {ESLintRuleContext} context ESLint context object. + * @param {ESTreeNode} node Current AST node. * * @return {ESLintScope|undefined} Function scope, if known. */ -function getClosestFunctionScope( context ) { - let functionScope = context.getScope(); +function getClosestFunctionScope( context, node ) { + let functionScope = context.sourceCode.getScope( node ); while ( functionScope.type !== 'function' && functionScope.upper ) { functionScope = functionScope.upper; } @@ -73,7 +74,7 @@ module.exports = /** @type {import('eslint').Rule} */ ( { // identifiers. Account for this by visiting JSX identifiers // first, and tracking them in a map per function scope, which // is later merged with the known variable references. - const functionScope = getClosestFunctionScope( context ); + const functionScope = getClosestFunctionScope( context, node ); if ( ! functionScope ) { return; } @@ -88,7 +89,7 @@ module.exports = /** @type {import('eslint').Rule} */ ( { FUNCTION_SCOPE_JSX_IDENTIFIERS.get( functionScope ).add( node ); }, 'ReturnStatement:exit'( node ) { - const functionScope = getClosestFunctionScope( context ); + const functionScope = getClosestFunctionScope( context, node ); if ( ! functionScope ) { return; } diff --git a/packages/eslint-plugin/rules/react-no-unsafe-timeout.js b/packages/eslint-plugin/rules/react-no-unsafe-timeout.js index 8253d0e3c1f03e..4804f33246ad51 100644 --- a/packages/eslint-plugin/rules/react-no-unsafe-timeout.js +++ b/packages/eslint-plugin/rules/react-no-unsafe-timeout.js @@ -71,7 +71,7 @@ module.exports = { // Consider whether `setTimeout` is a reference to the global // by checking references to see if `setTimeout` resolves to a // variable in scope. - const { references } = context.getScope(); + const { references } = context.sourceCode.getScope( node ); const hasResolvedReference = references.some( ( reference ) => reference.identifier.name === 'setTimeout' &&