Compare commits
97 Commits
v7.0.0-bet
...
v7.0.0-bet
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ea3f2d9299 | ||
|
|
88a0f52230 | ||
|
|
4a2e1dbbc3 | ||
|
|
2979dd99ac | ||
|
|
534ee4734c | ||
|
|
a01007a3d3 | ||
|
|
493996e02a | ||
|
|
c03a34e509 | ||
|
|
92580e750d | ||
|
|
213805f21e | ||
|
|
28d13cb09b | ||
|
|
9a8ba76e1f | ||
|
|
d88173b9f8 | ||
|
|
85174b6ce1 | ||
|
|
ec2e0b664a | ||
|
|
a3ad518ce1 | ||
|
|
dd0337cc85 | ||
|
|
4887d81929 | ||
|
|
22555cd15d | ||
|
|
593c1a0861 | ||
|
|
7234442fde | ||
|
|
5ea1bfe780 | ||
|
|
2254ed45d2 | ||
|
|
278cd5e572 | ||
|
|
96c0415c86 | ||
|
|
ed98d2491e | ||
|
|
eb38ea2b10 | ||
|
|
f19d559ff3 | ||
|
|
73e64c6cb0 | ||
|
|
3deb246c7d | ||
|
|
1ebc229fa5 | ||
|
|
cc4913699b | ||
|
|
82b6ee734b | ||
|
|
22c8f6376c | ||
|
|
023550c87b | ||
|
|
8e0d247e8c | ||
|
|
65ae4ff15b | ||
|
|
2185256589 | ||
|
|
3316a554bf | ||
|
|
252ea5a966 | ||
|
|
912bcc186d | ||
|
|
63ae923987 | ||
|
|
dde9274986 | ||
|
|
ba111c13b5 | ||
|
|
967414d926 | ||
|
|
92fc26d399 | ||
|
|
fa5eb4f605 | ||
|
|
d6a782b09c | ||
|
|
567d25cfa5 | ||
|
|
1da831baa4 | ||
|
|
b0d8c62d1c | ||
|
|
0e570eceb2 | ||
|
|
ca18ea5e79 | ||
|
|
63d9998aa4 | ||
|
|
88e550c733 | ||
|
|
b96fdf8780 | ||
|
|
180eda3211 | ||
|
|
b3969d35fa | ||
|
|
dccfed3601 | ||
|
|
5759c33b4c | ||
|
|
193bccc93c | ||
|
|
5ce54799ff | ||
|
|
064c17e03f | ||
|
|
f9804e6beb | ||
|
|
47ce7e71c9 | ||
|
|
5ac8ba19a4 | ||
|
|
b5d20ab171 | ||
|
|
539009a980 | ||
|
|
c3654d83c8 | ||
|
|
667f5815c1 | ||
|
|
4f39e6ea4a | ||
|
|
64dfb7574e | ||
|
|
d36b142e30 | ||
|
|
3c8d831fe4 | ||
|
|
0a517b51cd | ||
|
|
79c84f2f9b | ||
|
|
ca86648726 | ||
|
|
2d05487293 | ||
|
|
c3352ad2e0 | ||
|
|
26e4911eb2 | ||
|
|
aa888e666a | ||
|
|
8659e1a88c | ||
|
|
9e384f3915 | ||
|
|
f694b5d034 | ||
|
|
d765573c0b | ||
|
|
64161fa0b5 | ||
|
|
52f22e9e1e | ||
|
|
3e5fce07e4 | ||
|
|
0f42accb87 | ||
|
|
07e69c009b | ||
|
|
ce420ba51c | ||
|
|
9fe30b98a1 | ||
|
|
0a9f136d5f | ||
|
|
e9ed687666 | ||
|
|
63157159ab | ||
|
|
74682f33bc | ||
|
|
b1bf7798ab |
15
.babelrc.js
15
.babelrc.js
@@ -1,6 +1,6 @@
|
||||
"use strict";
|
||||
|
||||
// Blame Logan for this.
|
||||
// Thanks Logan for this.
|
||||
// This works around https://github.com/istanbuljs/istanbuljs/issues/92 until
|
||||
// we have a version of Istanbul that actually works with 7.x.
|
||||
function istanbulHacks() {
|
||||
@@ -38,14 +38,23 @@ const config = {
|
||||
comments: false,
|
||||
presets: [
|
||||
["@babel/env", envOpts],
|
||||
"@babel/flow"
|
||||
],
|
||||
plugins: [
|
||||
// TODO: Use @babel/preset-flow when
|
||||
// https://github.com/babel/babel/issues/7233 is fixed
|
||||
"@babel/plugin-transform-flow-strip-types",
|
||||
["@babel/proposal-class-properties", { loose: true }],
|
||||
"@babel/proposal-export-namespace-from",
|
||||
"@babel/proposal-numeric-separator",
|
||||
["@babel/proposal-object-rest-spread", { useBuiltIns: true }],
|
||||
]
|
||||
],
|
||||
overrides: [{
|
||||
test: "packages/babylon",
|
||||
plugins: [
|
||||
"babel-plugin-transform-charcodes",
|
||||
["@babel/transform-for-of", { assumeArray: true }],
|
||||
],
|
||||
}],
|
||||
};
|
||||
|
||||
if (process.env.BABEL_ENV === "cov") {
|
||||
|
||||
16
.babelrc.rollup.js
Normal file
16
.babelrc.rollup.js
Normal file
@@ -0,0 +1,16 @@
|
||||
"use strict";
|
||||
|
||||
const cloneDeep = require("lodash/cloneDeep");
|
||||
const babelrc = require("./.babelrc.js");
|
||||
|
||||
const config = cloneDeep(babelrc);
|
||||
|
||||
const presetEnv = config.presets.find(preset => preset[0] === "@babel/env");
|
||||
|
||||
if (!presetEnv) {
|
||||
throw new Error("Error while extracting @preset/env from .babelrc.js");
|
||||
}
|
||||
|
||||
presetEnv[1].modules = false;
|
||||
|
||||
module.exports = config;
|
||||
@@ -13,7 +13,7 @@ codemods/*/src
|
||||
[libs]
|
||||
lib/file.js
|
||||
lib/parser.js
|
||||
lib/types.js
|
||||
lib/packages/babel-types/lib/index.js.flow
|
||||
lib/third-party-libs.js.flow
|
||||
|
||||
[options]
|
||||
|
||||
@@ -175,21 +175,21 @@ For example, in [`@babel/plugin-transform-exponentiation-operator/test`](https:/
|
||||
|
||||
- In each subfolder, you can organize your directory structure by categories of tests. (Example: these folders can be named after the feature you are testing or can reference the issue number they fix)
|
||||
- Generally, there are two kinds of tests for plugins
|
||||
- The first is a simple test of the input and output produced by running Babel on some code. We do this by creating an [`actual.js`](https://github.com/babel/babel/blob/master/packages/babel-plugin-transform-exponentiation-operator/test/fixtures/exponentian-operator/binary/actual.js) file and an [`expected.js`](https://github.com/babel/babel/blob/master/packages/babel-plugin-transform-exponentiation-operator/test/fixtures/exponentian-operator/binary/expected.js) file.
|
||||
- If you need to expect an error, you can ignore creating the `expected.js` file and pass a new `throws` key to the `options.json` that contains the error string that is created.
|
||||
- The first is a simple test of the input and output produced by running Babel on some code. We do this by creating an [`input.js`](https://github.com/babel/babel/blob/master/packages/babel-plugin-transform-exponentiation-operator/test/fixtures/exponentian-operator/binary/input.js) file and an [`output.js`](https://github.com/babel/babel/blob/master/packages/babel-plugin-transform-exponentiation-operator/test/fixtures/exponentian-operator/binary/output.js) file.
|
||||
- If you need to expect an error, you can ignore creating the `output.js` file and pass a new `throws` key to the `options.json` that contains the error string that is created.
|
||||
- The second and preferred type is a test that actually evaluates the produced code and asserts that certain properties are true or false. We do this by creating an [`exec.js`](https://github.com/babel/babel/blob/master/packages/babel-plugin-transform-exponentiation-operator/test/fixtures/exponentian-operator/comprehensive/exec.js) file.
|
||||
|
||||
In an actual/expected test, you simply write out the code you want transformed in `actual.js`.
|
||||
In a fixture test, you simply write out the code you want transformed in `input.js`.
|
||||
|
||||
```js
|
||||
// actual.js
|
||||
// input.js
|
||||
2 ** 2;
|
||||
```
|
||||
|
||||
and the expected output after transforming it with your `options.json` in `expected.js`.
|
||||
and the expected output after transforming it with your `options.json` in `output.js`.
|
||||
|
||||
```js
|
||||
// expected.js
|
||||
// output.js
|
||||
Math.pow(2, 2);
|
||||
```
|
||||
In an `exec.js` test, we run or check that the code actually does what it's supposed to do rather than just check the static output.
|
||||
@@ -227,9 +227,9 @@ Inside the `packages/babylon/tests/fixtures` folder are categories/groupings of
|
||||
etc.). To add a test, create a folder under one of these groupings (or create a new one) with a
|
||||
descriptive name, and add the following:
|
||||
|
||||
* Create an `actual.js` file that contains the code you want Babylon to parse.
|
||||
* Create an `input.js` file that contains the code you want Babylon to parse.
|
||||
|
||||
* Add an `expected.json` file with the expected parser output. For added convenience, if there is no `expected.json` present, the test runner will generate one for you.
|
||||
* Add an `output.json` file with the expected parser output. For added convenience, if there is no `output.json` present, the test runner will generate one for you.
|
||||
|
||||
After writing tests for babylon, just build it by running:
|
||||
|
||||
@@ -245,7 +245,7 @@ $ TEST_ONLY=babylon make test-only
|
||||
|
||||
#### 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.
|
||||
For both `@babel/plugin-x` and `babylon`, you can easily generate an `output.js`/`output.json` automatically by just providing `input.js` and running the tests as you usually would.
|
||||
|
||||
```
|
||||
// Example
|
||||
@@ -256,8 +256,8 @@ For both `@babel/plugin-x` and `babylon`, you can easily generate an `expected.j
|
||||
- comments
|
||||
- basic
|
||||
- block-trailing-comment
|
||||
- actual.js
|
||||
- expected.json (will be generated if not created)
|
||||
- input.js
|
||||
- output.json (will be generated if not created)
|
||||
```
|
||||
|
||||
### Debugging code
|
||||
|
||||
118
Gulpfile.js
118
Gulpfile.js
@@ -12,6 +12,11 @@ const gulp = require("gulp");
|
||||
const path = require("path");
|
||||
const webpack = require("webpack");
|
||||
const merge = require("merge-stream");
|
||||
const rollup = require("rollup-stream");
|
||||
const source = require("vinyl-source-stream");
|
||||
const buffer = require("vinyl-buffer");
|
||||
const rollupBabel = require("rollup-plugin-babel");
|
||||
const rollupNodeResolve = require("rollup-plugin-node-resolve");
|
||||
const registerStandalonePackageTask = require("./scripts/gulp-tasks")
|
||||
.registerStandalonePackageTask;
|
||||
|
||||
@@ -27,53 +32,100 @@ function getGlobFromSource(source) {
|
||||
return `./${source}/*/src/**/*.js`;
|
||||
}
|
||||
|
||||
gulp.task("default", ["build"]);
|
||||
function getIndexFromPackage(name) {
|
||||
return `${name}/src/index.js`;
|
||||
}
|
||||
|
||||
gulp.task("build", function() {
|
||||
function compilationLogger(rollup) {
|
||||
return through.obj(function(file, enc, callback) {
|
||||
gutil.log(
|
||||
`Compiling '${chalk.cyan(file.relative)}'${
|
||||
rollup ? " with rollup " : ""
|
||||
}...`
|
||||
);
|
||||
callback(null, file);
|
||||
});
|
||||
}
|
||||
|
||||
function errorsLogger() {
|
||||
return plumber({
|
||||
errorHandler(err) {
|
||||
gutil.log(err.stack);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function rename(fn) {
|
||||
return through.obj(function(file, enc, callback) {
|
||||
file.path = fn(file);
|
||||
callback(null, file);
|
||||
});
|
||||
}
|
||||
|
||||
function buildBabel(exclude) {
|
||||
return merge(
|
||||
sources.map(source => {
|
||||
const base = path.join(__dirname, source);
|
||||
const f = filter(["**", "!**/packages/babylon/**"]);
|
||||
|
||||
return gulp
|
||||
.src(getGlobFromSource(source), { base: base })
|
||||
.pipe(f)
|
||||
.pipe(
|
||||
plumber({
|
||||
errorHandler: function(err) {
|
||||
gutil.log(err.stack);
|
||||
},
|
||||
})
|
||||
)
|
||||
.pipe(
|
||||
newer({
|
||||
dest: base,
|
||||
map: swapSrcWithLib,
|
||||
})
|
||||
)
|
||||
.pipe(
|
||||
through.obj(function(file, enc, callback) {
|
||||
gutil.log("Compiling", "'" + chalk.cyan(file.relative) + "'...");
|
||||
callback(null, file);
|
||||
})
|
||||
)
|
||||
let stream = gulp.src(getGlobFromSource(source), { base: base });
|
||||
|
||||
if (exclude) {
|
||||
const filters = exclude.map(p => `!**/${p}/**`);
|
||||
filters.unshift("**");
|
||||
stream = stream.pipe(filter(filters));
|
||||
}
|
||||
|
||||
return stream
|
||||
.pipe(errorsLogger())
|
||||
.pipe(newer({ dest: base, map: swapSrcWithLib }))
|
||||
.pipe(compilationLogger())
|
||||
.pipe(babel())
|
||||
.pipe(
|
||||
through.obj(function(file, enc, callback) {
|
||||
// Passing 'file.relative' because newer() above uses a relative
|
||||
// path and this keeps it consistent.
|
||||
file.path = path.resolve(file.base, swapSrcWithLib(file.relative));
|
||||
callback(null, file);
|
||||
})
|
||||
// Passing 'file.relative' because newer() above uses a relative
|
||||
// path and this keeps it consistent.
|
||||
rename(file => path.resolve(file.base, swapSrcWithLib(file.relative)))
|
||||
)
|
||||
.pipe(gulp.dest(base));
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
function buildRollup(packages) {
|
||||
return merge(
|
||||
packages.map(pkg => {
|
||||
return rollup({
|
||||
input: getIndexFromPackage(pkg),
|
||||
format: "cjs",
|
||||
plugins: [
|
||||
rollupBabel({
|
||||
babelrc: false,
|
||||
extends: "./.babelrc.rollup.js",
|
||||
}),
|
||||
rollupNodeResolve(),
|
||||
],
|
||||
})
|
||||
.pipe(source("index.js"))
|
||||
.pipe(buffer())
|
||||
.pipe(errorsLogger())
|
||||
.pipe(compilationLogger(/* rollup */ true))
|
||||
.pipe(gulp.dest(path.join(pkg, "lib")));
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
gulp.task("default", ["build"]);
|
||||
|
||||
gulp.task("build", function() {
|
||||
const bundles = ["packages/babylon"];
|
||||
|
||||
return merge([buildBabel(/* exclude */ bundles), buildRollup(bundles)]);
|
||||
});
|
||||
|
||||
gulp.task("watch", ["build"], function() {
|
||||
gulp.task("build-no-bundle", () => buildBabel());
|
||||
|
||||
gulp.task("watch", ["build-no-bundle"], function() {
|
||||
watch(sources.map(getGlobFromSource), { debounceDelay: 200 }, function() {
|
||||
gulp.start("build");
|
||||
gulp.start("build-no-bundle");
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
16
Makefile
16
Makefile
@@ -13,21 +13,20 @@ SOURCES = packages codemods
|
||||
|
||||
build: clean
|
||||
make clean-lib
|
||||
# Build babylon before building all other projects
|
||||
make build-babylon
|
||||
./node_modules/.bin/gulp build
|
||||
node ./packages/babel-types/scripts/generateTypeHelpers.js
|
||||
# call build again as the generated files might need to be compiled again.
|
||||
./node_modules/.bin/gulp build
|
||||
# generate flow and typescript typings
|
||||
node scripts/generators/flow.js > ./packages/babel-types/lib/index.js.flow
|
||||
node scripts/generators/typescript.js > ./packages/babel-types/lib/index.d.ts
|
||||
# generate docs
|
||||
node scripts/generators/docs.js > ./packages/babel-types/README.md
|
||||
ifneq ("$(BABEL_ENV)", "cov")
|
||||
make build-standalone
|
||||
make build-preset-env-standalone
|
||||
endif
|
||||
|
||||
build-babylon:
|
||||
cd packages/babylon; \
|
||||
./node_modules/.bin/rollup -c
|
||||
|
||||
build-standalone:
|
||||
./node_modules/.bin/gulp build-babel-standalone
|
||||
|
||||
@@ -39,16 +38,11 @@ build-dist: build
|
||||
scripts/build-dist.sh
|
||||
cd packages/babel-runtime; \
|
||||
node scripts/build-dist.js
|
||||
node scripts/generate-babel-types-docs.js
|
||||
|
||||
watch: clean
|
||||
make clean-lib
|
||||
BABEL_ENV=development ./node_modules/.bin/gulp watch
|
||||
|
||||
watch-babylon:
|
||||
cd packages/babylon; \
|
||||
./node_modules/.bin/rollup -c -w
|
||||
|
||||
flow:
|
||||
./node_modules/.bin/flow check --strip-root
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@babel/plugin-codemod-optional-catch-binding",
|
||||
"version": "7.0.0-beta.37",
|
||||
"version": "7.0.0-beta.40",
|
||||
"description": "Remove unused catch bindings",
|
||||
"repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-remove-unused-catch-binding",
|
||||
"license": "MIT",
|
||||
@@ -9,13 +9,13 @@
|
||||
"@babel/plugin"
|
||||
],
|
||||
"dependencies": {
|
||||
"@babel/plugin-syntax-optional-catch-binding": "7.0.0-beta.37"
|
||||
"@babel/plugin-syntax-optional-catch-binding": "7.0.0-beta.40"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@babel/core": "7.0.0-beta.37"
|
||||
"@babel/core": "7.0.0-beta.40"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "7.0.0-beta.37",
|
||||
"@babel/helper-plugin-test-runner": "7.0.0-beta.37"
|
||||
"@babel/core": "7.0.0-beta.40",
|
||||
"@babel/helper-plugin-test-runner": "7.0.0-beta.40"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"lerna": "2.0.0-rc.4",
|
||||
"version": "7.0.0-beta.37",
|
||||
"version": "7.0.0-beta.40",
|
||||
"changelog": {
|
||||
"repo": "babel/babel",
|
||||
"labels": {
|
||||
|
||||
1769
lib/types.js
1769
lib/types.js
File diff suppressed because it is too large
Load Diff
30
package.json
30
package.json
@@ -10,32 +10,33 @@
|
||||
"test": "make test"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/cli": "7.0.0-beta.33",
|
||||
"@babel/core": "7.0.0-beta.33",
|
||||
"@babel/plugin-proposal-export-namespace-from": "7.0.0-beta.33",
|
||||
"@babel/preset-env": "7.0.0-beta.33",
|
||||
"@babel/preset-flow": "7.0.0-beta.33",
|
||||
"@babel/preset-stage-0": "7.0.0-beta.33",
|
||||
"@babel/register": "7.0.0-beta.33",
|
||||
"@babel/cli": "7.0.0-beta.39",
|
||||
"@babel/core": "7.0.0-beta.39",
|
||||
"@babel/preset-env": "7.0.0-beta.39",
|
||||
"@babel/preset-flow": "7.0.0-beta.39",
|
||||
"@babel/preset-stage-0": "7.0.0-beta.39",
|
||||
"@babel/register": "7.0.0-beta.39",
|
||||
"async": "^1.5.0",
|
||||
"babel-eslint": "^8.0.1",
|
||||
"babel-loader": "8.0.0-beta.0",
|
||||
"babel-plugin-istanbul": "^4.1.4",
|
||||
"babylon": "7.0.0-beta.33",
|
||||
"babel-plugin-transform-charcodes": "0.0.10",
|
||||
"babylon": "7.0.0-beta.39",
|
||||
"browserify": "^13.1.1",
|
||||
"bundle-collapser": "^1.2.1",
|
||||
"chai": "^4.1.0",
|
||||
"chalk": "^2.0.0",
|
||||
"charcodes": "0.0.10",
|
||||
"derequire": "^2.0.2",
|
||||
"eslint": "^4.5.0",
|
||||
"eslint-config-babel": "^7.0.2",
|
||||
"eslint-plugin-flowtype": "^2.20.0",
|
||||
"eslint-plugin-prettier": "^2.2.0",
|
||||
"eslint-plugin-prettier": "^2.5.0",
|
||||
"flow-bin": "^0.59.0",
|
||||
"graceful-fs": "^4.1.11",
|
||||
"gulp": "^3.9.0",
|
||||
"gulp-babel": "^8.0.0-beta.0",
|
||||
"gulp-filter": "^5.0.1",
|
||||
"gulp-filter": "^5.1.0",
|
||||
"gulp-newer": "^1.0.0",
|
||||
"gulp-plumber": "^1.0.1",
|
||||
"gulp-rename": "^1.2.2",
|
||||
@@ -45,19 +46,24 @@
|
||||
"husky": "^0.14.3",
|
||||
"lerna": "2.0.0",
|
||||
"lerna-changelog": "^0.5.0",
|
||||
"lint-staged": "^4.2.3",
|
||||
"lint-staged": "^6.0.1",
|
||||
"lodash": "^4.2.0",
|
||||
"merge-stream": "^1.0.1",
|
||||
"mocha": "^3.0.0",
|
||||
"nyc": "^11.0.3",
|
||||
"output-file-sync": "^2.0.0",
|
||||
"prettier": "1.9.1",
|
||||
"prettier": "1.10.2",
|
||||
"pump": "^1.0.2",
|
||||
"rimraf": "^2.4.3",
|
||||
"rollup-plugin-babel": "^4.0.0-beta.0",
|
||||
"rollup-plugin-node-resolve": "^3.0.2",
|
||||
"rollup-stream": "^1.24.1",
|
||||
"semver": "^5.0.0",
|
||||
"through2": "^2.0.0",
|
||||
"uglify-js": "^2.4.16",
|
||||
"util.promisify": "^1.0.0",
|
||||
"vinyl-buffer": "^1.0.1",
|
||||
"vinyl-source-stream": "^2.0.0",
|
||||
"webpack": "^3.4.1",
|
||||
"webpack-dependency-suite": "^2.4.4",
|
||||
"webpack-stream": "^4.0.0"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@babel/cli",
|
||||
"version": "7.0.0-beta.37",
|
||||
"version": "7.0.0-beta.40",
|
||||
"description": "Babel command line.",
|
||||
"author": "Sebastian McKenzie <sebmck@gmail.com>",
|
||||
"homepage": "https://babeljs.io/",
|
||||
@@ -29,11 +29,11 @@
|
||||
"chokidar": "^1.6.1"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@babel/core": "7.0.0-beta.37"
|
||||
"@babel/core": "7.0.0-beta.40"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "7.0.0-beta.37",
|
||||
"@babel/helper-fixtures": "7.0.0-beta.37"
|
||||
"@babel/core": "7.0.0-beta.40",
|
||||
"@babel/helper-fixtures": "7.0.0-beta.40"
|
||||
},
|
||||
"bin": {
|
||||
"babel": "./bin/babel.js",
|
||||
|
||||
@@ -7,9 +7,13 @@ import fs from "fs";
|
||||
import * as util from "./util";
|
||||
|
||||
export default function(commander, filenames, opts) {
|
||||
function write(src, relative, base) {
|
||||
function write(src, relative, base, callback) {
|
||||
if (typeof base === "function") {
|
||||
callback = base;
|
||||
base = undefined;
|
||||
}
|
||||
if (!util.isCompilableExtension(relative, commander.extensions)) {
|
||||
return false;
|
||||
return process.nextTick(callback);
|
||||
}
|
||||
|
||||
// remove extension and then append back on .js
|
||||
@@ -17,7 +21,7 @@ export default function(commander, filenames, opts) {
|
||||
|
||||
const dest = getDest(commander, relative, base);
|
||||
|
||||
const data = util.compile(
|
||||
util.compile(
|
||||
src,
|
||||
defaults(
|
||||
{
|
||||
@@ -26,23 +30,28 @@ export default function(commander, filenames, opts) {
|
||||
},
|
||||
opts,
|
||||
),
|
||||
function(err, res) {
|
||||
if (err) return callback(err);
|
||||
if (!res) return callback();
|
||||
|
||||
// we've requested explicit sourcemaps to be written to disk
|
||||
if (
|
||||
res.map &&
|
||||
commander.sourceMaps &&
|
||||
commander.sourceMaps !== "inline"
|
||||
) {
|
||||
const mapLoc = dest + ".map";
|
||||
res.code = util.addSourceMappingUrl(res.code, mapLoc);
|
||||
outputFileSync(mapLoc, JSON.stringify(res.map));
|
||||
}
|
||||
|
||||
outputFileSync(dest, res.code);
|
||||
util.chmod(src, dest);
|
||||
|
||||
util.log(src + " -> " + dest);
|
||||
return callback(null, true);
|
||||
},
|
||||
);
|
||||
|
||||
if (!data) return false;
|
||||
|
||||
// we've requested explicit sourcemaps to be written to disk
|
||||
if (data.map && commander.sourceMaps && commander.sourceMaps !== "inline") {
|
||||
const mapLoc = dest + ".map";
|
||||
data.code = util.addSourceMappingUrl(data.code, mapLoc);
|
||||
outputFileSync(mapLoc, JSON.stringify(data.map));
|
||||
}
|
||||
|
||||
outputFileSync(dest, data.code);
|
||||
util.chmod(src, dest);
|
||||
|
||||
util.log(src + " -> " + dest);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function getDest(commander, filename, base) {
|
||||
@@ -50,17 +59,45 @@ export default function(commander, filenames, opts) {
|
||||
return path.join(commander.outDir, filename);
|
||||
}
|
||||
|
||||
function handleFile(src, filename, base) {
|
||||
const didWrite = write(src, filename, base);
|
||||
|
||||
if (!didWrite && commander.copyFiles) {
|
||||
const dest = getDest(commander, filename, base);
|
||||
outputFileSync(dest, fs.readFileSync(src));
|
||||
util.chmod(src, dest);
|
||||
function handleFile(src, filename, base, callback) {
|
||||
if (typeof base === "function") {
|
||||
callback = base;
|
||||
base = undefined;
|
||||
}
|
||||
|
||||
write(src, filename, base, function(err, res) {
|
||||
if (err) return callback(err);
|
||||
if (!res && commander.copyFiles) {
|
||||
const dest = getDest(commander, filename, base);
|
||||
outputFileSync(dest, fs.readFileSync(src));
|
||||
util.chmod(src, dest);
|
||||
}
|
||||
|
||||
return callback();
|
||||
});
|
||||
}
|
||||
|
||||
function handle(filename) {
|
||||
function sequentialHandleFile(files, dirname, index, callback) {
|
||||
if (typeof index === "function") {
|
||||
callback = index;
|
||||
index = 0;
|
||||
}
|
||||
|
||||
const filename = files[index];
|
||||
const src = path.join(dirname, filename);
|
||||
|
||||
handleFile(src, filename, dirname, function(err) {
|
||||
if (err) return callback(err);
|
||||
index++;
|
||||
if (index !== files.length) {
|
||||
sequentialHandleFile(files, dirname, index, callback);
|
||||
} else {
|
||||
callback();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function handle(filename, callback) {
|
||||
if (!fs.existsSync(filename)) return;
|
||||
|
||||
const stat = fs.statSync(filename);
|
||||
@@ -72,19 +109,32 @@ export default function(commander, filenames, opts) {
|
||||
util.deleteDir(commander.outDir);
|
||||
}
|
||||
|
||||
util
|
||||
.readdir(dirname, commander.includeDotfiles)
|
||||
.forEach(function(filename) {
|
||||
const src = path.join(dirname, filename);
|
||||
handleFile(src, filename, dirname);
|
||||
});
|
||||
const files = util.readdir(dirname, commander.includeDotfiles);
|
||||
sequentialHandleFile(files, dirname, callback);
|
||||
} else {
|
||||
write(filename, path.basename(filename), path.dirname(filename));
|
||||
write(
|
||||
filename,
|
||||
path.basename(filename),
|
||||
path.dirname(filename),
|
||||
callback,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function sequentialHandle(filenames, index = 0) {
|
||||
const filename = filenames[index];
|
||||
|
||||
handle(filename, function(err) {
|
||||
if (err) throw err;
|
||||
index++;
|
||||
if (index !== filenames.length) {
|
||||
sequentialHandle(filenames, index);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (!commander.skipInitialBuild) {
|
||||
filenames.forEach(handle);
|
||||
sequentialHandle(filenames);
|
||||
}
|
||||
|
||||
if (commander.watch) {
|
||||
@@ -104,7 +154,9 @@ export default function(commander, filenames, opts) {
|
||||
watcher.on(type, function(filename) {
|
||||
const relative = path.relative(dirname, filename) || filename;
|
||||
try {
|
||||
handleFile(filename, relative);
|
||||
handleFile(filename, relative, function(err) {
|
||||
if (err) throw err;
|
||||
});
|
||||
} catch (err) {
|
||||
console.error(err.stack);
|
||||
}
|
||||
|
||||
@@ -103,19 +103,21 @@ export default function(commander, filenames, opts) {
|
||||
});
|
||||
|
||||
process.stdin.on("end", function() {
|
||||
results.push(
|
||||
util.transform(
|
||||
commander.filename,
|
||||
code,
|
||||
defaults(
|
||||
{
|
||||
sourceFileName: "stdin",
|
||||
},
|
||||
opts,
|
||||
),
|
||||
util.transform(
|
||||
commander.filename,
|
||||
code,
|
||||
defaults(
|
||||
{
|
||||
sourceFileName: "stdin",
|
||||
},
|
||||
opts,
|
||||
),
|
||||
function(err, res) {
|
||||
if (err) throw err;
|
||||
results.push(res);
|
||||
output();
|
||||
},
|
||||
);
|
||||
output();
|
||||
});
|
||||
};
|
||||
|
||||
@@ -140,7 +142,9 @@ export default function(commander, filenames, opts) {
|
||||
}
|
||||
});
|
||||
|
||||
_filenames.forEach(function(filename) {
|
||||
let filesProcessed = 0;
|
||||
|
||||
_filenames.forEach(function(filename, index) {
|
||||
let sourceFilename = filename;
|
||||
if (commander.outFile) {
|
||||
sourceFilename = path.relative(
|
||||
@@ -150,7 +154,7 @@ export default function(commander, filenames, opts) {
|
||||
}
|
||||
sourceFilename = slash(sourceFilename);
|
||||
|
||||
const data = util.compile(
|
||||
util.compile(
|
||||
filename,
|
||||
defaults(
|
||||
{
|
||||
@@ -158,14 +162,18 @@ export default function(commander, filenames, opts) {
|
||||
},
|
||||
opts,
|
||||
),
|
||||
function(err, res) {
|
||||
if (err) throw err;
|
||||
|
||||
filesProcessed++;
|
||||
if (res) results[index] = res;
|
||||
|
||||
if (filesProcessed === _filenames.length) {
|
||||
output();
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
if (!data) return;
|
||||
|
||||
results.push(data);
|
||||
});
|
||||
|
||||
output();
|
||||
};
|
||||
|
||||
const files = function() {
|
||||
|
||||
@@ -50,25 +50,26 @@ export function log(msg) {
|
||||
if (!commander.quiet) console.log(msg);
|
||||
}
|
||||
|
||||
export function transform(filename, code, opts) {
|
||||
export function transform(filename, code, opts, callback) {
|
||||
opts = Object.assign({}, opts, {
|
||||
filename,
|
||||
});
|
||||
|
||||
return babel.transform(code, opts);
|
||||
babel.transform(code, opts, callback);
|
||||
}
|
||||
|
||||
export function compile(filename, opts) {
|
||||
try {
|
||||
return babel.transformFileSync(filename, opts);
|
||||
} catch (err) {
|
||||
if (commander.watch) {
|
||||
console.error(err);
|
||||
return { ignored: true };
|
||||
} else {
|
||||
throw err;
|
||||
export function compile(filename, opts, callback) {
|
||||
babel.transformFile(filename, opts, function(err, res) {
|
||||
if (err) {
|
||||
if (commander.watch) {
|
||||
console.error(err);
|
||||
return callback(null, null);
|
||||
} else {
|
||||
return callback(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
return callback(null, res);
|
||||
});
|
||||
}
|
||||
|
||||
export function deleteDir(path) {
|
||||
|
||||
@@ -68,6 +68,7 @@ console.log(result);
|
||||
|
||||
Toggles syntax highlighting the code as JavaScript for terminals.
|
||||
|
||||
|
||||
### `linesAbove`
|
||||
|
||||
`number`, defaults to `2`.
|
||||
@@ -86,6 +87,21 @@ Adjust the number of lines to show below the error.
|
||||
|
||||
Enable this to forcibly syntax highlight the code as JavaScript (for non-terminals); overrides `highlightCode`.
|
||||
|
||||
### `message`
|
||||
|
||||
`string`, otherwise nothing
|
||||
|
||||
Pass in a string to be displayed inline (if possible) next to the highlighted
|
||||
location in the code. If it can't be positioned inline, it will be placed above
|
||||
the code frame.
|
||||
|
||||
```
|
||||
1 | class Foo {
|
||||
> 2 | constructor()
|
||||
| ^ Missing {
|
||||
3 | };
|
||||
```
|
||||
|
||||
## Upgrading from prior versions
|
||||
|
||||
Prior to version 7, the only API exposed by this module was for a single line and optional column pointer. The old API will now log a deprecation warning.
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@babel/code-frame",
|
||||
"version": "7.0.0-beta.37",
|
||||
"version": "7.0.0-beta.40",
|
||||
"description": "Generate errors that contain a code frame that point to source locations.",
|
||||
"author": "Sebastian McKenzie <sebmck@gmail.com>",
|
||||
"homepage": "https://babeljs.io/",
|
||||
@@ -8,11 +8,10 @@
|
||||
"repository": "https://github.com/babel/babel/tree/master/packages/babel-code-frame",
|
||||
"main": "lib/index.js",
|
||||
"dependencies": {
|
||||
"chalk": "^2.0.0",
|
||||
"esutils": "^2.0.2",
|
||||
"js-tokens": "^3.0.0"
|
||||
"@babel/highlight": "7.0.0-beta.40"
|
||||
},
|
||||
"devDependencies": {
|
||||
"chalk": "^2.0.0",
|
||||
"strip-ansi": "^4.0.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
import jsTokens, { matchToToken } from "js-tokens";
|
||||
import esutils from "esutils";
|
||||
import Chalk from "chalk";
|
||||
import highlight, { shouldHighlight, getChalk } from "@babel/highlight";
|
||||
|
||||
let deprecationWarningShown = false;
|
||||
|
||||
@@ -15,23 +13,13 @@ type NodeLocation = {
|
||||
};
|
||||
|
||||
/**
|
||||
* Chalk styles for token types.
|
||||
* Chalk styles for code frame token types.
|
||||
*/
|
||||
|
||||
function getDefs(chalk) {
|
||||
return {
|
||||
keyword: chalk.cyan,
|
||||
capitalized: chalk.yellow,
|
||||
jsx_tag: chalk.yellow,
|
||||
punctuator: chalk.yellow,
|
||||
// bracket: intentionally omitted.
|
||||
number: chalk.magenta,
|
||||
string: chalk.green,
|
||||
regex: chalk.magenta,
|
||||
comment: chalk.grey,
|
||||
invalid: chalk.white.bgRed.bold,
|
||||
gutter: chalk.grey,
|
||||
marker: chalk.red.bold,
|
||||
message: chalk.red.bold,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -41,76 +29,6 @@ function getDefs(chalk) {
|
||||
|
||||
const NEWLINE = /\r\n|[\n\r\u2028\u2029]/;
|
||||
|
||||
/**
|
||||
* RegExp to test for what seems to be a JSX tag name.
|
||||
*/
|
||||
|
||||
const JSX_TAG = /^[a-z][\w-]*$/i;
|
||||
|
||||
/**
|
||||
* RegExp to test for the three types of brackets.
|
||||
*/
|
||||
|
||||
const BRACKET = /^[()[\]{}]$/;
|
||||
|
||||
/**
|
||||
* Get the type of token, specifying punctuator type.
|
||||
*/
|
||||
|
||||
function getTokenType(match) {
|
||||
const [offset, text] = match.slice(-2);
|
||||
const token = matchToToken(match);
|
||||
|
||||
if (token.type === "name") {
|
||||
if (esutils.keyword.isReservedWordES6(token.value)) {
|
||||
return "keyword";
|
||||
}
|
||||
|
||||
if (
|
||||
JSX_TAG.test(token.value) &&
|
||||
(text[offset - 1] === "<" || text.substr(offset - 2, 2) == "</")
|
||||
) {
|
||||
return "jsx_tag";
|
||||
}
|
||||
|
||||
if (token.value[0] !== token.value[0].toLowerCase()) {
|
||||
return "capitalized";
|
||||
}
|
||||
}
|
||||
|
||||
if (token.type === "punctuator" && BRACKET.test(token.value)) {
|
||||
return "bracket";
|
||||
}
|
||||
|
||||
if (
|
||||
token.type === "invalid" &&
|
||||
(token.value === "@" || token.value === "#")
|
||||
) {
|
||||
return "punctuator";
|
||||
}
|
||||
|
||||
return token.type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Highlight `text`.
|
||||
*/
|
||||
|
||||
function highlight(defs: Object, text: string) {
|
||||
return text.replace(jsTokens, function(...args) {
|
||||
const type = getTokenType(args);
|
||||
const colorize = defs[type];
|
||||
if (colorize) {
|
||||
return args[0]
|
||||
.split(NEWLINE)
|
||||
.map(str => colorize(str))
|
||||
.join("\n");
|
||||
} else {
|
||||
return args[0];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract what lines should be marked and highlighted.
|
||||
*/
|
||||
@@ -126,9 +44,7 @@ function getMarkerLines(
|
||||
loc.start,
|
||||
);
|
||||
const endLoc: Location = Object.assign({}, startLoc, loc.end);
|
||||
const linesAbove = opts.linesAbove || 2;
|
||||
const linesBelow = opts.linesBelow || 3;
|
||||
|
||||
const { linesAbove = 2, linesBelow = 3 } = opts || {};
|
||||
const startLine = startLoc.line;
|
||||
const startColumn = startLoc.column;
|
||||
const endLine = endLoc.line;
|
||||
@@ -187,29 +103,28 @@ export function codeFrameColumns(
|
||||
opts: Object = {},
|
||||
): string {
|
||||
const highlighted =
|
||||
(opts.highlightCode && Chalk.supportsColor) || opts.forceColor;
|
||||
let chalk = Chalk;
|
||||
if (opts.forceColor) {
|
||||
chalk = new Chalk.constructor({ enabled: true });
|
||||
}
|
||||
(opts.highlightCode || opts.forceColor) && shouldHighlight(opts);
|
||||
const chalk = getChalk(opts);
|
||||
const defs = getDefs(chalk);
|
||||
const maybeHighlight = (chalkFn, string) => {
|
||||
return highlighted ? chalkFn(string) : string;
|
||||
};
|
||||
const defs = getDefs(chalk);
|
||||
if (highlighted) rawLines = highlight(defs, rawLines);
|
||||
if (highlighted) rawLines = highlight(rawLines, opts);
|
||||
|
||||
const lines = rawLines.split(NEWLINE);
|
||||
const { start, end, markerLines } = getMarkerLines(loc, lines, opts);
|
||||
const hasColumns = loc.start && typeof loc.start.column === "number";
|
||||
|
||||
const numberMaxWidth = String(end).length;
|
||||
|
||||
const frame = lines
|
||||
let frame = lines
|
||||
.slice(start, end)
|
||||
.map((line, index) => {
|
||||
const number = start + 1 + index;
|
||||
const paddedNumber = ` ${number}`.slice(-numberMaxWidth);
|
||||
const gutter = ` ${paddedNumber} | `;
|
||||
const hasMarker = markerLines[number];
|
||||
const lastMarkerLine = !markerLines[number + 1];
|
||||
if (hasMarker) {
|
||||
let markerLine = "";
|
||||
if (Array.isArray(hasMarker)) {
|
||||
@@ -224,6 +139,10 @@ export function codeFrameColumns(
|
||||
markerSpacing,
|
||||
maybeHighlight(defs.marker, "^").repeat(numberOfMarkers),
|
||||
].join("");
|
||||
|
||||
if (lastMarkerLine && opts.message) {
|
||||
markerLine += " " + maybeHighlight(defs.message, opts.message);
|
||||
}
|
||||
}
|
||||
return [
|
||||
maybeHighlight(defs.marker, ">"),
|
||||
@@ -237,6 +156,10 @@ export function codeFrameColumns(
|
||||
})
|
||||
.join("\n");
|
||||
|
||||
if (opts.message && !hasColumns) {
|
||||
frame = `${" ".repeat(numberMaxWidth + 1)}${opts.message}\n${frame}`;
|
||||
}
|
||||
|
||||
if (highlighted) {
|
||||
return chalk.reset(frame);
|
||||
} else {
|
||||
|
||||
@@ -184,6 +184,57 @@ describe("@babel/code-frame", function() {
|
||||
);
|
||||
});
|
||||
|
||||
it("opts.linesAbove no lines above", function() {
|
||||
const rawLines = [
|
||||
"class Foo {",
|
||||
" constructor() {",
|
||||
" console.log(arguments);",
|
||||
" }",
|
||||
"};",
|
||||
].join("\n");
|
||||
assert.equal(
|
||||
codeFrameColumns(rawLines, { start: { line: 2 } }, { linesAbove: 0 }),
|
||||
[
|
||||
"> 2 | constructor() {",
|
||||
" 3 | console.log(arguments);",
|
||||
" 4 | }",
|
||||
" 5 | };",
|
||||
].join("\n"),
|
||||
);
|
||||
});
|
||||
|
||||
it("opts.linesBelow no lines below", function() {
|
||||
const rawLines = [
|
||||
"class Foo {",
|
||||
" constructor() {",
|
||||
" console.log(arguments);",
|
||||
" }",
|
||||
"};",
|
||||
].join("\n");
|
||||
assert.equal(
|
||||
codeFrameColumns(rawLines, { start: { line: 2 } }, { linesBelow: 0 }),
|
||||
[" 1 | class Foo {", "> 2 | constructor() {"].join("\n"),
|
||||
);
|
||||
});
|
||||
|
||||
it("opts.linesBelow single line", function() {
|
||||
const rawLines = [
|
||||
"class Foo {",
|
||||
" constructor() {",
|
||||
" console.log(arguments);",
|
||||
" }",
|
||||
"};",
|
||||
].join("\n");
|
||||
assert.equal(
|
||||
codeFrameColumns(
|
||||
rawLines,
|
||||
{ start: { line: 2 } },
|
||||
{ linesAbove: 0, linesBelow: 0 },
|
||||
),
|
||||
["> 2 | constructor() {"].join("\n"),
|
||||
);
|
||||
});
|
||||
|
||||
it("opts.forceColor", function() {
|
||||
const marker = chalk.red.bold;
|
||||
const gutter = chalk.grey;
|
||||
@@ -299,4 +350,101 @@ describe("@babel/code-frame", function() {
|
||||
].join("\n"),
|
||||
);
|
||||
});
|
||||
|
||||
it("opts.message", function() {
|
||||
const rawLines = ["class Foo {", " constructor()", "};"].join("\n");
|
||||
assert.equal(
|
||||
codeFrameColumns(
|
||||
rawLines,
|
||||
{ start: { line: 2, column: 16 } },
|
||||
{
|
||||
message: "Missing {",
|
||||
},
|
||||
),
|
||||
[
|
||||
" 1 | class Foo {",
|
||||
"> 2 | constructor()",
|
||||
" | ^ Missing {",
|
||||
" 3 | };",
|
||||
].join("\n"),
|
||||
);
|
||||
});
|
||||
|
||||
it("opts.message without column", function() {
|
||||
const rawLines = ["class Foo {", " constructor()", "};"].join("\n");
|
||||
assert.equal(
|
||||
codeFrameColumns(
|
||||
rawLines,
|
||||
{ start: { line: 2 } },
|
||||
{
|
||||
message: "Missing {",
|
||||
},
|
||||
),
|
||||
[
|
||||
" Missing {",
|
||||
" 1 | class Foo {",
|
||||
"> 2 | constructor()",
|
||||
" 3 | };",
|
||||
].join("\n"),
|
||||
);
|
||||
});
|
||||
|
||||
it("opts.message with multiple lines", function() {
|
||||
const rawLines = [
|
||||
"class Foo {",
|
||||
" constructor() {",
|
||||
" console.log(arguments);",
|
||||
" }",
|
||||
"};",
|
||||
].join("\n");
|
||||
assert.equal(
|
||||
codeFrameColumns(
|
||||
rawLines,
|
||||
{
|
||||
start: { line: 2, column: 17 },
|
||||
end: { line: 4, column: 3 },
|
||||
},
|
||||
{
|
||||
message: "something about the constructor body",
|
||||
},
|
||||
),
|
||||
[
|
||||
" 1 | class Foo {",
|
||||
"> 2 | constructor() {",
|
||||
" | ^",
|
||||
"> 3 | console.log(arguments);",
|
||||
" | ^^^^^^^^^^^^^^^^^^^^^^^^^^^",
|
||||
"> 4 | }",
|
||||
" | ^^^ something about the constructor body",
|
||||
" 5 | };",
|
||||
].join("\n"),
|
||||
);
|
||||
});
|
||||
|
||||
it("opts.message with multiple lines without columns", function() {
|
||||
const rawLines = [
|
||||
"class Foo {",
|
||||
" constructor() {",
|
||||
" console.log(arguments);",
|
||||
" }",
|
||||
"};",
|
||||
].join("\n");
|
||||
assert.equal(
|
||||
codeFrameColumns(
|
||||
rawLines,
|
||||
{ start: { line: 2 }, end: { line: 4 } },
|
||||
{
|
||||
message: "something about the constructor body",
|
||||
},
|
||||
),
|
||||
[
|
||||
" something about the constructor body",
|
||||
" 1 | class Foo {",
|
||||
"> 2 | constructor() {",
|
||||
"> 3 | console.log(arguments);",
|
||||
"> 4 | }",
|
||||
" 5 | };",
|
||||
].join("\n"),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@babel/core",
|
||||
"version": "7.0.0-beta.37",
|
||||
"version": "7.0.0-beta.40",
|
||||
"description": "Babel compiler core.",
|
||||
"main": "./lib/index.js",
|
||||
"author": "Sebastian McKenzie <sebmck@gmail.com>",
|
||||
@@ -28,13 +28,13 @@
|
||||
"./lib/transform-file-sync.js": "./lib/transform-file-sync-browser.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@babel/code-frame": "7.0.0-beta.37",
|
||||
"@babel/generator": "7.0.0-beta.37",
|
||||
"@babel/helpers": "7.0.0-beta.37",
|
||||
"@babel/template": "7.0.0-beta.37",
|
||||
"@babel/traverse": "7.0.0-beta.37",
|
||||
"@babel/types": "7.0.0-beta.37",
|
||||
"babylon": "7.0.0-beta.37",
|
||||
"@babel/code-frame": "7.0.0-beta.40",
|
||||
"@babel/generator": "7.0.0-beta.40",
|
||||
"@babel/helpers": "7.0.0-beta.40",
|
||||
"@babel/template": "7.0.0-beta.40",
|
||||
"@babel/traverse": "7.0.0-beta.40",
|
||||
"@babel/types": "7.0.0-beta.40",
|
||||
"babylon": "7.0.0-beta.40",
|
||||
"convert-source-map": "^1.1.0",
|
||||
"debug": "^3.0.1",
|
||||
"json5": "^0.5.0",
|
||||
@@ -44,7 +44,7 @@
|
||||
"source-map": "^0.5.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/helper-transform-fixture-test-runner": "7.0.0-beta.37",
|
||||
"@babel/register": "7.0.0-beta.37"
|
||||
"@babel/helper-transform-fixture-test-runner": "7.0.0-beta.40",
|
||||
"@babel/register": "7.0.0-beta.40"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,12 +12,7 @@ import {
|
||||
|
||||
const debug = buildDebug("babel:config:config-chain");
|
||||
|
||||
import {
|
||||
findBabelrc,
|
||||
findBabelignore,
|
||||
loadConfig,
|
||||
type ConfigFile,
|
||||
} from "./files";
|
||||
import { findRelativeConfig, loadConfig, type ConfigFile } from "./files";
|
||||
|
||||
import { makeWeakCache, makeStrongCache } from "./caching";
|
||||
|
||||
@@ -108,7 +103,6 @@ const loadPresetOverridesEnvDescriptors = makeWeakCache(
|
||||
* Build a config chain for Babel's full root configuration.
|
||||
*/
|
||||
export function buildRootChain(
|
||||
cwd: string,
|
||||
opts: ValidatedOptions,
|
||||
context: ConfigContext,
|
||||
): ConfigChain | null {
|
||||
@@ -125,22 +119,15 @@ export function buildRootChain(
|
||||
// resolve all .babelrc files
|
||||
if (opts.babelrc !== false && context.filename !== null) {
|
||||
const filename = context.filename;
|
||||
const babelignoreFile = findBabelignore(filename);
|
||||
if (
|
||||
babelignoreFile &&
|
||||
shouldIgnore(
|
||||
context,
|
||||
babelignoreFile.ignore,
|
||||
null,
|
||||
babelignoreFile.dirname,
|
||||
)
|
||||
) {
|
||||
|
||||
const { ignore, config } = findRelativeConfig(filename, context.envName);
|
||||
|
||||
if (ignore && shouldIgnore(context, ignore.ignore, null, ignore.dirname)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const babelrcFile = findBabelrc(filename, context.envName);
|
||||
if (babelrcFile) {
|
||||
const result = loadFileChain(babelrcFile, context);
|
||||
if (config) {
|
||||
const result = loadFileChain(config, context);
|
||||
if (!result) return null;
|
||||
|
||||
mergeChain(fileChain, result);
|
||||
|
||||
@@ -21,41 +21,58 @@ export type IgnoreFile = {
|
||||
ignore: Array<string>,
|
||||
};
|
||||
|
||||
export type RelativeConfig = {
|
||||
config: ConfigFile | null,
|
||||
ignore: IgnoreFile | null,
|
||||
};
|
||||
|
||||
const BABELRC_FILENAME = ".babelrc";
|
||||
const BABELRC_JS_FILENAME = ".babelrc.js";
|
||||
const PACKAGE_FILENAME = "package.json";
|
||||
const BABELIGNORE_FILENAME = ".babelignore";
|
||||
|
||||
export function findBabelrc(
|
||||
export function findRelativeConfig(
|
||||
filepath: string,
|
||||
envName: string,
|
||||
): ConfigFile | null {
|
||||
): RelativeConfig {
|
||||
let config = null;
|
||||
let ignore = null;
|
||||
|
||||
const dirname = path.dirname(filepath);
|
||||
let loc = dirname;
|
||||
while (true) {
|
||||
const conf = [
|
||||
BABELRC_FILENAME,
|
||||
BABELRC_JS_FILENAME,
|
||||
PACKAGE_FILENAME,
|
||||
].reduce((previousConfig: ConfigFile | null, name) => {
|
||||
const filepath = path.join(loc, name);
|
||||
const config = readConfig(filepath, envName);
|
||||
if (!config) {
|
||||
config = [BABELRC_FILENAME, BABELRC_JS_FILENAME, PACKAGE_FILENAME].reduce(
|
||||
(previousConfig: ConfigFile | null, name) => {
|
||||
const filepath = path.join(loc, name);
|
||||
const config = readConfig(filepath, envName);
|
||||
|
||||
if (config && previousConfig) {
|
||||
throw new Error(
|
||||
`Multiple configuration files found. Please remove one:\n` +
|
||||
` - ${path.basename(previousConfig.filepath)}\n` +
|
||||
` - ${name}\n` +
|
||||
`from ${loc}`,
|
||||
);
|
||||
if (config && previousConfig) {
|
||||
throw new Error(
|
||||
`Multiple configuration files found. Please remove one:\n` +
|
||||
` - ${path.basename(previousConfig.filepath)}\n` +
|
||||
` - ${name}\n` +
|
||||
`from ${loc}`,
|
||||
);
|
||||
}
|
||||
|
||||
return config || previousConfig;
|
||||
},
|
||||
null,
|
||||
);
|
||||
|
||||
if (config) {
|
||||
debug("Found configuration %o from %o.", config.filepath, dirname);
|
||||
}
|
||||
}
|
||||
|
||||
return config || previousConfig;
|
||||
}, null);
|
||||
if (!ignore) {
|
||||
const ignoreLoc = path.join(loc, BABELIGNORE_FILENAME);
|
||||
ignore = readIgnoreConfig(ignoreLoc);
|
||||
|
||||
if (conf) {
|
||||
debug("Found configuration %o from %o.", conf.filepath, dirname);
|
||||
return conf;
|
||||
if (ignore) {
|
||||
debug("Found ignore %o from %o.", ignore.filepath, dirname);
|
||||
}
|
||||
}
|
||||
|
||||
const nextLoc = path.dirname(loc);
|
||||
@@ -63,27 +80,7 @@ export function findBabelrc(
|
||||
loc = nextLoc;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
export function findBabelignore(filepath: string): IgnoreFile | null {
|
||||
const dirname = path.dirname(filepath);
|
||||
let loc = dirname;
|
||||
while (true) {
|
||||
const ignoreLoc = path.join(loc, BABELIGNORE_FILENAME);
|
||||
const ignore = readIgnoreConfig(ignoreLoc);
|
||||
|
||||
if (ignore) {
|
||||
debug("Found ignore %o from %o.", ignore.filepath, dirname);
|
||||
return ignore;
|
||||
}
|
||||
|
||||
const nextLoc = path.dirname(loc);
|
||||
if (loc === nextLoc) break;
|
||||
loc = nextLoc;
|
||||
}
|
||||
|
||||
return null;
|
||||
return { config, ignore };
|
||||
}
|
||||
|
||||
export function loadConfig(
|
||||
@@ -106,7 +103,7 @@ export function loadConfig(
|
||||
* Read the given config file, returning the result. Returns null if no config was found, but will
|
||||
* throw if there are parsing errors while loading a config.
|
||||
*/
|
||||
function readConfig(filepath, envName) {
|
||||
function readConfig(filepath, envName): ConfigFile | null {
|
||||
return path.extname(filepath) === ".js"
|
||||
? readConfigJS(filepath, { envName })
|
||||
: readConfigFile(filepath);
|
||||
|
||||
@@ -1,27 +1,14 @@
|
||||
// @flow
|
||||
|
||||
export type ConfigFile = {
|
||||
filepath: string,
|
||||
dirname: string,
|
||||
options: {},
|
||||
};
|
||||
import type { ConfigFile, IgnoreFile, RelativeConfig } from "./configuration";
|
||||
|
||||
export type IgnoreFile = {
|
||||
filepath: string,
|
||||
dirname: string,
|
||||
ignore: Array<string>,
|
||||
};
|
||||
export type { ConfigFile, IgnoreFile, RelativeConfig };
|
||||
|
||||
export function findBabelrc(
|
||||
export function findRelativeConfig(
|
||||
filepath: string,
|
||||
envName: string, // eslint-disable-line no-unused-vars
|
||||
): ConfigFile | null {
|
||||
return null;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
export function findBabelignore(filepath: string): IgnoreFile | null {
|
||||
return null;
|
||||
): RelativeConfig {
|
||||
return { config: null, ignore: null };
|
||||
}
|
||||
|
||||
export function loadConfig(name: string, dirname: string): ConfigFile {
|
||||
|
||||
@@ -62,7 +62,7 @@ export default function loadConfig(inputOpts: mixed): ResolvedConfig | null {
|
||||
envName,
|
||||
};
|
||||
|
||||
const configChain = buildRootChain(absoluteCwd, args, context);
|
||||
const configChain = buildRootChain(args, context);
|
||||
if (!configChain) return null;
|
||||
|
||||
const optionDefaults = {};
|
||||
|
||||
@@ -140,6 +140,8 @@ export function assertConfigApplicableTest(
|
||||
key: string,
|
||||
value: mixed,
|
||||
): ConfigApplicableTest | void {
|
||||
if (value === undefined) return value;
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
value.forEach((item, i) => {
|
||||
if (!checkValidTest(item)) {
|
||||
|
||||
@@ -43,6 +43,8 @@ export { default as transformFileSync } from "./transform-file-sync";
|
||||
export { default as transformFromAst } from "./transform-ast";
|
||||
export { default as transformFromAstSync } from "./transform-ast-sync";
|
||||
|
||||
export { default as parse } from "./parse";
|
||||
|
||||
/**
|
||||
* Recommended set of compilable extensions. Not used in @babel/core directly, but meant as
|
||||
* as an easy source for tooling making use of @babel/core.
|
||||
|
||||
22
packages/babel-core/src/parse.js
Normal file
22
packages/babel-core/src/parse.js
Normal file
@@ -0,0 +1,22 @@
|
||||
// @flow
|
||||
|
||||
import loadConfig, { type InputOptions } from "./config";
|
||||
import normalizeFile from "./transformation/normalize-file";
|
||||
import normalizeOptions from "./transformation/normalize-opts";
|
||||
|
||||
type AstRoot = BabelNodeFile | BabelNodeProgram;
|
||||
|
||||
export default function parse(
|
||||
code: string,
|
||||
opts: InputOptions,
|
||||
): AstRoot | null {
|
||||
const config = loadConfig(opts);
|
||||
|
||||
if (config === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const file = normalizeFile(config.passes, normalizeOptions(config), code);
|
||||
|
||||
return file.ast;
|
||||
}
|
||||
@@ -69,7 +69,7 @@ function buildModule(whitelist) {
|
||||
t.exportNamedDeclaration(
|
||||
null,
|
||||
Object.keys(refs).map(name => {
|
||||
return t.exportSpecifier(t.clone(refs[name]), t.identifier(name));
|
||||
return t.exportSpecifier(t.cloneNode(refs[name]), t.identifier(name));
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -123,7 +123,7 @@ export default class File {
|
||||
|
||||
addHelper(name: string): Object {
|
||||
const declar = this.declarations[name];
|
||||
if (declar) return declar;
|
||||
if (declar) return t.cloneNode(declar);
|
||||
|
||||
const generator = this.get("helperGenerator");
|
||||
const runtime = this.get("helpersNamespace");
|
||||
@@ -131,7 +131,7 @@ export default class File {
|
||||
const res = generator(name);
|
||||
if (res) return res;
|
||||
} else if (runtime) {
|
||||
return t.memberExpression(runtime, t.identifier(name));
|
||||
return t.memberExpression(t.cloneNode(runtime), t.identifier(name));
|
||||
}
|
||||
|
||||
const uid = (this.declarations[name] = this.scope.generateUidIdentifier(
|
||||
|
||||
@@ -91,7 +91,6 @@ function parser(pluginPasses, options, code) {
|
||||
} catch (err) {
|
||||
const { loc, missingPlugin } = err;
|
||||
if (loc) {
|
||||
err.loc = null;
|
||||
const codeFrame = codeFrameColumns(
|
||||
code,
|
||||
{
|
||||
@@ -110,6 +109,7 @@ function parser(pluginPasses, options, code) {
|
||||
err.message =
|
||||
`${options.filename || "unknown"}: ${err.message}\n\n` + codeFrame;
|
||||
}
|
||||
err.code = "BABEL_PARSE_ERROR";
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
|
||||
3
packages/babel-core/test/fixtures/parse/.babelrc
vendored
Normal file
3
packages/babel-core/test/fixtures/parse/.babelrc
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["@babel/syntax-decorators"]
|
||||
}
|
||||
6
packages/babel-core/test/fixtures/parse/input.js
vendored
Normal file
6
packages/babel-core/test/fixtures/parse/input.js
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
@annotation
|
||||
class MyClass { }
|
||||
|
||||
function annotation(target) {
|
||||
target.annotated = true;
|
||||
}
|
||||
286
packages/babel-core/test/fixtures/parse/output.json
vendored
Normal file
286
packages/babel-core/test/fixtures/parse/output.json
vendored
Normal file
@@ -0,0 +1,286 @@
|
||||
{
|
||||
"type": "File",
|
||||
"start": 0,
|
||||
"end": 90,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 6,
|
||||
"column": 1
|
||||
}
|
||||
},
|
||||
"program": {
|
||||
"type": "Program",
|
||||
"start": 0,
|
||||
"end": 90,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 6,
|
||||
"column": 1
|
||||
}
|
||||
},
|
||||
"sourceType": "module",
|
||||
"body": [
|
||||
{
|
||||
"type": "ClassDeclaration",
|
||||
"start": 0,
|
||||
"end": 29,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 17
|
||||
}
|
||||
},
|
||||
"decorators": [
|
||||
{
|
||||
"type": "Decorator",
|
||||
"start": 0,
|
||||
"end": 11,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 11
|
||||
}
|
||||
},
|
||||
"expression": {
|
||||
"type": "Identifier",
|
||||
"start": 1,
|
||||
"end": 11,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 1
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 11
|
||||
},
|
||||
"identifierName": "annotation"
|
||||
},
|
||||
"name": "annotation"
|
||||
}
|
||||
}
|
||||
],
|
||||
"id": {
|
||||
"type": "Identifier",
|
||||
"start": 18,
|
||||
"end": 25,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 2,
|
||||
"column": 6
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 13
|
||||
},
|
||||
"identifierName": "MyClass"
|
||||
},
|
||||
"name": "MyClass"
|
||||
},
|
||||
"superClass": null,
|
||||
"body": {
|
||||
"type": "ClassBody",
|
||||
"start": 26,
|
||||
"end": 29,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 2,
|
||||
"column": 14
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 17
|
||||
}
|
||||
},
|
||||
"body": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "FunctionDeclaration",
|
||||
"start": 31,
|
||||
"end": 90,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 4,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 6,
|
||||
"column": 1
|
||||
}
|
||||
},
|
||||
"id": {
|
||||
"type": "Identifier",
|
||||
"start": 40,
|
||||
"end": 50,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 4,
|
||||
"column": 9
|
||||
},
|
||||
"end": {
|
||||
"line": 4,
|
||||
"column": 19
|
||||
},
|
||||
"identifierName": "annotation"
|
||||
},
|
||||
"name": "annotation"
|
||||
},
|
||||
"generator": false,
|
||||
"async": false,
|
||||
"params": [
|
||||
{
|
||||
"type": "Identifier",
|
||||
"start": 51,
|
||||
"end": 57,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 4,
|
||||
"column": 20
|
||||
},
|
||||
"end": {
|
||||
"line": 4,
|
||||
"column": 26
|
||||
},
|
||||
"identifierName": "target"
|
||||
},
|
||||
"name": "target"
|
||||
}
|
||||
],
|
||||
"body": {
|
||||
"type": "BlockStatement",
|
||||
"start": 59,
|
||||
"end": 90,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 4,
|
||||
"column": 28
|
||||
},
|
||||
"end": {
|
||||
"line": 6,
|
||||
"column": 1
|
||||
}
|
||||
},
|
||||
"body": [
|
||||
{
|
||||
"type": "ExpressionStatement",
|
||||
"start": 64,
|
||||
"end": 88,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 5,
|
||||
"column": 3
|
||||
},
|
||||
"end": {
|
||||
"line": 5,
|
||||
"column": 27
|
||||
}
|
||||
},
|
||||
"expression": {
|
||||
"type": "AssignmentExpression",
|
||||
"start": 64,
|
||||
"end": 87,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 5,
|
||||
"column": 3
|
||||
},
|
||||
"end": {
|
||||
"line": 5,
|
||||
"column": 26
|
||||
}
|
||||
},
|
||||
"operator": "=",
|
||||
"left": {
|
||||
"type": "MemberExpression",
|
||||
"start": 64,
|
||||
"end": 80,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 5,
|
||||
"column": 3
|
||||
},
|
||||
"end": {
|
||||
"line": 5,
|
||||
"column": 19
|
||||
}
|
||||
},
|
||||
"object": {
|
||||
"type": "Identifier",
|
||||
"start": 64,
|
||||
"end": 70,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 5,
|
||||
"column": 3
|
||||
},
|
||||
"end": {
|
||||
"line": 5,
|
||||
"column": 9
|
||||
},
|
||||
"identifierName": "target"
|
||||
},
|
||||
"name": "target"
|
||||
},
|
||||
"property": {
|
||||
"type": "Identifier",
|
||||
"start": 71,
|
||||
"end": 80,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 5,
|
||||
"column": 10
|
||||
},
|
||||
"end": {
|
||||
"line": 5,
|
||||
"column": 19
|
||||
},
|
||||
"identifierName": "annotated"
|
||||
},
|
||||
"name": "annotated"
|
||||
},
|
||||
"computed": false
|
||||
},
|
||||
"right": {
|
||||
"type": "BooleanLiteral",
|
||||
"start": 83,
|
||||
"end": 87,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 5,
|
||||
"column": 22
|
||||
},
|
||||
"end": {
|
||||
"line": 5,
|
||||
"column": 26
|
||||
}
|
||||
},
|
||||
"value": true
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"directives": []
|
||||
}
|
||||
}
|
||||
],
|
||||
"directives": []
|
||||
},
|
||||
"comments": []
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"version": 3,
|
||||
"file": "source-maps/full/expected.js",
|
||||
"sources": ["source-maps/full/actual.js"],
|
||||
"file": "source-maps/full/output.js",
|
||||
"sources": ["source-maps/full/input.js"],
|
||||
"names": [
|
||||
"arr",
|
||||
"map",
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
arr.map(function (x) {
|
||||
return x * x;
|
||||
});
|
||||
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInNvdXJjZS1tYXBzL2lubGluZS9hY3R1YWwuanMiXSwibmFtZXMiOlsiYXJyIiwibWFwIiwieCJdLCJtYXBwaW5ncyI6IkFBQUFBLElBQUlDLEdBQUosQ0FBUTtBQUFBLFNBQUtDLElBQUlBLENBQVQ7QUFBQSxDQUFSIiwiZmlsZSI6InNvdXJjZS1tYXBzL2lubGluZS9leHBlY3RlZC5qcyIsInNvdXJjZXNDb250ZW50IjpbImFyci5tYXAoeCA9PiB4ICogeCk7Il19
|
||||
4
packages/babel-core/test/fixtures/transformation/source-maps/inline/output.js
vendored
Normal file
4
packages/babel-core/test/fixtures/transformation/source-maps/inline/output.js
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
arr.map(function (x) {
|
||||
return x * x;
|
||||
});
|
||||
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInNvdXJjZS1tYXBzL2lubGluZS9pbnB1dC5qcyJdLCJuYW1lcyI6WyJhcnIiLCJtYXAiLCJ4Il0sIm1hcHBpbmdzIjoiQUFBQUEsSUFBSUMsR0FBSixDQUFRO0FBQUEsU0FBS0MsSUFBSUEsQ0FBVDtBQUFBLENBQVIiLCJmaWxlIjoic291cmNlLW1hcHMvaW5saW5lL291dHB1dC5qcyIsInNvdXJjZXNDb250ZW50IjpbImFyci5tYXAoeCA9PiB4ICogeCk7Il19
|
||||
25
packages/babel-core/test/parse.js
Normal file
25
packages/babel-core/test/parse.js
Normal file
@@ -0,0 +1,25 @@
|
||||
import assert from "assert";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import { parse } from "../lib";
|
||||
|
||||
function fixture(...args) {
|
||||
return path.join(__dirname, "fixtures", "parse", ...args);
|
||||
}
|
||||
|
||||
describe("parse", function() {
|
||||
it("should parse using configuration from .babelrc when a filename is provided", function() {
|
||||
const input = fs.readFileSync(fixture("input.js"), "utf8");
|
||||
const output = fs.readFileSync(fixture("output.json"), "utf8");
|
||||
assert(
|
||||
parse(input, { filename: fixture("input.js"), cwd: fixture() }),
|
||||
output,
|
||||
);
|
||||
});
|
||||
|
||||
it("should parse using passed in configuration", function() {
|
||||
const input = fs.readFileSync(fixture("input.js"), "utf8");
|
||||
const output = fs.readFileSync(fixture("output.json"), "utf8");
|
||||
assert(parse(input, { parserOpts: { plugins: ["decorators"] } }), output);
|
||||
});
|
||||
});
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@babel/generator",
|
||||
"version": "7.0.0-beta.37",
|
||||
"version": "7.0.0-beta.40",
|
||||
"description": "Turns an AST into code.",
|
||||
"author": "Sebastian McKenzie <sebmck@gmail.com>",
|
||||
"homepage": "https://babeljs.io/",
|
||||
@@ -11,14 +11,14 @@
|
||||
"lib"
|
||||
],
|
||||
"dependencies": {
|
||||
"@babel/types": "7.0.0-beta.37",
|
||||
"@babel/types": "7.0.0-beta.40",
|
||||
"jsesc": "^2.5.1",
|
||||
"lodash": "^4.2.0",
|
||||
"source-map": "^0.5.0",
|
||||
"trim-right": "^1.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/helper-fixtures": "7.0.0-beta.37",
|
||||
"babylon": "7.0.0-beta.37"
|
||||
"@babel/helper-fixtures": "7.0.0-beta.40",
|
||||
"babylon": "7.0.0-beta.40"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,7 +95,34 @@ export function Decorator(node: Object) {
|
||||
this.newline();
|
||||
}
|
||||
|
||||
export function CallExpression(node: Object) {
|
||||
export function OptionalMemberExpression(node: Object) {
|
||||
this.print(node.object, node);
|
||||
|
||||
if (!node.computed && t.isMemberExpression(node.property)) {
|
||||
throw new TypeError("Got a MemberExpression for MemberExpression property");
|
||||
}
|
||||
|
||||
let computed = node.computed;
|
||||
if (t.isLiteral(node.property) && typeof node.property.value === "number") {
|
||||
computed = true;
|
||||
}
|
||||
if (node.optional) {
|
||||
this.token("?.");
|
||||
}
|
||||
|
||||
if (computed) {
|
||||
this.token("[");
|
||||
this.print(node.property, node);
|
||||
this.token("]");
|
||||
} else {
|
||||
if (!node.optional) {
|
||||
this.token(".");
|
||||
}
|
||||
this.print(node.property, node);
|
||||
}
|
||||
}
|
||||
|
||||
export function OptionalCallExpression(node: Object) {
|
||||
this.print(node.callee, node);
|
||||
|
||||
this.print(node.typeParameters, node); // TS
|
||||
@@ -108,6 +135,15 @@ export function CallExpression(node: Object) {
|
||||
this.token(")");
|
||||
}
|
||||
|
||||
export function CallExpression(node: Object) {
|
||||
this.print(node.callee, node);
|
||||
|
||||
this.print(node.typeParameters, node); // TS
|
||||
this.token("(");
|
||||
this.printList(node.arguments, node);
|
||||
this.token(")");
|
||||
}
|
||||
|
||||
export function Import() {
|
||||
this.word("import");
|
||||
}
|
||||
@@ -203,17 +239,12 @@ export function MemberExpression(node: Object) {
|
||||
computed = true;
|
||||
}
|
||||
|
||||
if (node.optional) {
|
||||
this.token("?.");
|
||||
}
|
||||
if (computed) {
|
||||
this.token("[");
|
||||
this.print(node.property, node);
|
||||
this.token("]");
|
||||
} else {
|
||||
if (!node.optional) {
|
||||
this.token(".");
|
||||
}
|
||||
this.token(".");
|
||||
this.print(node.property, node);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -475,6 +475,14 @@ export function TypeCastExpression(node: Object) {
|
||||
this.token(")");
|
||||
}
|
||||
|
||||
export function Variance(node: Object) {
|
||||
if (node.kind === "plus") {
|
||||
this.token("+");
|
||||
} else {
|
||||
this.token("-");
|
||||
}
|
||||
}
|
||||
|
||||
export function VoidTypeAnnotation() {
|
||||
this.word("void");
|
||||
}
|
||||
|
||||
@@ -124,7 +124,7 @@ export function NumericLiteral(node: Object) {
|
||||
}
|
||||
}
|
||||
|
||||
export function StringLiteral(node: Object, parent: Object) {
|
||||
export function StringLiteral(node: Object) {
|
||||
const raw = this.getPossibleRaw(node);
|
||||
if (!this.format.minified && raw != null) {
|
||||
this.token(raw);
|
||||
@@ -133,7 +133,7 @@ export function StringLiteral(node: Object, parent: Object) {
|
||||
|
||||
// ensure the output is ASCII-safe
|
||||
const opts = {
|
||||
quotes: t.isJSX(parent) ? "double" : this.format.quotes,
|
||||
quotes: "double",
|
||||
wrap: true,
|
||||
};
|
||||
if (this.format.jsonCompatibleStrings) {
|
||||
|
||||
@@ -46,7 +46,6 @@ function normalizeOptions(code, opts): Format {
|
||||
compact: opts.compact,
|
||||
minified: opts.minified,
|
||||
concise: opts.concise,
|
||||
quotes: "double",
|
||||
jsonCompatibleStrings: opts.jsonCompatibleStrings,
|
||||
indent: {
|
||||
adjustMultilineComment: true,
|
||||
|
||||
@@ -19,7 +19,6 @@ export type Format = {
|
||||
auxiliaryCommentAfter: string,
|
||||
compact: boolean | "auto",
|
||||
minified: boolean,
|
||||
quotes: "single" | "double",
|
||||
concise: boolean,
|
||||
indent: {
|
||||
adjustMultilineComment: boolean,
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user