Skip to content

Commit

Permalink
[Refactor] getFirstTokens: context -> sourceCode
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed May 18, 2024
1 parent 0c804bb commit 65f10f9
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 9 deletions.
7 changes: 5 additions & 2 deletions lib/rules/jsx-sort-default-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ const variableUtil = require('../util/variable');
const docsUrl = require('../util/docsUrl');
const report = require('../util/report');
const log = require('../util/log');
const getSourceCode = require('../util/eslint').getSourceCode;
const eslintUtil = require('../util/eslint');

const getFirstTokens = eslintUtil.getFirstTokens;
const getSourceCode = eslintUtil.getSourceCode;

let isWarnedForDeprecation = false;

Expand Down Expand Up @@ -66,7 +69,7 @@ module.exports = {
// (babel-eslint@5 does not expose property name so we have to rely on tokens)
}
if (node.type === 'ClassProperty') {
const tokens = getSourceCode(context).getFirstTokens(node, 2);
const tokens = getFirstTokens(context, node, 2);
return tokens[1] && tokens[1].type === 'Identifier' ? tokens[1].value : tokens[0].value;
}
return '';
Expand Down
7 changes: 5 additions & 2 deletions lib/rules/jsx-tag-spacing.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
const getTokenBeforeClosingBracket = require('../util/getTokenBeforeClosingBracket');
const docsUrl = require('../util/docsUrl');
const report = require('../util/report');
const getSourceCode = require('../util/eslint').getSourceCode;
const eslintUtil = require('../util/eslint');

const getFirstTokens = eslintUtil.getFirstTokens;
const getSourceCode = eslintUtil.getSourceCode;

const messages = {
selfCloseSlashNoSpace: 'Whitespace is forbidden between `/` and `>`; write `/>`',
Expand Down Expand Up @@ -65,7 +68,7 @@ function validateClosingSlash(context, node, option) {
});
}
} else {
const firstTokens = sourceCode.getFirstTokens(node, 2);
const firstTokens = getFirstTokens(context, node, 2);

adjacent = !sourceCode.isSpaceBetweenTokens(firstTokens[0], firstTokens[1]);

Expand Down
7 changes: 5 additions & 2 deletions lib/rules/sort-default-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
const variableUtil = require('../util/variable');
const docsUrl = require('../util/docsUrl');
const report = require('../util/report');
const getSourceCode = require('../util/eslint').getSourceCode;
const eslintUtil = require('../util/eslint');

const getFirstTokens = eslintUtil.getFirstTokens;
const getSourceCode = eslintUtil.getSourceCode;

// ------------------------------------------------------------------------------
// Rule Definition
Expand Down Expand Up @@ -61,7 +64,7 @@ module.exports = {
// (babel-eslint@5 does not expose property name so we have to rely on tokens)
}
if (node.type === 'ClassProperty') {
const tokens = getSourceCode(context).getFirstTokens(node, 2);
const tokens = getFirstTokens(context, node, 2);
return tokens[1] && tokens[1].type === 'Identifier' ? tokens[1].value : tokens[0].value;
}
return '';
Expand Down
4 changes: 3 additions & 1 deletion lib/util/annotations.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

'use strict';

const getFirstTokens = require('./eslint').getFirstTokens;

/**
* Checks if we are declaring a `props` argument with a flow type annotation.
* @param {ASTNode} node The AST node being checked.
Expand All @@ -19,7 +21,7 @@ function isAnnotatedFunctionPropsDeclaration(node, context) {

const typeNode = node.params[0].type === 'AssignmentPattern' ? node.params[0].left : node.params[0];

const tokens = context.getFirstTokens(typeNode, 2);
const tokens = getFirstTokens(context, typeNode, 2);
const isAnnotated = typeNode.typeAnnotation;
const isDestructuredProps = typeNode.type === 'ObjectPattern';
const isProps = tokens[0].value === 'props' || (tokens[1] && tokens[1].value === 'props');
Expand Down
3 changes: 2 additions & 1 deletion lib/util/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
const estraverse = require('estraverse');
const eslintUtil = require('./eslint');

const getFirstTokens = eslintUtil.getFirstTokens;
const getSourceCode = eslintUtil.getSourceCode;
// const pragmaUtil = require('./pragma');

Expand Down Expand Up @@ -287,7 +288,7 @@ function stripQuotes(string) {
*/
function getKeyValue(context, node) {
if (node.type === 'ObjectTypeProperty') {
const tokens = getSourceCode(context).getFirstTokens(node, 2);
const tokens = getFirstTokens(context, node, 2);
return (tokens[0].value === '+' || tokens[0].value === '-'
? tokens[1].value
: stripQuotes(tokens[0].value)
Expand Down
6 changes: 6 additions & 0 deletions lib/util/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ function markVariableAsUsed(name, node, context) {
: context.markVariableAsUsed(name);
}

function getFirstTokens(context, node, count) {
const sourceCode = getSourceCode(context);
return sourceCode.getFirstTokens ? sourceCode.getFirstTokens(node, count) : context.getFirstTokens(node, count);
}

module.exports = {
getAncestors,
getFirstTokens,
getSourceCode,
markVariableAsUsed,
};
3 changes: 2 additions & 1 deletion lib/util/propTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const astUtil = require('./ast');
const isFirstLetterCapitalized = require('./isFirstLetterCapitalized');
const eslintUtil = require('./eslint');

const getFirstTokens = eslintUtil.getFirstTokens;
const getSourceCode = eslintUtil.getSourceCode;

/**
Expand Down Expand Up @@ -1112,7 +1113,7 @@ module.exports = function propTypesInstructions(context, components, utils) {
*/
function isAnnotatedClassPropsDeclaration(node) {
if (node && (node.type === 'ClassProperty' || node.type === 'PropertyDefinition')) {
const tokens = context.getFirstTokens(node, 2);
const tokens = getFirstTokens(context, node, 2);
if (
node.typeAnnotation && (
tokens[0].value === 'props'
Expand Down

0 comments on commit 65f10f9

Please sign in to comment.