diff --git a/packages/eslint-plugin/src/stylex-sort-keys.js b/packages/eslint-plugin/src/stylex-sort-keys.js index 16fe0ce8..8850d170 100644 --- a/packages/eslint-plugin/src/stylex-sort-keys.js +++ b/packages/eslint-plugin/src/stylex-sort-keys.js @@ -227,12 +227,6 @@ const stylexSortKeys = { const sourceCode = getSourceCode(context); - if (!sourceCode) { - throw new Error( - 'ESLint context does not provide source code access. Please update ESLint to v>=8.40.0. See: https://eslint.org/blog/2023/09/preparing-custom-rules-eslint-v9/', - ); - } - const tokens = stack?.prevNode && sourceCode.getTokensBetween(stack.prevNode, node, { diff --git a/packages/eslint-plugin/src/stylex-valid-shorthands.js b/packages/eslint-plugin/src/stylex-valid-shorthands.js index 577d62f7..dcdf6c56 100644 --- a/packages/eslint-plugin/src/stylex-valid-shorthands.js +++ b/packages/eslint-plugin/src/stylex-valid-shorthands.js @@ -183,12 +183,6 @@ const stylexValidShorthands = { ? (fixer) => { const sourceCode = getSourceCode(context); - if (!sourceCode) { - throw new Error( - 'ESLint context does not provide source code access. Please update ESLint to v>=8.40.0. See: https://eslint.org/blog/2023/09/preparing-custom-rules-eslint-v9/', - ); - } - const startNodeIndentation = getNodeIndentation( sourceCode, property, diff --git a/packages/eslint-plugin/src/utils/getSourceCode.js b/packages/eslint-plugin/src/utils/getSourceCode.js index 85e1ffd9..6db0fc40 100644 --- a/packages/eslint-plugin/src/utils/getSourceCode.js +++ b/packages/eslint-plugin/src/utils/getSourceCode.js @@ -13,13 +13,16 @@ import type { SourceCode } from 'eslint/eslint-rule'; // Fallback to legacy `getSourceCode()` for compatibility with older ESLint versions -export default function getSourceCode( - context: Rule.RuleContext, -): SourceCode | null { - return ( +export default function getSourceCode(context: Rule.RuleContext): SourceCode { + const sourceCode = context.sourceCode || (typeof context.getSourceCode === 'function' ? context.getSourceCode() - : null) - ); + : null); + if (!sourceCode) { + throw new Error( + 'ESLint context does not provide source code access. Please update ESLint to v>=8.40.0. See: https://eslint.org/blog/2023/09/preparing-custom-rules-eslint-v9/', + ); + } + return sourceCode; }