Skip to content
This repository was archived by the owner on May 12, 2025. It is now read-only.

Commit d0867a9

Browse files
authored
export declarations support and bugfixes (#164)
1 parent b1ad719 commit d0867a9

File tree

22 files changed

+1628
-148
lines changed

22 files changed

+1628
-148
lines changed

openrewrite/src/javascript/parser.ts

Lines changed: 70 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -261,22 +261,19 @@ export class JavaScriptParserVisitor {
261261
private mapModifiers(node: ts.VariableDeclarationList | ts.VariableStatement | ts.ClassDeclaration | ts.PropertyDeclaration
262262
| ts.FunctionDeclaration | ts.ParameterDeclaration | ts.MethodDeclaration | ts.EnumDeclaration | ts.InterfaceDeclaration
263263
| ts.PropertySignature | ts.ConstructorDeclaration | ts.ModuleDeclaration | ts.GetAccessorDeclaration | ts.SetAccessorDeclaration
264-
| ts.ArrowFunction | ts.IndexSignatureDeclaration | ts.TypeAliasDeclaration) {
264+
| ts.ArrowFunction | ts.IndexSignatureDeclaration | ts.TypeAliasDeclaration | ts.ExportDeclaration | ts.ExportAssignment) {
265265
if (ts.isVariableStatement(node) || ts.isModuleDeclaration(node) || ts.isClassDeclaration(node) || ts.isEnumDeclaration(node)
266266
|| ts.isInterfaceDeclaration(node) || ts.isPropertyDeclaration(node) || ts.isPropertySignature(node) || ts.isParameter(node)
267-
|| ts.isMethodDeclaration(node) || ts.isConstructorDeclaration(node) || ts.isArrowFunction(node) || ts.isIndexSignatureDeclaration(node) || ts.isTypeAliasDeclaration(node)) {
267+
|| ts.isMethodDeclaration(node) || ts.isConstructorDeclaration(node) || ts.isArrowFunction(node)
268+
|| ts.isIndexSignatureDeclaration(node) || ts.isTypeAliasDeclaration(node) || ts.isExportDeclaration(node) || ts.isFunctionDeclaration(node)) {
268269
return node.modifiers ? node.modifiers?.filter(ts.isModifier).map(this.mapModifier) : [];
269-
} else if (ts.isFunctionDeclaration(node)) {
270-
return [...node.modifiers ? node.modifiers?.filter(ts.isModifier).map(this.mapModifier) : [],
271-
// empty modifier is used to capture spaces before FunctionKeyword
272-
new J.Modifier(
273-
randomId(),
274-
this.prefix(this.findChildNode(node, ts.SyntaxKind.FunctionKeyword)!),
275-
Markers.EMPTY,
276-
null,
277-
J.Modifier.Type.LanguageExtension,
278-
[]
279-
)]
270+
}
271+
else if (ts.isExportAssignment(node)) {
272+
const defaultModifier = this.findChildNode(node, ts.SyntaxKind.DefaultKeyword);
273+
return [
274+
...node.modifiers ? node.modifiers?.filter(ts.isModifier).map(this.mapModifier) : [],
275+
...defaultModifier && ts.isModifier(defaultModifier) ? [this.mapModifier(defaultModifier)] : []
276+
]
280277
}
281278
else if (ts.isVariableDeclarationList(node)) {
282279
let modifier: string | undefined;
@@ -661,7 +658,7 @@ export class JavaScriptParserVisitor {
661658
randomId(),
662659
this.prefix(node),
663660
Markers.EMPTY,
664-
[],
661+
this.mapDecorators(node),
665662
this.mapModifiers(node),
666663
this.mapTypeInfo(node),
667664
null,
@@ -685,7 +682,7 @@ export class JavaScriptParserVisitor {
685682
randomId(),
686683
this.prefix(node),
687684
Markers.EMPTY,
688-
[],
685+
this.mapDecorators(node),
689686
this.mapModifiers(node),
690687
this.mapTypeInfo(node),
691688
null,
@@ -718,7 +715,7 @@ export class JavaScriptParserVisitor {
718715
randomId(),
719716
this.prefix(node),
720717
Markers.EMPTY,
721-
[],
718+
this.mapDecorators(node),
722719
this.mapModifiers(node),
723720
this.mapTypeInfo(node),
724721
null,
@@ -742,7 +739,7 @@ export class JavaScriptParserVisitor {
742739
randomId(),
743740
this.prefix(node),
744741
Markers.EMPTY,
745-
[],
742+
this.mapDecorators(node),
746743
this.mapModifiers(node),
747744
this.mapTypeInfo(node),
748745
null,
@@ -849,7 +846,7 @@ export class JavaScriptParserVisitor {
849846
randomId(),
850847
this.prefix(node),
851848
Markers.EMPTY,
852-
[],
849+
this.mapDecorators(node),
853850
this.mapModifiers(node),
854851
this.mapTypeInfo(node),
855852
null,
@@ -875,7 +872,7 @@ export class JavaScriptParserVisitor {
875872
randomId(),
876873
this.prefix(node),
877874
Markers.EMPTY,
878-
[],
875+
this.mapDecorators(node),
879876
this.mapModifiers(node),
880877
this.mapTypeInfo(node),
881878
null,
@@ -899,7 +896,7 @@ export class JavaScriptParserVisitor {
899896
randomId(),
900897
this.prefix(node),
901898
Markers.EMPTY,
902-
[],
899+
this.mapDecorators(node),
903900
this.mapModifiers(node),
904901
this.mapTypeInfo(node),
905902
null,
@@ -1714,7 +1711,7 @@ export class JavaScriptParserVisitor {
17141711
const typeArguments = node.typeArguments ? this.mapTypeArguments(this.prefix(this.findChildNode(node, ts.SyntaxKind.LessThanToken)!), node.typeArguments) : null;
17151712

17161713
let select: JRightPadded<J.Expression> | null;
1717-
let name: J.Identifier = new J.Identifier( randomId(), Space.EMPTY, Markers.EMPTY, [], "", null, null);
1714+
let name: J.Identifier = new J.Identifier(randomId(), Space.EMPTY, Markers.EMPTY, [], "", null, null);
17181715

17191716
if (ts.isIdentifier(node.expression) && !node.questionDotToken) {
17201717
select = null;
@@ -1825,7 +1822,8 @@ export class JavaScriptParserVisitor {
18251822
this.prefix(node),
18261823
Markers.EMPTY,
18271824
[],
1828-
node.name ? this.visit(node.name) : null,
1825+
this.leftPadded(this.prefix(this.findChildNode(node, ts.SyntaxKind.FunctionKeyword)!), !!node.asteriskToken),
1826+
this.leftPadded(node.asteriskToken ? this.prefix(node.asteriskToken) : Space.EMPTY, node.name ? this.visit(node.name) : new J.Identifier(randomId(), Space.EMPTY, Markers.EMPTY, [], "", null, null)),
18291827
this.mapTypeParametersAsObject(node),
18301828
this.mapCommaSeparatedList(this.getParameterListNodes(node)),
18311829
this.mapTypeInfo(node),
@@ -2626,7 +2624,8 @@ export class JavaScriptParserVisitor {
26262624
this.prefix(node),
26272625
Markers.EMPTY,
26282626
this.mapModifiers(node),
2629-
node.name ? this.visit(node.name) : null,
2627+
this.leftPadded(this.prefix(this.findChildNode(node, ts.SyntaxKind.FunctionKeyword)!), !!node.asteriskToken),
2628+
this.leftPadded(node.asteriskToken ? this.prefix(node.asteriskToken) : Space.EMPTY, node.name ? this.visit(node.name) : new J.Identifier(randomId(), Space.EMPTY, Markers.EMPTY, [], "", null, null)),
26302629
this.mapTypeParametersAsObject(node),
26312630
this.mapCommaSeparatedList(this.getParameterListNodes(node)),
26322631
this.mapTypeInfo(node),
@@ -2921,23 +2920,65 @@ export class JavaScriptParserVisitor {
29212920
}
29222921

29232922
visitExportAssignment(node: ts.ExportAssignment) {
2924-
return this.visitUnknown(node);
2923+
return new JS.ExportAssignment(
2924+
randomId(),
2925+
this.prefix(node),
2926+
Markers.EMPTY,
2927+
this.mapModifiers(node),
2928+
this.leftPadded(node.isExportEquals ? this.prefix(this.findChildNode(node, ts.SyntaxKind.EqualsToken)!) : Space.EMPTY, (!!node.isExportEquals)),
2929+
this.visit(node.expression)
2930+
);
29252931
}
29262932

29272933
visitExportDeclaration(node: ts.ExportDeclaration) {
2928-
return this.visitUnknown(node);
2934+
return new JS.ExportDeclaration(
2935+
randomId(),
2936+
this.prefix(node),
2937+
Markers.EMPTY,
2938+
this.mapModifiers(node),
2939+
this.leftPadded(node.isTypeOnly ? this.prefix(this.findChildNode(node, ts.SyntaxKind.TypeKeyword)!) : Space.EMPTY, node.isTypeOnly),
2940+
node.exportClause ? this.visit(node.exportClause) : this.mapIdentifier(this.findChildNode(node, ts.SyntaxKind.AsteriskToken)!, "*"),
2941+
node.moduleSpecifier ? this.leftPadded(this.prefix(this.findChildNode(node, ts.SyntaxKind.FromKeyword)!), this.visit(node.moduleSpecifier)) : null
2942+
);
29292943
}
29302944

29312945
visitNamedExports(node: ts.NamedExports) {
2932-
return this.visitUnknown(node);
2946+
return new JS.NamedExports(
2947+
randomId(),
2948+
this.prefix(node),
2949+
Markers.EMPTY,
2950+
this.mapCommaSeparatedList(node.getChildren()),
2951+
this.mapType(node)
2952+
);
29332953
}
29342954

29352955
visitNamespaceExport(node: ts.NamespaceExport) {
2936-
return this.visitUnknown(node);
2956+
return new JS.Alias(
2957+
randomId(),
2958+
this.prefix(node),
2959+
Markers.EMPTY,
2960+
this.rightPadded(this.mapIdentifier(this.findChildNode(node, ts.SyntaxKind.AsteriskToken)!, "*"), this.prefix(this.findChildNode(node, ts.SyntaxKind.AsKeyword)!)),
2961+
this.visit(node.name)
2962+
)
29372963
}
29382964

29392965
visitExportSpecifier(node: ts.ExportSpecifier) {
2940-
return this.visitUnknown(node);
2966+
return new JS.ExportSpecifier(
2967+
randomId(),
2968+
this.prefix(node),
2969+
Markers.EMPTY,
2970+
this.leftPadded(node.isTypeOnly ? this.prefix(this.findChildNode(node, ts.SyntaxKind.TypeKeyword)!) : Space.EMPTY, node.isTypeOnly),
2971+
node.propertyName
2972+
? new JS.Alias(
2973+
randomId(),
2974+
this.prefix(node.propertyName),
2975+
Markers.EMPTY,
2976+
this.rightPadded(this.convert(node.propertyName), this.suffix(node.propertyName)),
2977+
this.convert(node.name)
2978+
)
2979+
: this.convert(node.name),
2980+
this.mapType(node)
2981+
);
29412982
}
29422983

29432984
visitMissingDeclaration(node: ts.MissingDeclaration) {
@@ -3464,7 +3505,7 @@ export class JavaScriptParserVisitor {
34643505
return args;
34653506
}
34663507

3467-
private mapDecorators(node: ts.ClassDeclaration | ts.FunctionDeclaration | ts.MethodDeclaration | ts.ConstructorDeclaration): J.Annotation[] {
3508+
private mapDecorators(node: ts.ClassDeclaration | ts.FunctionDeclaration | ts.MethodDeclaration | ts.ConstructorDeclaration | ts.ParameterDeclaration | ts.PropertyDeclaration): J.Annotation[] {
34683509
return node.modifiers?.filter(ts.isDecorator)?.map(this.convert<J.Annotation>) ?? [];
34693510
}
34703511

0 commit comments

Comments
 (0)