From 1c3445a95dceaa444d4e4ba78b7e71133b71b538 Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Wed, 25 Apr 2018 14:29:38 -0700 Subject: [PATCH 1/7] Backport #7378 for 6.x --- .../src/index.js | 19 +- .../auxiliary-comment/overview/expected.js | 6 +- .../misc/reference-source-map/actual.js | 14 ++ .../misc/reference-source-map/expected.js | 24 +++ .../misc/reference-source-map/options.json | 3 + .../reference-source-map/source-mappings.json | 162 ++++++++++++++++++ .../fixtures/strict/import/source-map.json | 16 ++ 7 files changed, 235 insertions(+), 9 deletions(-) create mode 100644 packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/misc/reference-source-map/actual.js create mode 100644 packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/misc/reference-source-map/expected.js create mode 100644 packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/misc/reference-source-map/options.json create mode 100644 packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/misc/reference-source-map/source-mappings.json create mode 100644 packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/import/source-map.json diff --git a/packages/babel-plugin-transform-es2015-modules-commonjs/src/index.js b/packages/babel-plugin-transform-es2015-modules-commonjs/src/index.js index 697ab5e8d35c..01c09e830b43 100644 --- a/packages/babel-plugin-transform-es2015-modules-commonjs/src/index.js +++ b/packages/babel-plugin-transform-es2015-modules-commonjs/src/index.js @@ -58,14 +58,19 @@ export default function () { // redeclared in this scope if (this.scope.getBinding(name) !== path.scope.getBinding(name)) return; + const replacement = t.cloneDeep(remap); + + // Preserve the binding location so that sourcemaps are nicer. + replacement.loc = path.node.loc; + if (path.parentPath.isCallExpression({ callee: path.node })) { - path.replaceWith(t.sequenceExpression([t.numericLiteral(0), remap])); - } else if (path.isJSXIdentifier() && t.isMemberExpression(remap)) { - const { object, property } = remap; + path.replaceWith(t.sequenceExpression([t.numericLiteral(0), replacement])); + } else if (path.isJSXIdentifier() && t.isMemberExpression(replacement)) { + const { object, property } = replacement; path.replaceWith(t.JSXMemberExpression(t.JSXIdentifier(object.name), t.JSXIdentifier(property.name))); } else { - path.replaceWith(remap); + path.replaceWith(replacement); } this.requeueInParent(path); }, @@ -481,8 +486,10 @@ export default function () { topNodes.push(varDecl); } } - remaps[specifier.local.name] = t.memberExpression(target, - t.cloneWithoutLoc(specifier.imported)); + remaps[specifier.local.name] = t.memberExpression( + t.cloneWithoutLoc(target), + t.cloneWithoutLoc(specifier.imported) + ); } } } else { diff --git a/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/auxiliary-comment/overview/expected.js b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/auxiliary-comment/overview/expected.js index af03c31c89da..5f556cd074b7 100644 --- a/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/auxiliary-comment/overview/expected.js +++ b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/auxiliary-comment/overview/expected.js @@ -26,8 +26,8 @@ var /*before*/_foo5 = require("foo5") /*after*/; /*before*/exports. /*after*/test = test; var test2 = /*before*/exports. /*after*/test2 = 5; -/*before*/(0, _foo4.bar) /*after*/( /*before*/_foo2.default /*after*/, /*before*/_foo5.foo /*after*/); +/*before*/(0, /*after*/ /*before*/_foo4 /*after*/. /*before*/bar) /*after*/( /*before*/_foo2 /*after*/. /*before*/default /*after*/, /*before*/_foo5 /*after*/. /*before*/foo /*after*/); /* my comment */ -/*before*/_foo5.foo /*after*/; -/*before*/_foo2.default /*after*/; +/*before*/_foo5 /*after*/. /*before*/foo /*after*/; +/*before*/_foo2 /*after*/. /*before*/default /*after*/; \ No newline at end of file diff --git a/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/misc/reference-source-map/actual.js b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/misc/reference-source-map/actual.js new file mode 100644 index 000000000000..b85fcb659efb --- /dev/null +++ b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/misc/reference-source-map/actual.js @@ -0,0 +1,14 @@ +import aDefault from "one"; +import { aNamed } from "two"; +import { orig as anAliased } from "three"; +import * as aNamespace from "four"; + +console.log(aDefault); +console.log(aNamed); +console.log(anAliased); +console.log(aNamespace); + +console.log(aDefault()); +console.log(aNamed()); +console.log(anAliased()); +console.log(aNamespace()); diff --git a/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/misc/reference-source-map/expected.js b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/misc/reference-source-map/expected.js new file mode 100644 index 000000000000..256e8caf02b6 --- /dev/null +++ b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/misc/reference-source-map/expected.js @@ -0,0 +1,24 @@ +"use strict"; + +var _one = require("one"); + +var _one2 = babelHelpers.interopRequireDefault(_one); + +var _two = require("two"); + +var _three = require("three"); + +var _four = require("four"); + +var aNamespace = babelHelpers.interopRequireWildcard(_four); + + +console.log(_one2.default); +console.log(_two.aNamed); +console.log(_three.orig); +console.log(aNamespace); + +console.log((0, _one2.default)()); +console.log((0, _two.aNamed)()); +console.log((0, _three.orig)()); +console.log(aNamespace()); diff --git a/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/misc/reference-source-map/options.json b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/misc/reference-source-map/options.json new file mode 100644 index 000000000000..7ccaf471d138 --- /dev/null +++ b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/misc/reference-source-map/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["external-helpers", "transform-es2015-modules-commonjs"] +} diff --git a/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/misc/reference-source-map/source-mappings.json b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/misc/reference-source-map/source-mappings.json new file mode 100644 index 000000000000..d9626f401fd3 --- /dev/null +++ b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/misc/reference-source-map/source-mappings.json @@ -0,0 +1,162 @@ +[ + { + "generated": { + "line": 16, + "column": 12 + }, + "original": { + "line": 6, + "column": 12 + } + }, + { + "generated": { + "line": 16, + "column": 23 + }, + "original": { + "line": 6, + "column": 12 + } + }, + { + "generated": { + "line": 17, + "column": 12 + }, + "original": { + "line": 7, + "column": 12 + } + }, + { + "generated": { + "line": 17, + "column": 22 + }, + "original": { + "line": 7, + "column": 12 + } + }, + { + "generated": { + "line": 18, + "column": 12 + }, + "original": { + "line": 8, + "column": 12 + } + }, + { + "generated": { + "line": 18, + "column": 22 + }, + "original": { + "line": 8, + "column": 12 + } + }, + { + "generated": { + "line": 19, + "column": 12 + }, + "original": { + "line": 9, + "column": 12 + } + }, + { + "generated": { + "line": 19, + "column": 21 + }, + "original": { + "line": 9, + "column": 12 + } + }, + { + "generated": { + "line": 21, + "column": 16 + }, + "original": { + "line": 11, + "column": 12 + } + }, + { + "generated": { + "line": 21, + "column": 27 + }, + "original": { + "line": 11, + "column": 12 + } + }, + { + "generated": { + "line": 22, + "column": 16 + }, + "original": { + "line": 12, + "column": 12 + } + }, + { + "generated": { + "line": 22, + "column": 26 + }, + "original": { + "line": 12, + "column": 12 + } + }, + { + "generated": { + "line": 23, + "column": 16 + }, + "original": { + "line": 13, + "column": 12 + } + }, + { + "generated": { + "line": 23, + "column": 26 + }, + "original": { + "line": 13, + "column": 12 + } + }, + { + "generated": { + "line": 24, + "column": 12 + }, + "original": { + "line": 14, + "column": 12 + } + }, + { + "generated": { + "line": 24, + "column": 21 + }, + "original": { + "line": 14, + "column": 12 + } + } +] diff --git a/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/import/source-map.json b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/import/source-map.json new file mode 100644 index 000000000000..ca0b31faab70 --- /dev/null +++ b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/import/source-map.json @@ -0,0 +1,16 @@ +{ + "file": "strict/import/expected.js", + "mappings": ";;AAAA;;AAKAA;AACAC;AACAC;AACA", + "names": [ + "foo", + "foo2", + "foo3" + ], + "sources": [ + "strict/import/actual.js" + ], + "sourcesContent": [ + "import foo from \"foo\";\nimport { default as foo2 } from \"foo\";\nimport { foo3 } from \"foo\";\nimport * as foo4 from \"foo\";\n\nfoo;\nfoo2;\nfoo3;\nfoo3();" + ], + "version": 3 +} From 7360a30385b236143b918b1903e3bc42657c1bcb Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Wed, 25 Apr 2018 15:00:03 -0700 Subject: [PATCH 2/7] Backport #7312 for 6.x --- .../internal-plugins/shadow-functions.js | 38 ++++++++++--------- .../arguments-source-maps/actual.js | 5 +++ .../arguments-source-maps/expected.js | 7 ++++ .../arguments-source-maps/options.json | 3 ++ .../arguments-source-maps/source-map.json | 18 +++++++++ .../source-mappings.json | 8 ++++ .../this-source-maps/actual.js | 5 +++ .../this-source-maps/expected.js | 7 ++++ .../this-source-maps/options.json | 3 ++ .../this-source-maps/source-map.json | 17 +++++++++ .../this-source-maps/source-mappings.json | 8 ++++ 11 files changed, 102 insertions(+), 17 deletions(-) create mode 100644 packages/babel-plugin-transform-es2015-arrow-functions/test/fixtures/arrow-functions/arguments-source-maps/actual.js create mode 100644 packages/babel-plugin-transform-es2015-arrow-functions/test/fixtures/arrow-functions/arguments-source-maps/expected.js create mode 100644 packages/babel-plugin-transform-es2015-arrow-functions/test/fixtures/arrow-functions/arguments-source-maps/options.json create mode 100644 packages/babel-plugin-transform-es2015-arrow-functions/test/fixtures/arrow-functions/arguments-source-maps/source-map.json create mode 100644 packages/babel-plugin-transform-es2015-arrow-functions/test/fixtures/arrow-functions/arguments-source-maps/source-mappings.json create mode 100644 packages/babel-plugin-transform-es2015-arrow-functions/test/fixtures/arrow-functions/this-source-maps/actual.js create mode 100644 packages/babel-plugin-transform-es2015-arrow-functions/test/fixtures/arrow-functions/this-source-maps/expected.js create mode 100644 packages/babel-plugin-transform-es2015-arrow-functions/test/fixtures/arrow-functions/this-source-maps/options.json create mode 100644 packages/babel-plugin-transform-es2015-arrow-functions/test/fixtures/arrow-functions/this-source-maps/source-map.json create mode 100644 packages/babel-plugin-transform-es2015-arrow-functions/test/fixtures/arrow-functions/this-source-maps/source-mappings.json diff --git a/packages/babel-core/src/transformation/internal-plugins/shadow-functions.js b/packages/babel-core/src/transformation/internal-plugins/shadow-functions.js index 1b50aa759c74..514b2e2ee6e8 100644 --- a/packages/babel-core/src/transformation/internal-plugins/shadow-functions.js +++ b/packages/babel-core/src/transformation/internal-plugins/shadow-functions.js @@ -91,29 +91,33 @@ function remap(path, key) { // binding since arrow function syntax already does that. if (!passedShadowFunction) return; - const cached = fnPath.getData(key); - if (cached) return path.replaceWith(cached); + let cached = fnPath.getData(key); + if (!cached) { + const id = path.scope.generateUidIdentifier(key); - const id = path.scope.generateUidIdentifier(key); + fnPath.setData(key, id); + cached = id; - fnPath.setData(key, id); + const classPath = fnPath.findParent((p) => p.isClass()); + const hasSuperClass = !!(classPath && classPath.node && classPath.node.superClass); - const classPath = fnPath.findParent((p) => p.isClass()); - const hasSuperClass = !!(classPath && classPath.node && classPath.node.superClass); + if (key === "this" && fnPath.isMethod({ kind: "constructor" }) && hasSuperClass) { + fnPath.scope.push({ id }); - if (key === "this" && fnPath.isMethod({ kind: "constructor" }) && hasSuperClass) { - fnPath.scope.push({ id }); + fnPath.traverse(superVisitor, { id }); + } else { + const init = key === "this" ? t.thisExpression() : t.identifier(key); - fnPath.traverse(superVisitor, { id }); - } else { - const init = key === "this" ? t.thisExpression() : t.identifier(key); - - // Forward the shadowed function, so that the identifiers do not get hoisted - // up to the first non shadow function but rather up to the bound shadow function - if (shadowFunction) init._shadowedFunctionLiteral = shadowFunction; + // Forward the shadowed function, so that the identifiers do not get hoisted + // up to the first non shadow function but rather up to the bound shadow function + if (shadowFunction) init._shadowedFunctionLiteral = shadowFunction; - fnPath.scope.push({ id, init }); + fnPath.scope.push({ id, init }); + } } - return path.replaceWith(id); + const node = t.cloneDeep(cached); + node.loc = path.node.loc; + + return path.replaceWith(node); } diff --git a/packages/babel-plugin-transform-es2015-arrow-functions/test/fixtures/arrow-functions/arguments-source-maps/actual.js b/packages/babel-plugin-transform-es2015-arrow-functions/test/fixtures/arrow-functions/arguments-source-maps/actual.js new file mode 100644 index 000000000000..94690c22185e --- /dev/null +++ b/packages/babel-plugin-transform-es2015-arrow-functions/test/fixtures/arrow-functions/arguments-source-maps/actual.js @@ -0,0 +1,5 @@ +function fn() { + var inner = () => { + console.log(arguments); + }; +} diff --git a/packages/babel-plugin-transform-es2015-arrow-functions/test/fixtures/arrow-functions/arguments-source-maps/expected.js b/packages/babel-plugin-transform-es2015-arrow-functions/test/fixtures/arrow-functions/arguments-source-maps/expected.js new file mode 100644 index 000000000000..4817823f3504 --- /dev/null +++ b/packages/babel-plugin-transform-es2015-arrow-functions/test/fixtures/arrow-functions/arguments-source-maps/expected.js @@ -0,0 +1,7 @@ +function fn() { + var _arguments = arguments; + + var inner = function () { + console.log(_arguments); + }; +} diff --git a/packages/babel-plugin-transform-es2015-arrow-functions/test/fixtures/arrow-functions/arguments-source-maps/options.json b/packages/babel-plugin-transform-es2015-arrow-functions/test/fixtures/arrow-functions/arguments-source-maps/options.json new file mode 100644 index 000000000000..fc3d74226316 --- /dev/null +++ b/packages/babel-plugin-transform-es2015-arrow-functions/test/fixtures/arrow-functions/arguments-source-maps/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["transform-es2015-arrow-functions"] +} diff --git a/packages/babel-plugin-transform-es2015-arrow-functions/test/fixtures/arrow-functions/arguments-source-maps/source-map.json b/packages/babel-plugin-transform-es2015-arrow-functions/test/fixtures/arrow-functions/arguments-source-maps/source-map.json new file mode 100644 index 000000000000..9046b82b800b --- /dev/null +++ b/packages/babel-plugin-transform-es2015-arrow-functions/test/fixtures/arrow-functions/arguments-source-maps/source-map.json @@ -0,0 +1,18 @@ +{ + "file": "arrow-functions/arguments-source-maps/expected.js", + "mappings": "AAAA,SAASA,EAAT,GAAc;AAAA;;AACZ,MAAIC,QAAQ,YAAM;AAChBC,YAAQC,GAAR,CAAYC,UAAZ;AACD,GAFD;AAGD", + "names": [ + "fn", + "inner", + "console", + "log", + "arguments" + ], + "sources": [ + "arrow-functions/arguments-source-maps/actual.js" + ], + "sourcesContent": [ + "function fn() {\n var inner = () => {\n console.log(arguments);\n };\n}" + ], + "version": 3 +} diff --git a/packages/babel-plugin-transform-es2015-arrow-functions/test/fixtures/arrow-functions/arguments-source-maps/source-mappings.json b/packages/babel-plugin-transform-es2015-arrow-functions/test/fixtures/arrow-functions/arguments-source-maps/source-mappings.json new file mode 100644 index 000000000000..8126503ee67b --- /dev/null +++ b/packages/babel-plugin-transform-es2015-arrow-functions/test/fixtures/arrow-functions/arguments-source-maps/source-mappings.json @@ -0,0 +1,8 @@ +[{ + "generated": { + "line": 5, "column": 16 + }, + "original": { + "line": 3, "column": 16 + } +}] diff --git a/packages/babel-plugin-transform-es2015-arrow-functions/test/fixtures/arrow-functions/this-source-maps/actual.js b/packages/babel-plugin-transform-es2015-arrow-functions/test/fixtures/arrow-functions/this-source-maps/actual.js new file mode 100644 index 000000000000..82a771edc598 --- /dev/null +++ b/packages/babel-plugin-transform-es2015-arrow-functions/test/fixtures/arrow-functions/this-source-maps/actual.js @@ -0,0 +1,5 @@ +function fn() { + var inner = () => { + console.log(this); + }; +} diff --git a/packages/babel-plugin-transform-es2015-arrow-functions/test/fixtures/arrow-functions/this-source-maps/expected.js b/packages/babel-plugin-transform-es2015-arrow-functions/test/fixtures/arrow-functions/this-source-maps/expected.js new file mode 100644 index 000000000000..327df642d041 --- /dev/null +++ b/packages/babel-plugin-transform-es2015-arrow-functions/test/fixtures/arrow-functions/this-source-maps/expected.js @@ -0,0 +1,7 @@ +function fn() { + var _this = this; + + var inner = function () { + console.log(_this); + }; +} diff --git a/packages/babel-plugin-transform-es2015-arrow-functions/test/fixtures/arrow-functions/this-source-maps/options.json b/packages/babel-plugin-transform-es2015-arrow-functions/test/fixtures/arrow-functions/this-source-maps/options.json new file mode 100644 index 000000000000..fc3d74226316 --- /dev/null +++ b/packages/babel-plugin-transform-es2015-arrow-functions/test/fixtures/arrow-functions/this-source-maps/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["transform-es2015-arrow-functions"] +} diff --git a/packages/babel-plugin-transform-es2015-arrow-functions/test/fixtures/arrow-functions/this-source-maps/source-map.json b/packages/babel-plugin-transform-es2015-arrow-functions/test/fixtures/arrow-functions/this-source-maps/source-map.json new file mode 100644 index 000000000000..2eb2845e49cf --- /dev/null +++ b/packages/babel-plugin-transform-es2015-arrow-functions/test/fixtures/arrow-functions/this-source-maps/source-map.json @@ -0,0 +1,17 @@ +{ + "file": "arrow-functions/this-source-maps/expected.js", + "mappings": "AAAA,SAASA,EAAT,GAAc;AAAA;;AACZ,MAAIC,QAAQ,YAAM;AAChBC,YAAQC,GAAR,CAAY,KAAZ;AACD,GAFD;AAGD", + "names": [ + "fn", + "inner", + "console", + "log" + ], + "sources": [ + "arrow-functions/this-source-maps/actual.js" + ], + "sourcesContent": [ + "function fn() {\n var inner = () => {\n console.log(this);\n };\n}" + ], + "version": 3 +} diff --git a/packages/babel-plugin-transform-es2015-arrow-functions/test/fixtures/arrow-functions/this-source-maps/source-mappings.json b/packages/babel-plugin-transform-es2015-arrow-functions/test/fixtures/arrow-functions/this-source-maps/source-mappings.json new file mode 100644 index 000000000000..8126503ee67b --- /dev/null +++ b/packages/babel-plugin-transform-es2015-arrow-functions/test/fixtures/arrow-functions/this-source-maps/source-mappings.json @@ -0,0 +1,8 @@ +[{ + "generated": { + "line": 5, "column": 16 + }, + "original": { + "line": 3, "column": 16 + } +}] From 80f433a18712465505164a8254bb49ba31f68c35 Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Wed, 25 Apr 2018 15:22:53 -0700 Subject: [PATCH 3/7] Backport #7761 for 6.x --- .../src/transformation/file/index.js | 40 +-- .../src/transformation/file/merge-map.js | 316 ++++++++++++++++++ .../input-source-map-complex/actual.js | 113 +++++++ .../input-source-map-complex/expected.js | 118 +++++++ .../input-source-map.json | 12 + .../input-source-map-complex/options.json | 5 + .../input-source-map-complex/source-map.json | 12 + .../source-mappings.json | 72 ++++ .../input-source-map/source-map.json | 2 +- packages/babel-helper-fixtures/src/index.js | 5 + .../src/index.js | 1 + 11 files changed, 658 insertions(+), 38 deletions(-) create mode 100644 packages/babel-core/src/transformation/file/merge-map.js create mode 100644 packages/babel-core/test/fixtures/transformation/source-maps/input-source-map-complex/actual.js create mode 100644 packages/babel-core/test/fixtures/transformation/source-maps/input-source-map-complex/expected.js create mode 100644 packages/babel-core/test/fixtures/transformation/source-maps/input-source-map-complex/input-source-map.json create mode 100644 packages/babel-core/test/fixtures/transformation/source-maps/input-source-map-complex/options.json create mode 100644 packages/babel-core/test/fixtures/transformation/source-maps/input-source-map-complex/source-map.json create mode 100644 packages/babel-core/test/fixtures/transformation/source-maps/input-source-map-complex/source-mappings.json diff --git a/packages/babel-core/src/transformation/file/index.js b/packages/babel-core/src/transformation/file/index.js index 50b641c19435..0ace87dc41a0 100644 --- a/packages/babel-core/src/transformation/file/index.js +++ b/packages/babel-core/src/transformation/file/index.js @@ -7,7 +7,6 @@ import OptionManager from "./options/option-manager"; import type Pipeline from "../pipeline"; import PluginPass from "../plugin-pass"; import { NodePath, Hub, Scope } from "babel-traverse"; -import sourceMap from "source-map"; import generate from "babel-generator"; import codeFrame from "babel-code-frame"; import defaults from "lodash/defaults"; @@ -19,6 +18,7 @@ import * as util from "../../util"; import path from "path"; import * as t from "babel-types"; +import mergeSourceMap from "./merge-map"; import resolve from "../../helpers/resolve"; import blockHoistPlugin from "../internal-plugins/block-hoist"; @@ -368,42 +368,8 @@ export default class File extends Store { mergeSourceMap(map: Object) { const inputMap = this.opts.inputSourceMap; - if (inputMap) { - const inputMapConsumer = new sourceMap.SourceMapConsumer(inputMap); - const outputMapConsumer = new sourceMap.SourceMapConsumer(map); - - const mergedGenerator = new sourceMap.SourceMapGenerator({ - file: inputMapConsumer.file, - sourceRoot: inputMapConsumer.sourceRoot - }); - - // This assumes the output map always has a single source, since Babel always compiles a - // single source file to a single output file. - const source = outputMapConsumer.sources[0]; - - inputMapConsumer.eachMapping(function (mapping) { - const generatedPosition = outputMapConsumer.generatedPositionFor({ - line: mapping.generatedLine, - column: mapping.generatedColumn, - source: source - }); - if (generatedPosition.column != null) { - mergedGenerator.addMapping({ - source: mapping.source, - - original: mapping.source == null ? null : { - line: mapping.originalLine, - column: mapping.originalColumn - }, - - generated: generatedPosition - }); - } - }); - - const mergedMap = mergedGenerator.toJSON(); - inputMap.mappings = mergedMap.mappings; - return inputMap; + if (inputMap && map) { + return mergeSourceMap(inputMap, map); } else { return map; } diff --git a/packages/babel-core/src/transformation/file/merge-map.js b/packages/babel-core/src/transformation/file/merge-map.js new file mode 100644 index 000000000000..cfbee17ab658 --- /dev/null +++ b/packages/babel-core/src/transformation/file/merge-map.js @@ -0,0 +1,316 @@ +import sourceMap from "source-map"; + +export default function mergeSourceMap( + inputMap: SourceMap, + map: SourceMap, +): SourceMap { + const input = buildMappingData(inputMap); + const output = buildMappingData(map); + + // Babel-generated maps always map to a single input filename. + if (output.sources.length !== 1) { + throw new Error("Assertion failure - expected a single output file"); + } + const defaultSource = output.sources[0]; + + const mergedGenerator = new sourceMap.SourceMapGenerator(); + for (const { source } of input.sources) { + if (typeof source.content === "string") { + mergedGenerator.setSourceContent(source.path, source.content); + } + } + + const insertedMappings = new Map(); + + // Process each generated range in the input map, e.g. each range over the + // code that Babel was originally given. + eachInputGeneratedRange(input, (generated, original, source) => { + // Then pick out each range over Babel's _output_ that corresponds with + // the given range on the code given to Babel. + eachOverlappingGeneratedOutputRange(defaultSource, generated, (item) => { + // It's possible that multiple input ranges will overlap the same + // generated range. Since sourcemap don't traditionally represent + // generated locations with multiple original locations, we explicitly + // skip generated locations once we've seen them the first time. + const key = makeMappingKey(item); + if (insertedMappings.has(key)) return; + insertedMappings.set(key, item); + + mergedGenerator.addMapping({ + source: source.path, + original: { + line: original.line, + column: original.columnStart, + }, + generated: { + line: item.line, + column: item.columnStart, + }, + name: original.name, + }); + }); + }); + + // Since mappings are manipulated using single locations, but are interpreted + // as ranges, the insertions above may not actually have their ending + // locations mapped yet. Here be go through each one and ensure that it has + // a well-defined ending location, if one wasn't already created by the start + // of a different range. + for (const item of insertedMappings.values()) { + if (item.columnEnd === Infinity) { + continue; + } + + const clearItem = { + line: item.line, + columnStart: item.columnEnd, + }; + + const key = makeMappingKey(clearItem); + if (insertedMappings.has(key)) { + continue; + } + + // Insert mappings with no original position to terminate any mappings + // that were found above, so that they don't expand beyond their correct + // range. + mergedGenerator.addMapping({ + generated: { + line: clearItem.line, + column: clearItem.columnStart, + }, + }); + } + + const result = mergedGenerator.toJSON(); + // addMapping expects a relative path, and setSourceContent expects an + // absolute path. To avoid this whole confusion, we leave the root out + // entirely, and add it at the end here. + if (typeof input.sourceRoot === "string") { + result.sourceRoot = input.sourceRoot; + } + return result; +} + +function makeMappingKey(item: { line: number, columnStart: number }) { + return JSON.stringify([item.line, item.columnStart]); +} + +function eachOverlappingGeneratedOutputRange( + outputFile: ResolvedFileMappings, + inputGeneratedRange: ResolvedGeneratedRange, + callback: ResolvedGeneratedRange => mixed, +) { + // Find the Babel-generated mappings that overlap with this range in the + // input sourcemap. Generated locations within the input sourcemap + // correspond with the original locations in the map Babel generates. + const overlappingOriginal = filterApplicableOriginalRanges( + outputFile, + inputGeneratedRange, + ); + + for (const { generated } of overlappingOriginal) { + for (const item of generated) { + callback(item); + } + } +} + +function filterApplicableOriginalRanges( + { mappings }: ResolvedFileMappings, + { line, columnStart, columnEnd }: ResolvedGeneratedRange, +): OriginalMappings { + // The mapping array is sorted by original location, so we can + // binary-search it for the overlapping ranges. + return filterSortedArray(mappings, ({ original: outOriginal }) => { + if (line > outOriginal.line) return -1; + if (line < outOriginal.line) return 1; + + if (columnStart >= outOriginal.columnEnd) return -1; + if (columnEnd <= outOriginal.columnStart) return 1; + + return 0; + }); +} + +function eachInputGeneratedRange( + map: ResolvedMappings, + callback: ( + ResolvedGeneratedRange, + ResolvedOriginalRange, + ResolvedSource, + ) => mixed, +) { + for (const { source, mappings } of map.sources) { + for (const { original, generated } of mappings) { + for (const item of generated) { + callback(item, original, source); + } + } + } +} + +type ResolvedMappings = {| + file: ?string, + sourceRoot: ?string, + sources: Array, +|}; +type ResolvedFileMappings = {| + source: ResolvedSource, + mappings: OriginalMappings, +|}; +type OriginalMappings = Array<{| + original: ResolvedOriginalRange, + generated: Array, +|}>; +type ResolvedSource = {| + path: string, + content: string | null, +|}; +type ResolvedOriginalRange = {| + line: number, + columnStart: number, + columnEnd: number, + name: string | null, +|}; +type ResolvedGeneratedRange = {| + line: number, + columnStart: number, + columnEnd: number, +|}; + +function buildMappingData(map: SourceMap): ResolvedMappings { + const consumer = new sourceMap.SourceMapConsumer({ + ...map, + + // This is a bit hack. .addMapping expects source values to be relative, + // but eachMapping returns mappings with absolute paths. To avoid that + // incompatibility, we leave the sourceRoot out here and add it to the + // final map at the end instead. + sourceRoot: null, + }); + + const sources = new Map(); + const mappings = new Map(); + + let last = null; + + consumer.computeColumnSpans(); + + consumer.eachMapping( + (m) => { + if (m.originalLine === null) return; + + let source = sources.get(m.source); + if (!source) { + source = { + path: m.source, + content: consumer.sourceContentFor(m.source, true), + }; + sources.set(m.source, source); + } + + let sourceData = mappings.get(source); + if (!sourceData) { + sourceData = { + source, + mappings: [], + }; + mappings.set(source, sourceData); + } + + const obj = { + line: m.originalLine, + columnStart: m.originalColumn, + columnEnd: Infinity, + name: m.name, + }; + + if ( + last && + last.source === source && + last.mapping.line === m.originalLine + ) { + last.mapping.columnEnd = m.originalColumn; + } + + last = { + source, + mapping: obj, + }; + + sourceData.mappings.push({ + original: obj, + generated: consumer + .allGeneratedPositionsFor({ + source: m.source, + line: m.originalLine, + column: m.originalColumn, + }) + .map((item) => ({ + line: item.line, + columnStart: item.column, + // source-map's lastColumn is inclusive, not exclusive, so we need + // to add 1 to it. + columnEnd: item.lastColumn + 1, + })), + }); + }, + null, + sourceMap.SourceMapConsumer.ORIGINAL_ORDER, + ); + + return { + file: map.file, + sourceRoot: map.sourceRoot, + sources: Array.from(mappings.values()), + }; +} + +function findInsertionLocation( + array: Array, + callback: T => number, +): number { + let left = 0; + let right = array.length; + while (left < right) { + const mid = Math.floor((left + right) / 2); + const item = array[mid]; + + const result = callback(item); + if (result === 0) { + left = mid; + break; + } + if (result >= 0) { + right = mid; + } else { + left = mid + 1; + } + } + + // Ensure the value is the start of any set of matches. + let i = left; + if (i < array.length) { + while (i > 0 && callback(array[i]) >= 0) { + i--; + } + return i + 1; + } + + return i; +} + +function filterSortedArray( + array: Array, + callback: T => number, +): Array { + const start = findInsertionLocation(array, callback); + + const results = []; + for (let i = start; i < array.length && callback(array[i]) === 0; i++) { + results.push(array[i]); + } + + return results; +} diff --git a/packages/babel-core/test/fixtures/transformation/source-maps/input-source-map-complex/actual.js b/packages/babel-core/test/fixtures/transformation/source-maps/input-source-map-complex/actual.js new file mode 100644 index 000000000000..7188e524b2c0 --- /dev/null +++ b/packages/babel-core/test/fixtures/transformation/source-maps/input-source-map-complex/actual.js @@ -0,0 +1,113 @@ +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// + +// +export default { + name: 'HelloWorld', + data () { + return { + msg: 'Welcome to Your Vue.js App' + } + } +} +// + +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// diff --git a/packages/babel-core/test/fixtures/transformation/source-maps/input-source-map-complex/expected.js b/packages/babel-core/test/fixtures/transformation/source-maps/input-source-map-complex/expected.js new file mode 100644 index 000000000000..25e9b4f55151 --- /dev/null +++ b/packages/babel-core/test/fixtures/transformation/source-maps/input-source-map-complex/expected.js @@ -0,0 +1,118 @@ +'use strict'; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// + +// +exports.default = { + name: 'HelloWorld', + data() { + return { + msg: 'Welcome to Your Vue.js App' + }; + } +}; +// + +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// diff --git a/packages/babel-core/test/fixtures/transformation/source-maps/input-source-map-complex/input-source-map.json b/packages/babel-core/test/fixtures/transformation/source-maps/input-source-map-complex/input-source-map.json new file mode 100644 index 000000000000..22977244838e --- /dev/null +++ b/packages/babel-core/test/fixtures/transformation/source-maps/input-source-map-complex/input-source-map.json @@ -0,0 +1,12 @@ +{ + "version": 3, + "sources": [ + "HelloWorld.vue" + ], + "names": [], + "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsFA;AACA;AACA;AACA;AACA;AACA;AACA;AACA", + "sourceRoot": "src/components", + "sourcesContent": [ + "\n\n\n\n\n\n" + ] +} diff --git a/packages/babel-core/test/fixtures/transformation/source-maps/input-source-map-complex/options.json b/packages/babel-core/test/fixtures/transformation/source-maps/input-source-map-complex/options.json new file mode 100644 index 000000000000..f7a8b05624b1 --- /dev/null +++ b/packages/babel-core/test/fixtures/transformation/source-maps/input-source-map-complex/options.json @@ -0,0 +1,5 @@ +{ + "plugins": [ + "transform-es2015-modules-commonjs" + ] +} diff --git a/packages/babel-core/test/fixtures/transformation/source-maps/input-source-map-complex/source-map.json b/packages/babel-core/test/fixtures/transformation/source-maps/input-source-map-complex/source-map.json new file mode 100644 index 000000000000..0f5291a8b8e6 --- /dev/null +++ b/packages/babel-core/test/fixtures/transformation/source-maps/input-source-map-complex/source-map.json @@ -0,0 +1,12 @@ +{ + "version": 3, + "sources": [ + "HelloWorld.vue" + ], + "names": [], + "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;kBAsFA;AACA,QAAA,YADA;AAEA,SAAA;AACA,WAAA;AACA,WAAA;AADA,KAAA;AAGA;AANA,C", + "sourceRoot": "src/components", + "sourcesContent": [ + "\n\n\n\n\n\n" + ] +} diff --git a/packages/babel-core/test/fixtures/transformation/source-maps/input-source-map-complex/source-mappings.json b/packages/babel-core/test/fixtures/transformation/source-maps/input-source-map-complex/source-mappings.json new file mode 100644 index 000000000000..545109d4f77e --- /dev/null +++ b/packages/babel-core/test/fixtures/transformation/source-maps/input-source-map-complex/source-mappings.json @@ -0,0 +1,72 @@ +[ + { + "generated": { + "line": 92, + "column": 19 + }, + "original": { + "line": 87, + "column": 0 + } + }, + { + "generated": { + "line": 93, + "column": 0 + }, + "original": { + "line": 88, + "column": 0 + } + }, + { + "generated": { + "line": 94, + "column": 9 + }, + "original": { + "line": 89, + "column": 0 + } + }, + { + "generated": { + "line": 95, + "column": 0 + }, + "original": { + "line": 90, + "column": 0 + } + }, + { + "generated": { + "line": 96, + "column": 0 + }, + "original": { + "line": 91, + "column": 0 + } + }, + { + "generated": { + "line": 97, + "column": 0 + }, + "original": { + "line": 90, + "column": 0 + } + }, + { + "generated": { + "line": 99, + "column": 0 + }, + "original": { + "line": 87, + "column": 0 + } + } +] diff --git a/packages/babel-core/test/fixtures/transformation/source-maps/input-source-map/source-map.json b/packages/babel-core/test/fixtures/transformation/source-maps/input-source-map/source-map.json index a1f20e981173..9d0cb92bb363 100644 --- a/packages/babel-core/test/fixtures/transformation/source-maps/input-source-map/source-map.json +++ b/packages/babel-core/test/fixtures/transformation/source-maps/input-source-map/source-map.json @@ -1,6 +1,6 @@ { "version": 3, - "mappings": "AAAA,UAAU,Y;SAAM,A;AAAC", + "mappings": "AAAA,IAAA,MAAU,Y;SAAM,C;AAAC,CAAjB", "names": [], "sources": [ "original.js" diff --git a/packages/babel-helper-fixtures/src/index.js b/packages/babel-helper-fixtures/src/index.js index 598937c51b66..91f880a5e7c5 100644 --- a/packages/babel-helper-fixtures/src/index.js +++ b/packages/babel-helper-fixtures/src/index.js @@ -142,6 +142,11 @@ export default function get(entryLoc): Array { if (fs.existsSync(sourceMapLoc)) { test.sourceMap = JSON.parse(readFile(sourceMapLoc)); } + + const inputMapLoc = taskDir + "/input-source-map.json"; + if (fs.existsSync(inputMapLoc)) { + test.inputSourceMap = JSON.parse(readFile(inputMapLoc)); + } } } diff --git a/packages/babel-helper-transform-fixture-test-runner/src/index.js b/packages/babel-helper-transform-fixture-test-runner/src/index.js index fbca0c3949dd..39505b584865 100644 --- a/packages/babel-helper-transform-fixture-test-runner/src/index.js +++ b/packages/babel-helper-transform-fixture-test-runner/src/index.js @@ -165,6 +165,7 @@ export default function ( suppressDeprecationMessages: true, babelrc: false, sourceMap: !!(task.sourceMappings || task.sourceMap), + inputSourceMap: task.inputSourceMap || undefined, }); extend(task.options, taskOpts); From e72cf66b4c6f418c9b64b713c733e4cc8b9f2e92 Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Wed, 25 Apr 2018 16:30:39 -0700 Subject: [PATCH 4/7] Fix Babel 6 builds on Node 0.10 --- Makefile | 4 ++-- package.json | 1 + scripts/lerna.js | 11 +++++++++++ yarn.lock | 25 ++++++++++++++++++++++++- 4 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 scripts/lerna.js diff --git a/Makefile b/Makefile index 3cdb787a27c0..a7f6e5f0ddbc 100644 --- a/Makefile +++ b/Makefile @@ -68,13 +68,13 @@ publish: BABEL_ENV=production make build-dist make test # not using lerna independent mode atm, so only update packages that have changed since we use ^ - ./node_modules/.bin/lerna publish --only-explicit-updates + node ./scripts/lerna.js publish --only-explicit-updates make clean bootstrap: make clean-all npm install - ./node_modules/.bin/lerna bootstrap + node ./scripts/lerna.js bootstrap make build cd packages/babel-runtime; \ npm install; \ diff --git a/package.json b/package.json index a08fca760d2a..1f6d73603c02 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "babel-plugin-transform-class-properties": "^6.6.0", "babel-plugin-transform-flow-strip-types": "^6.3.13", "babel-plugin-transform-runtime": "^6.3.13", + "babel-polyfill": "^6.26.0", "babel-preset-es2015": "^6.13.2", "babel-preset-stage-0": "^6.0.0", "babel-register": "^6.14.0", diff --git a/scripts/lerna.js b/scripts/lerna.js new file mode 100644 index 000000000000..cd0b1292cce7 --- /dev/null +++ b/scripts/lerna.js @@ -0,0 +1,11 @@ +"use strict"; + +var path = require("path"); + +// A subdependency of lerna has dropped support for early versions of Node without +// a major version bump, so we must load the polyfill to define Object.assign. +// https://github.com/npm/hosted-git-info/pull/25 +require("babel-polyfill"); + +require(path.resolve(__dirname, "../node_modules/lerna/bin/lerna.js")); + diff --git a/yarn.lock b/yarn.lock index d6ae6e8bdb74..636710c363fa 100644 --- a/yarn.lock +++ b/yarn.lock @@ -738,6 +738,14 @@ babel-plugin-transform-strict-mode@^6.24.1: babel-runtime "^6.22.0" babel-types "^6.24.1" +babel-polyfill@^6.26.0: + version "6.26.0" + resolved "https://registry.yarnpkg.com/babel-polyfill/-/babel-polyfill-6.26.0.tgz#379937abc67d7895970adc621f284cd966cf2153" + dependencies: + babel-runtime "^6.26.0" + core-js "^2.5.0" + regenerator-runtime "^0.10.5" + babel-preset-es2015@^6.13.2: version "6.24.1" resolved "https://registry.yarnpkg.com/babel-preset-es2015/-/babel-preset-es2015-6.24.1.tgz#d44050d6bc2c9feea702aaf38d727a0210538939" @@ -821,6 +829,13 @@ babel-runtime@^6.0.0, babel-runtime@^6.18.0, babel-runtime@^6.22.0: core-js "^2.4.0" regenerator-runtime "^0.10.0" +babel-runtime@^6.26.0: + version "6.26.0" + resolved "https://registry.yarnpkg.com/babel-runtime/-/babel-runtime-6.26.0.tgz#965c7058668e82b55d7bfe04ff2337bc8b5647fe" + dependencies: + core-js "^2.4.0" + regenerator-runtime "^0.11.0" + babel-template@^6.16.0, babel-template@^6.24.1, babel-template@^6.25.0: version "6.25.0" resolved "https://registry.yarnpkg.com/babel-template/-/babel-template-6.25.0.tgz#665241166b7c2aa4c619d71e192969552b10c071" @@ -1310,6 +1325,10 @@ core-js@^2.4.0: version "2.4.1" resolved "https://registry.yarnpkg.com/core-js/-/core-js-2.4.1.tgz#4de911e667b0eae9124e34254b53aea6fc618d3e" +core-js@^2.5.0: + version "2.5.5" + resolved "https://registry.yarnpkg.com/core-js/-/core-js-2.5.5.tgz#b14dde936c640c0579a6b50cabcc132dd6127e3b" + core-util-is@~1.0.0: version "1.0.2" resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7" @@ -3741,10 +3760,14 @@ regenerate@^1.2.1: version "1.3.2" resolved "https://registry.yarnpkg.com/regenerate/-/regenerate-1.3.2.tgz#d1941c67bad437e1be76433add5b385f95b19260" -regenerator-runtime@^0.10.0: +regenerator-runtime@^0.10.0, regenerator-runtime@^0.10.5: version "0.10.5" resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.10.5.tgz#336c3efc1220adcedda2c9fab67b5a7955a33658" +regenerator-runtime@^0.11.0: + version "0.11.1" + resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.11.1.tgz#be05ad7f9bf7d22e056f9726cee5017fbf19e2e9" + regenerator-transform@0.9.11: version "0.9.11" resolved "https://registry.yarnpkg.com/regenerator-transform/-/regenerator-transform-0.9.11.tgz#3a7d067520cb7b7176769eb5ff868691befe1283" From ae43e064018e239316428e9cdc61cf4fb983cb23 Mon Sep 17 00:00:00 2001 From: Buu Nguyen Date: Thu, 25 May 2017 08:59:01 -0700 Subject: [PATCH 5/7] Backport minNodeVersion test option from #5765 for 6.x --- CONTRIBUTING.md | 9 +++++++++ packages/babel-helper-fixtures/package.json | 3 ++- packages/babel-helper-fixtures/src/index.js | 19 +++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 4ea00201696f..e1ced90c994d 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -195,6 +195,15 @@ If you need to check for an error that is thrown you can add to the `options.jso } ``` +If the test requires a minimum Node version, you can add `minNodeVersion` (must be in semver format). + +```js +// options.json example +{ + "minNodeVersion": "5.0.0" +} +``` + #### Bootstrapping expected output For both `babel-plugin-x` and `babylon`, you can easily generate an `expected.js`/`expected.json` automatically by just providing `actual.js` and running the tests as you usually would. diff --git a/packages/babel-helper-fixtures/package.json b/packages/babel-helper-fixtures/package.json index ac8eeb39d24a..fe7172c90d73 100644 --- a/packages/babel-helper-fixtures/package.json +++ b/packages/babel-helper-fixtures/package.json @@ -9,6 +9,7 @@ "dependencies": { "babel-runtime": "^6.26.0", "lodash": "^4.17.4", - "try-resolve": "^1.0.1" + "try-resolve": "^1.0.1", + "semver": "^5.3.0" } } diff --git a/packages/babel-helper-fixtures/src/index.js b/packages/babel-helper-fixtures/src/index.js index 91f880a5e7c5..dfb8425dd449 100644 --- a/packages/babel-helper-fixtures/src/index.js +++ b/packages/babel-helper-fixtures/src/index.js @@ -3,9 +3,12 @@ import trimEnd from "lodash/trimEnd"; import resolve from "try-resolve"; import clone from "lodash/clone"; import merge from "lodash/merge"; +import semver from "semver"; import path from "path"; import fs from "fs"; +const nodeVersion = semver.clean(process.version.slice(1)); + function humanize(val, noext) { if (noext) val = path.basename(val, path.extname(val)); return val.replace(/-/g, " "); @@ -125,6 +128,22 @@ export default function get(entryLoc): Array { } }; + // If there's node requirement, check it before pushing task + if (taskOpts.minNodeVersion) { + const minimumVersion = semver.clean(taskOpts.minNodeVersion); + + if (minimumVersion == null) { + throw new Error(`'minNodeVersion' has invalid semver format: ${taskOpts.minNodeVersion}`); + } + + if (semver.lt(nodeVersion, minimumVersion)) { + return; + } + + // Delete to avoid option validation error + delete taskOpts.minNodeVersion; + } + // traceur checks if (test.exec.code.indexOf("// Async.") >= 0) { From 753c2af506d636879c25fa8a769ec4f2458a23be Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Wed, 25 Apr 2018 16:51:45 -0700 Subject: [PATCH 6/7] Disable 2 tests on Node 0.10 --- .../fixtures/babel-node/v8Flag-dashed-with-param/options.json | 1 + .../babel-node/v8Flag-underscored-with-param/options.json | 1 + 2 files changed, 2 insertions(+) diff --git a/packages/babel-cli/test/fixtures/babel-node/v8Flag-dashed-with-param/options.json b/packages/babel-cli/test/fixtures/babel-node/v8Flag-dashed-with-param/options.json index a9cabbc10c64..315d430a661d 100644 --- a/packages/babel-cli/test/fixtures/babel-node/v8Flag-dashed-with-param/options.json +++ b/packages/babel-cli/test/fixtures/babel-node/v8Flag-dashed-with-param/options.json @@ -1,4 +1,5 @@ { + "minNodeVersion": "4.0.0", "args": ["--expose-gc-as=garbageCollector", "--eval", "console.log(typeof global.garbageCollector)"], "stdout": "function" } diff --git a/packages/babel-cli/test/fixtures/babel-node/v8Flag-underscored-with-param/options.json b/packages/babel-cli/test/fixtures/babel-node/v8Flag-underscored-with-param/options.json index a8b0e91738d0..df471111e170 100644 --- a/packages/babel-cli/test/fixtures/babel-node/v8Flag-underscored-with-param/options.json +++ b/packages/babel-cli/test/fixtures/babel-node/v8Flag-underscored-with-param/options.json @@ -1,4 +1,5 @@ { + "minNodeVersion": "4.0.0", "args": ["--expose_gc_as=garbageCollector", "--eval", "console.log(typeof global.garbageCollector)"], "stdout": "function" } From c8b4c2864355e5c9bd160961fd97a4b9acab05ee Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Wed, 25 Apr 2018 17:20:33 -0700 Subject: [PATCH 7/7] Implement minNodeVersion for babel-cli tests too. --- packages/babel-cli/package.json | 3 ++- packages/babel-cli/test/index.js | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/babel-cli/package.json b/packages/babel-cli/package.json index 456024395eb4..d4b1b890b0f0 100644 --- a/packages/babel-cli/package.json +++ b/packages/babel-cli/package.json @@ -35,7 +35,8 @@ "chokidar": "^1.6.1" }, "devDependencies": { - "babel-helper-fixtures": "^6.26.0" + "babel-helper-fixtures": "^6.26.0", + "semver": "^5.5.0" }, "bin": { "babel-doctor": "./bin/babel-doctor.js", diff --git a/packages/babel-cli/test/index.js b/packages/babel-cli/test/index.js index 9da8405733a0..04bf35d8b40f 100644 --- a/packages/babel-cli/test/index.js +++ b/packages/babel-cli/test/index.js @@ -9,6 +9,9 @@ const merge = require("lodash/merge"); const path = require("path"); const chai = require("chai"); const fs = require("fs"); +const semver = require("semver"); + +const nodeVersion = semver.clean(process.version.slice(1)); const fixtureLoc = path.join(__dirname, "fixtures"); const tmpLoc = path.join(__dirname, "tmp"); @@ -174,6 +177,22 @@ fs.readdirSync(fixtureLoc).forEach(function (binName) { opts.inFiles[".babelrc"] = helper.readFile(babelrcLoc); } + // If there's node requirement, check it before pushing task + if (opts.minNodeVersion) { + const minimumVersion = semver.clean(opts.minNodeVersion); + + if (minimumVersion == null) { + throw new Error(`'minNodeVersion' has invalid semver format: ${opts.minNodeVersion}`); + } + + if (semver.lt(nodeVersion, minimumVersion)) { + return; + } + + // Delete to avoid option validation error + delete opts.minNodeVersion; + } + it(testName, buildTest(binName, testName, opts)); }); });