Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add eslint v9 support #3743

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Adjust all rules and utils
  • Loading branch information
MatiPl01 committed Apr 28, 2024
commit ba1b2c64fc2d3ec8169a81e9398354bcfb131025
7 changes: 4 additions & 3 deletions lib/rules/boolean-prop-naming.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const propsUtil = require('../util/props');
const docsUrl = require('../util/docsUrl');
const propWrapperUtil = require('../util/propWrapper');
const report = require('../util/report');
const { getSourceCode } = require('../util/eslint');

// ------------------------------------------------------------------------------
// Rule Definition
Expand Down Expand Up @@ -115,7 +116,7 @@ module.exports = {
// we can't get the name of the Flow object key name. So we have
// to hack around it for now.
if (node.type === 'ObjectTypeProperty') {
return context.getSourceCode().getFirstToken(node).value;
return getSourceCode(context).getFirstToken(node).value;
}

return node.key.name;
Expand Down Expand Up @@ -308,7 +309,7 @@ module.exports = {
&& node.value.type === 'CallExpression'
&& propWrapperUtil.isPropWrapperFunction(
context,
context.getSourceCode().getText(node.value.callee)
getSourceCode(context).getText(node.value.callee)
)
) {
checkPropWrapperArguments(node, node.value.arguments);
Expand All @@ -334,7 +335,7 @@ module.exports = {
right.type === 'CallExpression'
&& propWrapperUtil.isPropWrapperFunction(
context,
context.getSourceCode().getText(right.callee)
getSourceCode(context).getText(right.callee)
)
) {
checkPropWrapperArguments(component.node, right.arguments);
Expand Down
13 changes: 7 additions & 6 deletions lib/rules/destructuring-assignment.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

const Components = require('../util/Components');
const docsUrl = require('../util/docsUrl');
const { getScope, getSourceCode } = require('../util/eslint');
const isAssignmentLHS = require('../util/ast').isAssignmentLHS;
const report = require('../util/report');

Expand Down Expand Up @@ -102,7 +103,7 @@ module.exports = {
function handleStatelessComponent(node) {
const params = evalParams(node.params);

const SFCComponent = components.get(context.getScope(node).block);
const SFCComponent = components.get(getScope(node, context).block);
if (!SFCComponent) {
return;
}
Expand All @@ -120,7 +121,7 @@ module.exports = {
}

function handleStatelessComponentExit(node) {
const SFCComponent = components.get(context.getScope(node).block);
const SFCComponent = components.get(getScope(node, context).block);
if (SFCComponent) {
sfcParams.pop();
}
Expand Down Expand Up @@ -192,7 +193,7 @@ module.exports = {
'FunctionExpression:exit': handleStatelessComponentExit,

MemberExpression(node) {
let scope = context.getScope(node);
let scope = getScope(node, context);
let SFCComponent = components.get(scope.block);
while (!SFCComponent && scope.upper && scope.upper !== scope) {
SFCComponent = components.get(scope.upper.block);
Expand All @@ -210,7 +211,7 @@ module.exports = {

VariableDeclarator(node) {
const classComponent = utils.getParentComponent(node);
const SFCComponent = components.get(context.getScope(node).block);
const SFCComponent = components.get(getScope(node, context).block);

const destructuring = (node.init && node.id && node.id.type === 'ObjectPattern');
// let {foo} = props;
Expand Down Expand Up @@ -248,7 +249,7 @@ module.exports = {
&& destructureInSignature === 'always'
&& node.init.name === 'props'
) {
const scopeSetProps = context.getScope().set.get('props');
const scopeSetProps = getScope(node, context).set.get('props');
const propsRefs = scopeSetProps && scopeSetProps.references;
if (!propsRefs) {
return;
Expand All @@ -269,7 +270,7 @@ module.exports = {
param.typeAnnotation ? param.typeAnnotation.range[0] : param.range[1],
];
return [
fixer.replaceTextRange(replaceRange, context.getSourceCode().getText(node.id)),
fixer.replaceTextRange(replaceRange, getSourceCode(context).getText(node.id)),
fixer.remove(node.parent),
];
},
Expand Down
5 changes: 3 additions & 2 deletions lib/rules/forbid-elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

const has = require('object.hasown/polyfill')();
const docsUrl = require('../util/docsUrl');
const { getSourceCode } = require('../util/eslint');
const isCreateElement = require('../util/isCreateElement');
const report = require('../util/report');

Expand Down Expand Up @@ -90,7 +91,7 @@ module.exports = {

return {
JSXOpeningElement(node) {
reportIfForbidden(context.getSourceCode().getText(node.name), node.name);
reportIfForbidden(getSourceCode(context).getText(node.name), node.name);
},

CallExpression(node) {
Expand All @@ -110,7 +111,7 @@ module.exports = {
} else if (argType === 'Literal' && /^[a-z][^.]*$/.test(argument.value)) {
reportIfForbidden(argument.value, argument);
} else if (argType === 'MemberExpression') {
reportIfForbidden(context.getSourceCode().getText(argument), argument);
reportIfForbidden(getSourceCode(context).getText(argument), argument);
}
},
};
Expand Down
10 changes: 7 additions & 3 deletions lib/rules/forbid-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const astUtil = require('../util/ast');
const docsUrl = require('../util/docsUrl');
const propWrapperUtil = require('../util/propWrapper');
const report = require('../util/report');
const { getSourceCode } = require('../util/eslint');

// ------------------------------------------------------------------------------
// Constants
Expand Down Expand Up @@ -162,7 +163,7 @@ module.exports = {
checkProperties(node.properties);
break;
case 'Identifier': {
const propTypesObject = variableUtil.findVariableByName(context, node.name);
const propTypesObject = variableUtil.findVariableByName(node, context, node.name);
if (propTypesObject && propTypesObject.properties) {
checkProperties(propTypesObject.properties);
}
Expand All @@ -171,8 +172,11 @@ module.exports = {
case 'CallExpression': {
const innerNode = node.arguments && node.arguments[0];
if (
propWrapperUtil.isPropWrapperFunction(context, context.getSourceCode().getText(node.callee))
&& innerNode
propWrapperUtil.isPropWrapperFunction(
context,
getSourceCode(context).getText(node.callee)
) &&
innerNode
) {
checkNode(innerNode);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/function-component-definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const arrayIncludes = require('array-includes');
const Components = require('../util/Components');
const docsUrl = require('../util/docsUrl');
const reportC = require('../util/report');
const { getSourceCode } = require('../util/eslint');

// ------------------------------------------------------------------------------
// Rule Definition
Expand Down Expand Up @@ -181,7 +182,7 @@ module.exports = {
);

function getFixer(node, options) {
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);
const source = sourceCode.getText();

const typeAnnotation = getTypeAnnotation(node, source);
Expand Down
48 changes: 30 additions & 18 deletions lib/rules/hook-use-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,24 +154,36 @@ module.exports = {
useMemoCode = 'useMemo';
}

suggestions.unshift(Object.assign(
getMessageData('suggestMemo', messages.suggestMemo),
{
fix: (fixer) => [
// Add useMemo import, if necessary
useStateReactImportSpecifier
&& (!useMemoReactImportSpecifier || defaultReactImportName)
&& fixer.insertTextAfter(useStateReactImportSpecifier, ', useMemo'),
// Convert single-value destructure to simple assignment
fixer.replaceTextRange(node.parent.id.range, valueVariableName),
// Convert useState call to useMemo + arrow function + dependency array
fixer.replaceTextRange(
node.range,
`${useMemoCode}(() => ${context.getSourceCode().getText(node.arguments[0])}, [])`
),
].filter(Boolean),
}
));
suggestions.unshift(
Object.assign(
getMessageData('suggestMemo', messages.suggestMemo),
{
fix: (fixer) =>
[
// Add useMemo import, if necessary
useStateReactImportSpecifier &&
(!useMemoReactImportSpecifier ||
defaultReactImportName) &&
fixer.insertTextAfter(
useStateReactImportSpecifier,
', useMemo'
),
// Convert single-value destructure to simple assignment
fixer.replaceTextRange(
node.parent.id.range,
valueVariableName
),
// Convert useState call to useMemo + arrow function + dependency array
fixer.replaceTextRange(
node.range,
`${useMemoCode}(() => ${getSourceCode(context).getText(
node.arguments[0]
)}, [])`
),
].filter(Boolean),
}
)
);
}

if (isOnlyValueDestructuring) {
Expand Down
11 changes: 8 additions & 3 deletions lib/rules/jsx-closing-bracket-location.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

const has = require('object.hasown/polyfill')();
const docsUrl = require('../util/docsUrl');
const { getSourceCode } = require('../util/eslint');
const report = require('../util/report');

// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -170,11 +171,15 @@ module.exports = {
let spaces = [];
switch (expectedLocation) {
case 'props-aligned':
indentation = /^\s*/.exec(context.getSourceCode().lines[tokens.lastProp.firstLine - 1])[0];
indentation = /^\s*/.exec(
getSourceCode(context).lines[tokens.lastProp.firstLine - 1]
)[0];
break;
case 'tag-aligned':
case 'line-aligned':
indentation = /^\s*/.exec(context.getSourceCode().lines[tokens.opening.line - 1])[0];
indentation = /^\s*/.exec(
getSourceCode(context).lines[tokens.opening.line - 1]
)[0];
break;
default:
indentation = '';
Expand All @@ -194,7 +199,7 @@ module.exports = {
* prop and start of opening line.
*/
function getTokensLocations(node) {
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);
const opening = sourceCode.getFirstToken(node).loc.start;
const closing = sourceCode.getLastTokens(node, node.selfClosing ? 2 : 1)[0].loc.start;
const tag = sourceCode.getFirstToken(node.name).loc.start;
Expand Down
12 changes: 8 additions & 4 deletions lib/rules/jsx-curly-brace-presence.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const arrayIncludes = require('array-includes');
const docsUrl = require('../util/docsUrl');
const jsxUtil = require('../util/jsx');
const report = require('../util/report');
const { getSourceCode } = require('../util/eslint');

// ------------------------------------------------------------------------------
// Constants
Expand Down Expand Up @@ -176,7 +177,7 @@ module.exports = {

let textToReplace;
if (jsxUtil.isJSX(expression)) {
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);
textToReplace = sourceCode.getText(expression);
} else {
const expressionType = expression && expression.type;
Expand All @@ -188,7 +189,7 @@ module.exports = {
: expression.raw.slice(1, -1)
}"`;
} else if (jsxUtil.isJSX(expression)) {
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);

textToReplace = sourceCode.getText(expression);
} else {
Expand All @@ -207,7 +208,10 @@ module.exports = {
node: literalNode,
fix(fixer) {
if (jsxUtil.isJSX(literalNode)) {
return fixer.replaceText(literalNode, `{${context.getSourceCode().getText(literalNode)}}`);
return fixer.replaceText(
literalNode,
`{${getSourceCode(context).getText(literalNode)}}`
);
}

// If a HTML entity name is found, bail out because it can be fixed
Expand Down Expand Up @@ -251,7 +255,7 @@ module.exports = {
const expression = JSXExpressionNode.expression;
const expressionType = expression.type;

const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);
// Curly braces containing comments are necessary
if (sourceCode.getCommentsInside && sourceCode.getCommentsInside(JSXExpressionNode).length > 0) {
return;
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/jsx-curly-newline.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
'use strict';

const docsUrl = require('../util/docsUrl');
const { getSourceCode } = require('../util/eslint');
const report = require('../util/report');

// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -77,7 +78,7 @@ module.exports = {
},

create(context) {
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);
const option = getNormalizedOption(context);

// ----------------------------------------------------------------------
Expand Down
13 changes: 7 additions & 6 deletions lib/rules/jsx-curly-spacing.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

const has = require('object.hasown/polyfill')();
const docsUrl = require('../util/docsUrl');
const { getSourceCode } = require('../util/eslint');
const report = require('../util/report');

// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -175,7 +176,7 @@ module.exports = {
* @returns {Object|*|{range, text}}
*/
function fixByTrimmingWhitespace(fixer, fromLoc, toLoc, mode, spacing) {
let replacementText = context.getSourceCode().text.slice(fromLoc, toLoc);
let replacementText = getSourceCode(context).text.slice(fromLoc, toLoc);
if (mode === 'start') {
replacementText = replacementText.replace(/^\s+/gm, '');
} else {
Expand Down Expand Up @@ -206,7 +207,7 @@ module.exports = {
token: token.value,
},
fix(fixer) {
const nextToken = context.getSourceCode().getTokenAfter(token);
const nextToken = getSourceCode(context).getTokenAfter(token);
return fixByTrimmingWhitespace(fixer, token.range[1], nextToken.range[0], 'start', spacing);
},
});
Expand All @@ -227,7 +228,7 @@ module.exports = {
token: token.value,
},
fix(fixer) {
const previousToken = context.getSourceCode().getTokenBefore(token);
const previousToken = getSourceCode(context).getTokenBefore(token);
return fixByTrimmingWhitespace(fixer, previousToken.range[1], token.range[0], 'end', spacing);
},
});
Expand All @@ -247,7 +248,7 @@ module.exports = {
token: token.value,
},
fix(fixer) {
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);
const nextToken = sourceCode.getTokenAfter(token);
let nextComment;

Expand Down Expand Up @@ -284,7 +285,7 @@ module.exports = {
token: token.value,
},
fix(fixer) {
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);
const previousToken = sourceCode.getTokenBefore(token);
let previousComment;

Expand Down Expand Up @@ -370,7 +371,7 @@ module.exports = {
return;
}

const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);
const first = sourceCode.getFirstToken(node);
const last = sourceCode.getLastToken(node);
let second = sourceCode.getTokenAfter(first, { includeComments: true });
Expand Down
Loading