Skip to content

Commit

Permalink
extract null checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Samantha-Zhan committed Nov 12, 2024
1 parent b829838 commit 4e4b985
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 18 deletions.
6 changes: 0 additions & 6 deletions packages/eslint-plugin/src/stylex-sort-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
6 changes: 0 additions & 6 deletions packages/eslint-plugin/src/stylex-valid-shorthands.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 9 additions & 6 deletions packages/eslint-plugin/src/utils/getSourceCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 4e4b985

Please sign in to comment.