Skip to content

Commit

Permalink
Merge pull request #7812 from loganfsmyth/backported-sourcemap-changes
Browse files Browse the repository at this point in the history
Backported several sourcemap PRs
  • Loading branch information
loganfsmyth committed Apr 26, 2018
2 parents 6689d2d + c8b4c28 commit 6fa027e
Show file tree
Hide file tree
Showing 39 changed files with 1,086 additions and 69 deletions.
9 changes: 9 additions & 0 deletions CONTRIBUTING.md
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Expand Up @@ -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; \
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion packages/babel-cli/package.json
Expand Up @@ -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",
Expand Down
@@ -1,4 +1,5 @@
{
"minNodeVersion": "4.0.0",
"args": ["--expose-gc-as=garbageCollector", "--eval", "console.log(typeof global.garbageCollector)"],
"stdout": "function"
}
@@ -1,4 +1,5 @@
{
"minNodeVersion": "4.0.0",
"args": ["--expose_gc_as=garbageCollector", "--eval", "console.log(typeof global.garbageCollector)"],
"stdout": "function"
}
19 changes: 19 additions & 0 deletions packages/babel-cli/test/index.js
Expand Up @@ -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");
Expand Down Expand Up @@ -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));
});
});
Expand Down
40 changes: 3 additions & 37 deletions packages/babel-core/src/transformation/file/index.js
Expand Up @@ -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";
Expand All @@ -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";
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 6fa027e

Please sign in to comment.