Skip to content

feat(eslint-plugin): [no-unused-vars] add destructuredArrayIgnorePattern options #4748

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

Merged
Show file tree
Hide file tree
Changes from all commits
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
73 changes: 59 additions & 14 deletions packages/eslint-plugin/src/rules/no-unused-vars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type Options = [
argsIgnorePattern?: string;
caughtErrors?: 'all' | 'none';
caughtErrorsIgnorePattern?: string;
destructuredArrayIgnorePattern?: string;
},
];

Expand All @@ -25,6 +26,7 @@ interface TranslatedOptions {
argsIgnorePattern?: RegExp;
caughtErrors: 'all' | 'none';
caughtErrorsIgnorePattern?: RegExp;
destructuredArrayIgnorePattern?: RegExp;
}

export default util.createRule<Options, MessageIds>({
Expand Down Expand Up @@ -66,6 +68,9 @@ export default util.createRule<Options, MessageIds>({
caughtErrorsIgnorePattern: {
type: 'string',
},
destructuredArrayIgnorePattern: {
type: 'string',
},
},
additionalProperties: false,
},
Expand Down Expand Up @@ -123,12 +128,33 @@ export default util.createRule<Options, MessageIds>({
'u',
);
}

if (firstOption.destructuredArrayIgnorePattern) {
options.destructuredArrayIgnorePattern = new RegExp(
firstOption.destructuredArrayIgnorePattern,
'u',
);
}
}
}
return options;
})();

function collectUnusedVariables(): TSESLint.Scope.Variable[] {
/**
* Checks whether a node is a sibling of the rest property or not.
* @param {ASTNode} node a node to check
* @returns {boolean} True if the node is a sibling of the rest property, otherwise false.
*/
function hasRestSibling(node: TSESTree.Node): boolean {
return (
node.type === AST_NODE_TYPES.Property &&
node.parent?.type === AST_NODE_TYPES.ObjectPattern &&
node.parent.properties[node.parent.properties.length - 1].type ===
AST_NODE_TYPES.RestElement
);
}

/**
* Determines if a variable has a sibling rest property
* @param variable eslint-scope variable object.
Expand All @@ -138,17 +164,14 @@ export default util.createRule<Options, MessageIds>({
variable: TSESLint.Scope.Variable,
): boolean {
if (options.ignoreRestSiblings) {
return variable.defs.some(def => {
const propertyNode = def.name.parent!;
const patternNode = propertyNode.parent!;

return (
propertyNode.type === AST_NODE_TYPES.Property &&
patternNode.type === AST_NODE_TYPES.ObjectPattern &&
patternNode.properties[patternNode.properties.length - 1].type ===
AST_NODE_TYPES.RestElement
);
});
const hasRestSiblingDefinition = variable.defs.some(def =>
hasRestSibling(def.name.parent!),
);
const hasRestSiblingReference = variable.references.some(ref =>
hasRestSibling(ref.identifier.parent!),
);

return hasRestSiblingDefinition || hasRestSiblingReference;
}

return false;
Expand Down Expand Up @@ -188,6 +211,20 @@ export default util.createRule<Options, MessageIds>({
continue;
}

const refUsedInArrayPatterns = variable.references.some(
ref => ref.identifier.parent?.type === AST_NODE_TYPES.ArrayPattern,
);

// skip elements of array destructuring patterns
if (
(def.name.parent?.type === AST_NODE_TYPES.ArrayPattern ||
refUsedInArrayPatterns) &&
'name' in def.name &&
options.destructuredArrayIgnorePattern?.test(def.name.name)
) {
continue;
}

// skip catch variables
if (def.type === TSESLint.Scope.DefinitionType.CatchClause) {
if (options.caughtErrors === 'none') {
Expand Down Expand Up @@ -361,9 +398,17 @@ export default util.createRule<Options, MessageIds>({
function getAssignedMessageData(
unusedVar: TSESLint.Scope.Variable,
): Record<string, unknown> {
const additional = options.varsIgnorePattern
? `. Allowed unused vars must match ${options.varsIgnorePattern.toString()}`
: '';
const def = unusedVar.defs[0];
let additional = '';

if (
options.destructuredArrayIgnorePattern &&
def?.name.parent?.type === AST_NODE_TYPES.ArrayPattern
) {
additional = `. Allowed unused elements of array destructuring patterns must match ${options.destructuredArrayIgnorePattern.toString()}`;
} else if (options.varsIgnorePattern) {
additional = `. Allowed unused vars must match ${options.varsIgnorePattern.toString()}`;
}

return {
varName: unusedVar.name,
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy