Skip to content

Commit

Permalink
refactor(typescript-estree): simplify applyModifiersToResult (#3084)
Browse files Browse the repository at this point in the history
  • Loading branch information
armano2 committed Feb 21, 2021
1 parent 763a252 commit 0469102
Showing 1 changed file with 5 additions and 10 deletions.
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

0 comments on commit 0469102

Please sign in to comment.