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

refactor(typescript-estree): simplify applyModifiersToResult #3084

Merged
merged 1 commit into from Feb 21, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 5 additions & 10 deletions packages/typescript-estree/src/convert.ts
Expand Up @@ -580,13 +580,13 @@ export class Converter {
if (!modifiers || !modifiers.length) {
return;
}
const remainingModifiers: TSESTree.Modifier[] = [];
/**
* Some modifiers are explicitly handled by applying them as
* boolean values on the result node. As well as adding them
* to the result, we remove them from the array, so that they
* are not handled twice.
*/
const handledModifierIndices: { [key: number]: boolean } = {};
for (let i = 0; i < modifiers.length; i++) {
const modifier = modifiers[i];
switch (modifier.kind) {
Expand All @@ -596,31 +596,26 @@ export class Converter {
*/
case SyntaxKind.ExportKeyword:
case SyntaxKind.DefaultKeyword:
handledModifierIndices[i] = true;
break;
case SyntaxKind.ConstKeyword:
(result as any).const = true;
handledModifierIndices[i] = true;
break;
case SyntaxKind.DeclareKeyword:
result.declare = true;
handledModifierIndices[i] = true;
break;
default:
remainingModifiers.push(this.convertChild(modifier));
break;
}
}
/**
* If there are still valid modifiers available which have
* not been explicitly handled above, we just convert and
* add the modifiers array to the result node.
*/
const remainingModifiers = modifiers.filter(
(_, i) => !handledModifierIndices[i],
);
if (!remainingModifiers || !remainingModifiers.length) {
return;
if (remainingModifiers.length) {
result.modifiers = remainingModifiers;
}
result.modifiers = remainingModifiers.map(el => this.convertChild(el));
}

/**
Expand Down