Skip to content

Commit c044040

Browse files
fix: add support for export { default } from './source'
See decaffeinate/decaffeinate#1367
1 parent 03c45a8 commit c044040

File tree

3 files changed

+152
-6
lines changed

3 files changed

+152
-6
lines changed

src/mappers/mapModuleDeclaration.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import {
22
ExportAllDeclaration as CoffeeExportAllDeclaration,
33
ExportDefaultDeclaration as CoffeeExportDefaultDeclaration,
44
ExportNamedDeclaration as CoffeeExportNamedDeclaration, ExportSpecifierList,
5+
IdentifierLiteral,
56
ImportDeclaration as CoffeeImportDeclaration,
67
ImportNamespaceSpecifier,
78
ImportSpecifierList,
89
Literal,
9-
ModuleDeclaration,
10-
ModuleSpecifier as CoffeeModuleSpecifier, StringLiteral,
10+
ModuleDeclaration, ModuleSpecifier as CoffeeModuleSpecifier, StringLiteral,
1111
} from 'decaffeinate-coffeescript2/lib/coffeescript/nodes';
1212
import {
1313
ExportAllDeclaration,
@@ -104,9 +104,12 @@ function mapSpecifier(context: ParseContext, specifier: CoffeeModuleSpecifier):
104104
}
105105

106106
function mapLiteralToIdentifier(context: ParseContext, literal: Literal): Identifier {
107-
let identifier = mapLiteral(context, literal);
108-
if (!(identifier instanceof Identifier)) {
109-
throw new Error('Expected identifier in declaration.');
107+
if (literal instanceof IdentifierLiteral) {
108+
return mapLiteral(context, literal) as Identifier;
109+
} else if (literal.constructor === Literal) {
110+
let { line, column, start, end, raw } = getLocation(context, literal);
111+
return new Identifier(line, column, start, end, raw, literal.value);
112+
} else {
113+
throw new Error('Expected identifier in module declaration.');
110114
}
111-
return identifier;
112115
}

test/__snapshots__/examples.test.ts.snap

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7838,6 +7838,77 @@ Program {
78387838
}
78397839
`;
78407840

7841+
exports[`CS1: export-default-from-source 1`] = `
7842+
Program {
7843+
"body": Block {
7844+
"column": 1,
7845+
"end": 35,
7846+
"inline": false,
7847+
"line": 1,
7848+
"raw": "export { default } from './source';",
7849+
"start": 0,
7850+
"statements": Array [
7851+
ExportBindingsDeclaration {
7852+
"column": 1,
7853+
"end": 34,
7854+
"line": 1,
7855+
"namedExports": Array [
7856+
ModuleSpecifier {
7857+
"alias": null,
7858+
"column": 10,
7859+
"end": 16,
7860+
"line": 1,
7861+
"original": Identifier {
7862+
"column": 10,
7863+
"data": "default",
7864+
"end": 16,
7865+
"line": 1,
7866+
"raw": "default",
7867+
"start": 9,
7868+
"type": "Identifier",
7869+
},
7870+
"raw": "default",
7871+
"start": 9,
7872+
"type": "ModuleSpecifier",
7873+
},
7874+
],
7875+
"raw": "export { default } from './source'",
7876+
"source": String {
7877+
"column": 25,
7878+
"end": 34,
7879+
"expressions": Array [],
7880+
"line": 1,
7881+
"quasis": Array [
7882+
Quasi {
7883+
"column": 26,
7884+
"data": "./source",
7885+
"end": 33,
7886+
"line": 1,
7887+
"raw": "./source",
7888+
"start": 25,
7889+
"type": "Quasi",
7890+
},
7891+
],
7892+
"raw": "'./source'",
7893+
"start": 24,
7894+
"type": "String",
7895+
},
7896+
"start": 0,
7897+
"type": "ExportBindingsDeclaration",
7898+
},
7899+
],
7900+
"type": "Block",
7901+
},
7902+
"column": 1,
7903+
"end": 36,
7904+
"line": 1,
7905+
"raw": "export { default } from './source';
7906+
",
7907+
"start": 0,
7908+
"type": "Program",
7909+
}
7910+
`;
7911+
78417912
exports[`CS1: export-multiple-bindings 1`] = `
78427913
Program {
78437914
"body": Block {
@@ -28696,6 +28767,77 @@ Program {
2869628767
}
2869728768
`;
2869828769

28770+
exports[`CS2: export-default-from-source 1`] = `
28771+
Program {
28772+
"body": Block {
28773+
"column": 1,
28774+
"end": 35,
28775+
"inline": false,
28776+
"line": 1,
28777+
"raw": "export { default } from './source';",
28778+
"start": 0,
28779+
"statements": Array [
28780+
ExportBindingsDeclaration {
28781+
"column": 1,
28782+
"end": 34,
28783+
"line": 1,
28784+
"namedExports": Array [
28785+
ModuleSpecifier {
28786+
"alias": null,
28787+
"column": 10,
28788+
"end": 16,
28789+
"line": 1,
28790+
"original": Identifier {
28791+
"column": 10,
28792+
"data": "default",
28793+
"end": 16,
28794+
"line": 1,
28795+
"raw": "default",
28796+
"start": 9,
28797+
"type": "Identifier",
28798+
},
28799+
"raw": "default",
28800+
"start": 9,
28801+
"type": "ModuleSpecifier",
28802+
},
28803+
],
28804+
"raw": "export { default } from './source'",
28805+
"source": String {
28806+
"column": 25,
28807+
"end": 34,
28808+
"expressions": Array [],
28809+
"line": 1,
28810+
"quasis": Array [
28811+
Quasi {
28812+
"column": 26,
28813+
"data": "./source",
28814+
"end": 33,
28815+
"line": 1,
28816+
"raw": "./source",
28817+
"start": 25,
28818+
"type": "Quasi",
28819+
},
28820+
],
28821+
"raw": "'./source'",
28822+
"start": 24,
28823+
"type": "String",
28824+
},
28825+
"start": 0,
28826+
"type": "ExportBindingsDeclaration",
28827+
},
28828+
],
28829+
"type": "Block",
28830+
},
28831+
"column": 1,
28832+
"end": 36,
28833+
"line": 1,
28834+
"raw": "export { default } from './source';
28835+
",
28836+
"start": 0,
28837+
"type": "Program",
28838+
}
28839+
`;
28840+
2869928841
exports[`CS2: export-multiple-bindings 1`] = `
2870028842
Program {
2870128843
"body": Block {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from './source';

0 commit comments

Comments
 (0)