Skip to content

Commit

Permalink
feat(eslint-plugin): update deprecated methods
Browse files Browse the repository at this point in the history
  • Loading branch information
bryceosterhaus committed Jun 24, 2024
1 parent 2407184 commit 5515c4a
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 48 deletions.
2 changes: 1 addition & 1 deletion projects/eslint-plugin/rules/general/lib/common/imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function getLeadingComments(node, context) {
//
// something(); // I'm a trailing comment.

const tokenBefore = context.getTokenBefore(comment, {
const tokenBefore = code.getTokenBefore(comment, {
includeComments: true,
});

Expand Down
10 changes: 5 additions & 5 deletions projects/eslint-plugin/rules/general/lib/rules/group-imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ const DESCRIPTION = 'imports must be grouped';

module.exports = {
create(context) {
const source = context.getSourceCode();

const imports = [];

const {scope, visitors} = withScope();

function expectBlankLines(node, count = 1) {
const comments = getLeadingComments(node, context);
const initial = comments[0] || node;
const token = context.getTokenBefore(initial, {
const token = source.getTokenBefore(initial, {
includeComments: true,
});

if (token) {
const source = context.getSourceCode();

const start = token.range[1];
const end = initial.range[0];

Expand Down Expand Up @@ -155,9 +155,9 @@ module.exports = {
continue;
}

const token = context.getTokenBefore(current);
const token = source.getTokenBefore(current);

const last = context.getNodeByRangeIndex(token.range[0]);
const last = source.getNodeByRangeIndex(token.range[0]);

if (last !== previous) {
expectBlankLines(current);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const {

module.exports = {
create(context) {
const source = context.getSourceCode();
const {scope, visitors} = withScope();

let lastImportIndex = -1;
Expand Down Expand Up @@ -49,13 +50,13 @@ module.exports = {

// Haven't seen a non-import yet, so must search.

const token = context.getTokenBefore(current);
const token = source.getTokenBefore(current);

if (!token) {
break;
}

current = context.getNodeByRangeIndex(token.range[0]);
current = source.getNodeByRangeIndex(token.range[0]);

if (isDirective(current)) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const message =

module.exports = {
create(context) {
const source = context.getSourceCode();

return {
LogicalExpression(node) {
const leftSideLength =
Expand All @@ -26,8 +28,8 @@ module.exports = {
}

if (rightSideLength || leftSideLength) {
const jsxExpressionScope = context
.getAncestors()
const jsxExpressionScope = source
.getAncestors(node)
.find((node) => node.type === 'JSXExpressionContainer');

if (jsxExpressionScope) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ module.exports = {
CallExpression(node) {
for (const isMatch of CALL_EXPRESSIONS) {
if (isMatch(node)) {
const token = context.getTokenBefore(node);
const token = source.getTokenBefore(node);

if (token) {
const previous = context.getNodeByRangeIndex(
const previous = source.getNodeByRangeIndex(
token.range[0]
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const NAME_PATTERN = /.*Ref/;

module.exports = {
create(context) {
const source = context.getSourceCode();
return {
VariableDeclarator(node) {
if (
Expand All @@ -25,7 +26,7 @@ module.exports = {
variableName !== 'ref' &&
!variableName.match(NAME_PATTERN)
) {
const [variable] = context.getDeclaredVariables(node);
const [variable] = source.getDeclaredVariables(node);
const newVariableName = variable.name + 'Ref';

for (const reference of variable.references) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const DESCRIPTION = 'useState must follow naming pattern `const [* , set*] =`';

module.exports = {
create(context) {
const source = context.getSourceCode();

return {
CallExpression(node) {
const reactUseState =
Expand Down Expand Up @@ -61,7 +63,7 @@ module.exports = {
return;
}

const variable = context
const variable = source
.getDeclaredVariables(node.parent)
.find((item) => item.name === setterVariableName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ const DESCRIPTION =

module.exports = {
create(context) {
const source = context.getSourceCode();

const isReactDOMImport = (node) => {
const ancestors = context.getAncestors(node);
const ancestors = source.getAncestors(node);

const parent = ancestors[ancestors.length - 1];

Expand All @@ -27,7 +29,7 @@ module.exports = {

const isSame = (identifier, variable) => {
const name = variable.name;
let scope = context.getScope(identifier);
let scope = source.getScope(identifier);

while (scope) {
for (let i = 0; i < scope.variables.length; i++) {
Expand Down Expand Up @@ -111,7 +113,7 @@ module.exports = {
*/
ImportDefaultSpecifier(node) {
if (isReactDOMImport(node)) {
add(foundNamespaces, context.getDeclaredVariables(node));
add(foundNamespaces, source.getDeclaredVariables(node));
}
},

Expand All @@ -122,7 +124,7 @@ module.exports = {
*/
ImportNamespaceSpecifier(node) {
if (isReactDOMImport(node)) {
add(foundNamespaces, context.getDeclaredVariables(node));
add(foundNamespaces, source.getDeclaredVariables(node));
}
},

Expand All @@ -136,17 +138,15 @@ module.exports = {
if (isReactDOMImport(node)) {
add(
foundBindings,
context
.getDeclaredVariables(node)
.filter((variable) => {
return (
variable.defs[0] &&
variable.defs[0].node &&
variable.defs[0].node.imported &&
variable.defs[0].node.imported.name ===
'createPortal'
);
})
source.getDeclaredVariables(node).filter((variable) => {
return (
variable.defs[0] &&
variable.defs[0].node &&
variable.defs[0].node.imported &&
variable.defs[0].node.imported.name ===
'createPortal'
);
})
);
}
},
Expand All @@ -161,7 +161,7 @@ module.exports = {
node.init.arguments[0].type === 'Literal' &&
node.init.arguments[0].value === 'react-dom'
) {
const variables = context.getDeclaredVariables(node);
const variables = source.getDeclaredVariables(node);

if (node.id.type === 'Identifier') {

Expand All @@ -176,7 +176,7 @@ module.exports = {

add(
foundBindings,
context
source
.getDeclaredVariables(node)
.filter((variable) => {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ const DESCRIPTION =

module.exports = {
create(context) {
const source = context.getSourceCode();

const isReactDOMImport = (node) => {
const ancestors = context.getAncestors(node);
const ancestors = source.getAncestors(node);

const parent = ancestors[ancestors.length - 1];

Expand All @@ -27,7 +29,7 @@ module.exports = {

const isSame = (identifier, variable) => {
const name = variable.name;
let scope = context.getScope(identifier);
let scope = source.getScope(identifier);

while (scope) {
for (let i = 0; i < scope.variables.length; i++) {
Expand Down Expand Up @@ -111,7 +113,7 @@ module.exports = {
*/
ImportDefaultSpecifier(node) {
if (isReactDOMImport(node)) {
add(foundNamespaces, context.getDeclaredVariables(node));
add(foundNamespaces, source.getDeclaredVariables(node));
}
},

Expand All @@ -122,7 +124,7 @@ module.exports = {
*/
ImportNamespaceSpecifier(node) {
if (isReactDOMImport(node)) {
add(foundNamespaces, context.getDeclaredVariables(node));
add(foundNamespaces, source.getDeclaredVariables(node));
}
},

Expand All @@ -136,17 +138,14 @@ module.exports = {
if (isReactDOMImport(node)) {
add(
foundBindings,
context
.getDeclaredVariables(node)
.filter((variable) => {
return (
variable.defs[0] &&
variable.defs[0].node &&
variable.defs[0].node.imported &&
variable.defs[0].node.imported.name ===
'render'
);
})
source.getDeclaredVariables(node).filter((variable) => {
return (
variable.defs[0] &&
variable.defs[0].node &&
variable.defs[0].node.imported &&
variable.defs[0].node.imported.name === 'render'
);
})
);
}
},
Expand All @@ -161,7 +160,7 @@ module.exports = {
node.init.arguments[0].type === 'Literal' &&
node.init.arguments[0].value === 'react-dom'
) {
const variables = context.getDeclaredVariables(node);
const variables = source.getDeclaredVariables(node);

if (node.id.type === 'Identifier') {

Expand All @@ -176,7 +175,7 @@ module.exports = {

add(
foundBindings,
context
source
.getDeclaredVariables(node)
.filter((variable) => {
return (
Expand Down

0 comments on commit 5515c4a

Please sign in to comment.