Skip to content

Commit

Permalink
Implement ExportNamespaceSpecifier (#70)
Browse files Browse the repository at this point in the history
* Implement ExportNamespaceSpecifier

* add two tests

* Stringifier/ExportNamedDeclaration: keep original newline layout (fixes unit test aswell)
  • Loading branch information
kungfooman authored Dec 3, 2023
1 parent 2fad064 commit 2581d35
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 11 deletions.
45 changes: 34 additions & 11 deletions src-transpiler/Stringifier.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -942,27 +942,50 @@ class Stringifier {
ExportAllDeclaration(node) {
return `export * from ${node.source.extra.raw};`;
}
/**
* @param {import("@babel/types").ExportNamespaceSpecifier} node - The Babel AST node.
* @returns {string} Stringification of the node.
*/
ExportNamespaceSpecifier(node) {
const {exported} = node;
return '* as ' + this.toSource(exported);
}
/**
* @param {import("@babel/types").ExportNamedDeclaration} node - The Babel AST node.
* @returns {string} Stringification of the node.
*/
ExportNamedDeclaration(node) {
const {declaration, source, specifiers} = node;
const {declaration, source, specifiers, loc} = node;
let out = this.spaces;
if (specifiers.length) {
if (!source) {
//debugger;
console.warn("ExportNamedDeclaration> no source given");
}
out += 'export {\n';
// todo: fix spacing system
out += this.mapToSource(specifiers).map(_ => ` ${this.spaces}${_}`).join(',\n');
out += '\n';
out += this.spaces;
out += '}';
if (source) {
out += " from " + this.toSource(source);
if (specifiers.length === 1 && specifiers[0].type === 'ExportNamespaceSpecifier') {
out += 'export ';
out += this.toSource(specifiers[0]);
out += ' from ';
out += this.toSource(source);
out += ";";
} else {
const sameLine = loc.start.line === loc.end.line;
if (sameLine) {
out += 'export {';
out += this.mapToSource(specifiers).join(', ');
out += '}';
} else {
out += 'export {\n';
// todo: fix spacing system
out += this.mapToSource(specifiers).map(_ => ` ${this.spaces}${_}`).join(',\n');
out += '\n';
out += this.spaces;
out += '}';
}
if (source) {
out += " from " + this.toSource(source);
}
out += ";\n";
}
out += ";\n";
} else if (declaration) {
out += 'export ' + this.toSource(declaration);
} else {
Expand Down
8 changes: 8 additions & 0 deletions test/typechecking.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
"input": "./test/typechecking/ClassProperty-value-is-null-input.mjs",
"output": "./test/typechecking/ClassProperty-value-is-null-output.mjs"
},
{
"input": "./test/typechecking/ExportNamespaceSpecifier-input.mjs",
"output": "./test/typechecking/ExportNamespaceSpecifier-output.mjs"
},
{
"input": "./test/typechecking/ParenthesizedExpression-multiline-input.mjs",
"output": "./test/typechecking/ParenthesizedExpression-multiline-output.mjs"
Expand All @@ -39,6 +43,10 @@
"input": "./test/typechecking/computed-ClassMethod-input.mjs",
"output": "./test/typechecking/computed-ClassMethod-output.mjs"
},
{
"input": "./test/typechecking/export-input.mjs",
"output": "./test/typechecking/export-output.mjs"
},
{
"input": "./test/typechecking/functions-input.mjs",
"output": "./test/typechecking/functions-output.mjs"
Expand Down
1 change: 1 addition & 0 deletions test/typechecking/ExportNamespaceSpecifier-input.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as rti from '@runtime-type-inspector/runtime';
1 change: 1 addition & 0 deletions test/typechecking/ExportNamespaceSpecifier-output.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as rti from '@runtime-type-inspector/runtime';
3 changes: 3 additions & 0 deletions test/typechecking/export-input.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export {default as rti1} from '@runtime-type-inspector/runtime';
export * as rti2 from '@runtime-type-inspector/runtime';
export * from '@runtime-type-inspector/runtime';
3 changes: 3 additions & 0 deletions test/typechecking/export-output.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export {default as rti1} from '@runtime-type-inspector/runtime';
export * as rti2 from '@runtime-type-inspector/runtime';
export * from '@runtime-type-inspector/runtime';

0 comments on commit 2581d35

Please sign in to comment.