From f2f6bbb02c0041e6763a3435a1f0b0992922187f Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 21 May 2015 01:03:23 +0100 Subject: [PATCH] clean up explosion of module declarations, remove and inherit comments when taking off the declaration - fixes #1583 --- .../transformers/internal/modules.js | 40 +++++++++++-------- .../es6.modules/comments-explosion/actual.js | 4 ++ .../comments-explosion/expected.js | 8 ++++ .../comments-explosion/options.json | 4 ++ 4 files changed, 39 insertions(+), 17 deletions(-) create mode 100644 test/core/fixtures/transformation/es6.modules/comments-explosion/actual.js create mode 100644 test/core/fixtures/transformation/es6.modules/comments-explosion/expected.js create mode 100644 test/core/fixtures/transformation/es6.modules/comments-explosion/options.json diff --git a/src/babel/transformation/transformers/internal/modules.js b/src/babel/transformation/transformers/internal/modules.js index cb741bdc39..fca2340d86 100644 --- a/src/babel/transformation/transformers/internal/modules.js +++ b/src/babel/transformation/transformers/internal/modules.js @@ -7,6 +7,14 @@ import clone from "lodash/lang/clone"; import * as t from "../../../types"; +function getDeclar(node) { + var declar = node.declaration; + t.inheritsComments(declar, node); + t.removeComments(node); + declar._ignoreUserWhitespace = true; + return declar; +} + export var metadata = { group: "builtin-setup" }; @@ -24,28 +32,28 @@ export function ExportDefaultDeclaration(node, parent, scope) { var declar = node.declaration; - var getDeclar = function () { - declar._ignoreUserWhitespace = true; - return declar; - }; - if (t.isClassDeclaration(declar)) { // export default class Foo {}; + var nodes = [getDeclar(node), node]; node.declaration = declar.id; - return [getDeclar(), node]; + return nodes; } else if (t.isClassExpression(declar)) { // export default class {}; var temp = scope.generateUidIdentifier("default"); - declar = t.variableDeclaration("var", [ + node.declaration = t.variableDeclaration("var", [ t.variableDeclarator(temp, declar) ]); + + var nodes = [getDeclar(node), node]; node.declaration = temp; - return [getDeclar(), node]; + return nodes; } else if (t.isFunctionDeclaration(declar)) { // export default function Foo() {} node._blockHoist = 2; + + var nodes = [getDeclar(node), node]; node.declaration = declar.id; - return [getDeclar(), node]; + return nodes; } } @@ -58,22 +66,20 @@ export function ExportNamedDeclaration(node, parent, scope) { var declar = node.declaration; - var getDeclar = function () { - declar._ignoreUserWhitespace = true; - return declar; - }; - if (t.isClassDeclaration(declar)) { // export class Foo {} node.specifiers = [buildExportSpecifier(declar.id)]; + var nodes = [getDeclar(node), node]; node.declaration = null; - return [getDeclar(), node]; + return nodes; } else if (t.isFunctionDeclaration(declar)) { // export function Foo() {} node.specifiers = [buildExportSpecifier(declar.id)]; - node.declaration = null; node._blockHoist = 2; - return [getDeclar(), node]; + + var nodes = [getDeclar(node), node]; + node.declaration = null; + return nodes; } else if (t.isVariableDeclaration(declar)) { // export var foo = "bar"; var specifiers = []; diff --git a/test/core/fixtures/transformation/es6.modules/comments-explosion/actual.js b/test/core/fixtures/transformation/es6.modules/comments-explosion/actual.js new file mode 100644 index 0000000000..6affde40ac --- /dev/null +++ b/test/core/fixtures/transformation/es6.modules/comments-explosion/actual.js @@ -0,0 +1,4 @@ +bar(); + +// bar +export function foo() {} diff --git a/test/core/fixtures/transformation/es6.modules/comments-explosion/expected.js b/test/core/fixtures/transformation/es6.modules/comments-explosion/expected.js new file mode 100644 index 0000000000..966456dcd2 --- /dev/null +++ b/test/core/fixtures/transformation/es6.modules/comments-explosion/expected.js @@ -0,0 +1,8 @@ +"use strict"; + +export { foo }; +bar(); + +// bar + +function foo() {} diff --git a/test/core/fixtures/transformation/es6.modules/comments-explosion/options.json b/test/core/fixtures/transformation/es6.modules/comments-explosion/options.json new file mode 100644 index 0000000000..3c07e6da8c --- /dev/null +++ b/test/core/fixtures/transformation/es6.modules/comments-explosion/options.json @@ -0,0 +1,4 @@ +{ + "blacklist": ["es6.modules"], + "noCheckAst": true +}