Skip to content

Commit 1b9b345

Browse files
Jenny Messerlycommit-bot@chromium.org
Jenny Messerly
authored andcommitted
fix DDC ES6 module export names, part of #32272
We use renamable variables for Dart libraries, this ensures we still export it with the correct name. Change-Id: I96dc161e33d265c0ffbd07f8d642629504dffe62 Reviewed-on: https://dart-review.googlesource.com/42892 Commit-Queue: Jenny Messerly <[email protected]> Reviewed-by: Vijay Menon <[email protected]>
1 parent 69f8d4e commit 1b9b345

File tree

4 files changed

+54
-36
lines changed

4 files changed

+54
-36
lines changed

pkg/dev_compiler/lib/src/analyzer/code_generator.dart

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,9 @@ class CodeGenerator extends Object
329329
var root = new JS.Identifier('_root');
330330
items.add(js.statement('const # = Object.create(null)', [root]));
331331

332-
if (compilationUnits.any((u) => isSdkInternalRuntime(
333-
resolutionMap.elementDeclaredByCompilationUnit(u).library))) {
332+
var isBuildingSdk =
333+
compilationUnits.any((u) => isSdkInternalRuntime(u.element.library));
334+
if (isBuildingSdk) {
334335
// Don't allow these to be renamed when we're building the SDK.
335336
// There is JS code in dart:* that depends on their names.
336337
_runtimeModule = new JS.Identifier('dart');
@@ -343,28 +344,29 @@ class CodeGenerator extends Object
343344
_typeTable = new TypeTable(_runtimeModule);
344345

345346
// Initialize our library variables.
346-
var isBuildingSdk = false;
347+
var exports = <JS.NameSpecifier>[];
348+
void emitLibrary(JS.Identifier id) {
349+
items.add(js.statement('const # = Object.create(#)', [id, root]));
350+
exports.add(new JS.NameSpecifier(id));
351+
}
352+
347353
for (var unit in compilationUnits) {
348-
var library =
349-
resolutionMap.elementDeclaredByCompilationUnit(unit).library;
354+
var library = unit.element.library;
350355
if (unit.element != library.definingCompilationUnit) continue;
351356

352357
var libraryTemp = isSdkInternalRuntime(library)
353358
? _runtimeModule
354359
: new JS.TemporaryId(jsLibraryName(_libraryRoot, library));
355360
_libraries[library] = libraryTemp;
356-
items.add(new JS.ExportDeclaration(
357-
js.call('const # = Object.create(#)', [libraryTemp, root])));
358-
359-
// dart:_runtime has a magic module that holds extension method symbols.
360-
// TODO(jmesserly): find a cleaner design for this.
361-
if (isSdkInternalRuntime(library)) {
362-
isBuildingSdk = true;
363-
items.add(new JS.ExportDeclaration(js.call(
364-
'const # = Object.create(#)', [_extensionSymbolsModule, root])));
365-
}
361+
emitLibrary(libraryTemp);
366362
}
367363

364+
// dart:_runtime has a magic module that holds extension method symbols.
365+
// TODO(jmesserly): find a cleaner design for this.
366+
if (isBuildingSdk) emitLibrary(_extensionSymbolsModule);
367+
368+
items.add(new JS.ExportDeclaration(new JS.ExportClause(exports)));
369+
368370
// Collect all class/type Element -> Node mappings
369371
// in case we need to forward declare any classes.
370372
_declarationNodes = new HashMap<TypeDefiningElement, AstNode>.identity();

pkg/dev_compiler/lib/src/compiler/module_builder.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,10 @@ abstract class _ModuleBuilder {
134134

135135
visitExportDeclaration(ExportDeclaration node) {
136136
exports.add(node);
137-
statements.add(node.exported.toStatement());
137+
var exported = node.exported;
138+
if (exported is! ExportClause) {
139+
statements.add(exported.toStatement());
140+
}
138141
}
139142

140143
visitStatement(Statement node) {

pkg/dev_compiler/lib/src/js_ast/printer.dart

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,7 +1233,7 @@ class Printer extends TypeScriptTypePrinter implements NodeVisitor {
12331233
spaceOut();
12341234
}
12351235
}
1236-
nameSpecifierListOut(node.namedImports);
1236+
nameSpecifierListOut(node.namedImports, false);
12371237
fromClauseOut(node.from);
12381238
outSemicolonLn();
12391239
}
@@ -1248,15 +1248,15 @@ class Printer extends TypeScriptTypePrinter implements NodeVisitor {
12481248
}
12491249

12501250
visitExportClause(ExportClause node) {
1251-
nameSpecifierListOut(node.exports);
1251+
nameSpecifierListOut(node.exports, true);
12521252
fromClauseOut(node.from);
12531253
}
12541254

1255-
nameSpecifierListOut(List<NameSpecifier> names) {
1255+
nameSpecifierListOut(List<NameSpecifier> names, bool export) {
12561256
if (names == null) return;
12571257

12581258
if (names.length == 1 && names[0].name == '*') {
1259-
visit(names[0]);
1259+
nameSpecifierOut(names[0], export);
12601260
return;
12611261
}
12621262

@@ -1267,7 +1267,7 @@ class Printer extends TypeScriptTypePrinter implements NodeVisitor {
12671267
out(',');
12681268
spaceOut();
12691269
}
1270-
visit(names[i]);
1270+
nameSpecifierOut(names[i], export);
12711271
}
12721272
spaceOut();
12731273
out('}');
@@ -1281,22 +1281,28 @@ class Printer extends TypeScriptTypePrinter implements NodeVisitor {
12811281
}
12821282
}
12831283

1284+
/// This is unused, see [nameSpecifierOut].
12841285
visitNameSpecifier(NameSpecifier node) {
1286+
throw new UnsupportedError('visitNameSpecifier');
1287+
}
1288+
1289+
nameSpecifierOut(NameSpecifier node, bool export) {
12851290
if (node.isStar) {
12861291
out('*');
12871292
} else {
1288-
var importName = node.name.name;
1289-
out(importName);
1290-
1293+
var name = node.name.name;
12911294
if (node.asName == null) {
12921295
// If our local was renamed, generate an implicit "as".
1293-
// This is a convenience feature so imports can be renamed.
1296+
// This is a convenience feature so imports and exports can be renamed.
12941297
var localName = localNamer.getName(node.name);
1295-
if (localName != importName) {
1298+
if (localName != name) {
1299+
out(export ? localName : name);
12961300
out(' as ');
1297-
out(localName);
1301+
out(export ? name : localName);
1302+
return;
12981303
}
12991304
}
1305+
out(name);
13001306
}
13011307
if (node.asName != null) {
13021308
out(' as ');

pkg/dev_compiler/lib/src/kernel/compiler.dart

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -280,22 +280,29 @@ class ProgramCompiler
280280

281281
// Initialize our library variables.
282282
var items = <JS.ModuleItem>[];
283+
var exports = <JS.NameSpecifier>[];
284+
var root = new JS.Identifier('_root');
285+
items.add(js.statement('const # = Object.create(null)', [root]));
286+
287+
void emitLibrary(JS.Identifier id) {
288+
items.add(js.statement('const # = Object.create(#)', [id, root]));
289+
exports.add(new JS.NameSpecifier(id));
290+
}
291+
283292
for (var library in libraries) {
284293
var libraryTemp = library == ddcRuntime
285294
? _runtimeModule
286295
: new JS.TemporaryId(jsLibraryName(library));
287296
_libraries[library] = libraryTemp;
288-
items.add(new JS.ExportDeclaration(
289-
js.call('const # = Object.create(null)', [libraryTemp])));
290-
291-
// dart:_runtime has a magic module that holds extension method symbols.
292-
// TODO(jmesserly): find a cleaner design for this.
293-
if (library == ddcRuntime) {
294-
items.add(new JS.ExportDeclaration(js
295-
.call('const # = Object.create(null)', [_extensionSymbolsModule])));
296-
}
297+
emitLibrary(libraryTemp);
297298
}
298299

300+
// dart:_runtime has a magic module that holds extension method symbols.
301+
// TODO(jmesserly): find a cleaner design for this.
302+
if (ddcRuntime != null) emitLibrary(_extensionSymbolsModule);
303+
304+
items.add(new JS.ExportDeclaration(new JS.ExportClause(exports)));
305+
299306
// Collect all class/type Element -> Node mappings
300307
// in case we need to forward declare any classes.
301308
_pendingClasses = new HashSet.identity();

0 commit comments

Comments
 (0)