Handles arbitrary module identifiers

This commit enables support for arbitrary names for identifier and namespace imports and exports
This commit is contained in:
Sid Shankar
2024-09-11 00:27:42 +00:00
parent 2c74b5ee50
commit aa787a9cb1

View File

@@ -1203,11 +1203,13 @@ public class TypeScriptASTConverter {
Literal source = tryConvertChild(node, "moduleSpecifier", Literal.class);
Expression attributes = convertChild(node, "attributes");
if (hasChild(node, "exportClause")) {
JsonElement exportClauseNode = node.get("exportClause");
boolean hasTypeKeyword = node.get("isTypeOnly").getAsBoolean();
boolean isNamespaceExportNode = hasKind(exportClauseNode, "NamespaceExport");
List<ExportSpecifier> specifiers =
hasKind(node.get("exportClause"), "NamespaceExport")
isNamespaceExportNode
? Collections.singletonList(convertChild(node, "exportClause"))
: convertChildren(node.get("exportClause").getAsJsonObject(), "elements");
: convertChildren(exportClauseNode.getAsJsonObject(), "elements");
return new ExportNamedDeclaration(loc, null, specifiers, source, attributes, hasTypeKeyword);
} else {
return new ExportAllDeclaration(loc, source, attributes);
@@ -1215,15 +1217,21 @@ public class TypeScriptASTConverter {
}
private Node convertExportSpecifier(JsonObject node, SourceLocation loc) throws ParseError {
Identifier local = convertChild(node, hasChild(node, "propertyName") ? "propertyName" : "name");
JsonObject exportedToken = node.get("name").getAsJsonObject();
Identifier exported = convertNodeAsIdentifier(exportedToken);
return new ExportSpecifier(
loc,
convertChild(node, hasChild(node, "propertyName") ? "propertyName" : "name"),
convertChild(node, "name"));
local,
exported);
}
private Node convertNamespaceExport(JsonObject node, SourceLocation loc) throws ParseError {
// Convert the "* as ns" from an export declaration.
return new ExportNamespaceSpecifier(loc, convertChild(node, "name"));
JsonObject exportedNamespaceToken = node.get("name").getAsJsonObject();
Identifier exportedNamespaceIdentifier = convertNodeAsIdentifier(exportedNamespaceToken);
return new ExportNamespaceSpecifier(loc, exportedNamespaceIdentifier);
}
private Node convertExpressionStatement(JsonObject node, SourceLocation loc) throws ParseError {
@@ -1431,7 +1439,8 @@ public class TypeScriptASTConverter {
private Node convertImportSpecifier(JsonObject node, SourceLocation loc) throws ParseError {
boolean hasImported = hasChild(node, "propertyName");
Identifier imported = convertChild(node, hasImported ? "propertyName" : "name");
JsonObject importedToken = node.get(hasImported? "propertyName" : "name").getAsJsonObject();
Identifier imported = convertNodeAsIdentifier(importedToken);
Identifier local = convertChild(node, "name");
boolean isTypeOnly = node.get("isTypeOnly").getAsBoolean() == true;
return new ImportSpecifier(loc, imported, local, isTypeOnly);