Skip to content

Commit

Permalink
Support problems with a range instead of a node (typescript-eslint 7.…
Browse files Browse the repository at this point in the history
…8.0) (#78)
  • Loading branch information
abrahamguo committed Apr 30, 2024
1 parent ca1fe47 commit 232c95e
Show file tree
Hide file tree
Showing 4 changed files with 427 additions and 421 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.idea
63 changes: 17 additions & 46 deletions lib/rules/predicates.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,23 @@
exports.unusedVarsPredicate = (problem, context) => {
const { node } = problem;
const { parent } = node;

// If parent is null just let the composed rule handle it
if (parent == null) {
return problem;
}
const makePredicate =
(isImport, addFixer) =>
(problem, { sourceCode }) => {
const { parent } =
problem.node ??
// typescript-eslint >= 7.8 sets a range instead of a node
sourceCode.getNodeByRangeIndex(sourceCode.getIndexFromLoc(problem.loc.start));
return parent
? /^Import(|Default|Namespace)Specifier$/.test(parent.type) == isImport &&
Object.assign(problem, addFixer?.(parent, sourceCode))
: problem; // If parent is null just let the composed rule handle it
};

// Remove these 3 cases, pass any other trough.
switch (parent.type) {
case "ImportSpecifier":
case "ImportDefaultSpecifier":
case "ImportNamespaceSpecifier":
return false;
default:
return problem;
}
};
exports.unusedVarsPredicate = makePredicate(false);

const commaFilter = { filter: (token) => token.value === "," };
const includeCommentsFilter = { includeComments: true };

exports.unusedImportsPredicate = (problem, context) => {
const { sourceCode } = context;

const { node } = problem;
const { parent } = node;

// If parent is null just let the composed rule handle it
if (parent == null) {
return problem;
}

// Only handle these 3 cases.
switch (parent.type) {
case "ImportSpecifier":
case "ImportDefaultSpecifier":
case "ImportNamespaceSpecifier":
break;
default:
return false;
}

problem.fix = (fixer) => {
if (!parent) {
return null;
}
exports.unusedImportsPredicate = makePredicate(true, (parent, sourceCode) => ({
fix(fixer) {
const grandParent = parent.parent;

if (!grandParent) {
Expand Down Expand Up @@ -99,6 +71,5 @@ exports.unusedImportsPredicate = (problem, context) => {
sourceCode.getTokenBefore(parent, commaFilter).range[0],
parent.range[1],
]);
};
return problem;
};
},
}));

0 comments on commit 232c95e

Please sign in to comment.