Skip to content

Commit

Permalink
chore: extract eslint get souce code to util (#772)
Browse files Browse the repository at this point in the history
* chore: extract eslint get souce code to util

* extract null checks

* change import order for better style
  • Loading branch information
Samantha-Zhan authored Nov 12, 2024
1 parent ec13602 commit 93fd979
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 36 deletions.
10 changes: 3 additions & 7 deletions packages/eslint-plugin/src/stylex-no-unused.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

'use strict';

/*:: import { Rule } from 'eslint'; */
import type {
CallExpression,
Expression,
Expand All @@ -24,6 +23,8 @@ import type {
ExportNamedDeclaration,
ReturnStatement,
} from 'estree';
import getSourceCode from './utils/getSourceCode';
/*:: import { Rule } from 'eslint'; */

type PropertyValue =
| Property
Expand Down Expand Up @@ -244,12 +245,7 @@ const stylexNoUnused = {
},

'Program:exit'() {
// Fallback to legacy `getSourceCode()` for compatibility with older ESLint versions
const sourceCode =
context.sourceCode ||
(typeof context.getSourceCode === 'function'
? context.getSourceCode()
: null);
const sourceCode = getSourceCode(context);

stylexProperties.forEach((namespaces, varName) => {
namespaces.forEach((node, namespaceName) => {
Expand Down
14 changes: 2 additions & 12 deletions packages/eslint-plugin/src/stylex-sort-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type {
ObjectExpression,
Comment,
} from 'estree';
import getSourceCode from './utils/getSourceCode';
import getPropertyName from './utils/getPropertyName';
import getPropertyPriorityAndType from './utils/getPropertyPriorityAndType';
/*:: import { Rule } from 'eslint'; */
Expand Down Expand Up @@ -224,18 +225,7 @@ const stylexSortKeys = {
const currName = getPropertyName(node);
let isBlankLineBetweenNodes = stack?.prevBlankLine;

// Fallback to legacy `getSourceCode()` for compatibility with older ESLint versions
const sourceCode =
context.sourceCode ||
(typeof context.getSourceCode === 'function'
? context.getSourceCode()
: 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/',
);
}
const sourceCode = getSourceCode(context);

const tokens =
stack?.prevNode &&
Expand Down
19 changes: 2 additions & 17 deletions packages/eslint-plugin/src/stylex-valid-shorthands.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,15 @@ import type {
ObjectExpression,
Comment,
} from 'estree';

import type { SourceCode } from 'eslint/eslint-rule';

import type { Token } from 'eslint/eslint-ast';

import {
createBlockInlineTransformer,
createSpecificTransformer,
createDirectionalTransformer,
} from './utils/splitShorthands.js';

import { CANNOT_FIX } from './utils/splitShorthands.js';

import getSourceCode from './utils/getSourceCode';
/*:: import { Rule } from 'eslint'; */

const legacyNameMapping: $ReadOnly<{ [key: string]: ?string }> = {
Expand Down Expand Up @@ -179,18 +175,7 @@ const stylexValidShorthands = {
},
fix: !isUnfixableError
? (fixer) => {
// Fallback to legacy `getSourceCode()` for compatibility with older ESLint versions
const sourceCode =
context.sourceCode ||
(typeof context.getSourceCode === 'function'
? context.getSourceCode()
: 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/',
);
}
const sourceCode = getSourceCode(context);

const startNodeIndentation = getNodeIndentation(
sourceCode,
Expand Down
28 changes: 28 additions & 0 deletions packages/eslint-plugin/src/utils/getSourceCode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
*/

'use strict';

import type { SourceCode } from 'eslint/eslint-rule';
/*:: import { Rule } from 'eslint'; */

// Fallback to legacy `getSourceCode()` for compatibility with older ESLint versions
export default function getSourceCode(context: Rule.RuleContext): SourceCode {
const sourceCode =
context.sourceCode ||
(typeof context.getSourceCode === 'function'
? context.getSourceCode()
: 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 93fd979

Please sign in to comment.