diff --git a/CHANGELOG.md b/CHANGELOG.md index 96d28278e150..f90ac42c1212 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,107 @@ _Note: Gaps between patch versions are faulty, broken or test releases._ See [CHANGELOG - 6to5](CHANGELOG-6to5.md) for the pre-4.0.0 version changelog. +## 6.24.0 (2017-03-13) + +A quick release for 2 features: + +- Thanks to @rwjblue, there is now a `noInterop` option for our `es2015-modules` transform to remove the `interopRequireDefault` and `interopRequireWildcard` helpers. + +Input + +```js +import foo from "foo"; +foo; +``` + +Regular Output + +```js +var _foo = require("foo"); +var _foo2 = _interopRequireDefault(_foo); +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } +_foo2.default; +``` + +Output with option `noInterop` + +```js +"use strict"; +var _foo = require("foo"); +(0, _foo.default)(); +``` + +> This also helps [ember-cli migrate to Babel 6](https://github.com/ember-cli/ember-cli/pull/6828). + +- @izaakschroeder has added `dirname` to the preset constructor which presets can use to resolve things relative to files. + +Example usage of `fileContext.dirname` in a preset + +```js +module.exports = function preset (context, options, fileContext) { + if (/resolve-addons-relative-to-file$/.test(fileContext.dirname)) { + return { + plugins: ['plugin-here'], + }; + } + return {}; +}; +``` + +> This will help out with reusing a [`browserslist` file for babel-preset-env](https://github.com/babel/babel-preset-env/issues/26) and for plugins like https://github.com/tleunen/babel-plugin-module-resolver. + +#### :rocket: New Feature +* `babel-plugin-transform-es2015-modules-amd`, `babel-plugin-transform-es2015-modules-commonjs` + * [#5427](https://github.com/babel/babel/pull/5427) Backport `noInterop` flag for modules to 6.x. ([@rwjblue](https://github.com/rwjblue)) +* `babel-core` + * [#4834](https://github.com/babel/babel/pull/4834) Pass `dirname` as extra metadata to preset constructor. ([@izaakschroeder](https://github.com/izaakschroeder)) + +#### :bug: Bug Fix +* `babel-generator` + * [#5453](https://github.com/babel/babel/pull/5453) Keep parentheses for logical expression when in await expression. ([@aaronang](https://github.com/aaronang)) + * [#5339](https://github.com/babel/babel/pull/5339) Wrap some generated do expressions in parens. ([@zjmiller](https://github.com/zjmiller)) +* `babel-generator`, `babel-plugin-transform-object-rest-spread` + * [#5322](https://github.com/babel/babel/pull/5322) Fix for-await printing. ([@danez](https://github.com/danez)) + +#### :memo: Documentation + +* [#5449](https://github.com/babel/babel/pull/5449) Fixed broken links in README.md [skip-ci]. ([@sethbergman](https://github.com/sethbergman)) +* [#5409](https://github.com/babel/babel/pull/5409) Fix transform-object-rest-spread README. ([@existentialism](https://github.com/existentialism)) +* [#5379](https://github.com/babel/babel/pull/5379) Lint code snippets in READMEs. ([@xtuc](https://github.com/xtuc)) +* [#5334](https://github.com/babel/babel/pull/5334) Shorthand properties examples. ([@bhoule](https://github.com/bhoule)) +* [#5329](https://github.com/babel/babel/pull/5329) Update CONTRIBUTING.md with respect to coverage check [skip ci]. ([@zjmiller](https://github.com/zjmiller)) + +#### :house: Internal +* Other + * [#5338](https://github.com/babel/babel/pull/5338) Improve babel-generator's code coverage. ([@alxpy](https://github.com/alxpy)) + * [#5336](https://github.com/babel/babel/pull/5336) Enable codecov partial coverage. ([@danez](https://github.com/danez)) + * [#5350](https://github.com/babel/babel/pull/5350) Remove redundant NODE_ENV=test in Makefile. ([@aaronang](https://github.com/aaronang)) + * [#5312](https://github.com/babel/babel/pull/5312) [skip ci] Add devEngines to package.json. ([@yavorsky](https://github.com/yavorsky)) +* `babel-cli` + * [#5342](https://github.com/babel/babel/pull/5342) Add test for passing arguments to babel-node (#5163). ([@outsideris](https://github.com/outsideris)) +* `babel-traverse` + * [#5296](https://github.com/babel/babel/pull/5296) Add test for reference paths. ([@jasonLaster](https://github.com/jasonLaster)) + +#### Committers: 14 +- Aaron Ang ([aaronang](https://github.com/aaronang)) +- Alex Kuzmenko ([alxpy](https://github.com/alxpy)) +- Artem Gurzhii ([artemgurzhii](https://github.com/artemgurzhii)) +- Artem Yavorsky ([yavorsky](https://github.com/yavorsky)) +- Brendan Houle ([bhoule](https://github.com/bhoule)) +- Brian Ng ([existentialism](https://github.com/existentialism)) +- Daniel Tschinder ([danez](https://github.com/danez)) +- Izaak Schroeder ([izaakschroeder](https://github.com/izaakschroeder)) +- Jason Laster ([jasonLaster](https://github.com/jasonLaster)) +- JeongHoon Byun (aka Outsider) ([outsideris](https://github.com/outsideris)) +- Robert Jackson ([rwjblue](https://github.com/rwjblue)) +- Seth Bergman ([sethbergman](https://github.com/sethbergman)) +- Sven SAULEAU ([xtuc](https://github.com/xtuc)) +- Zachary Miller ([zjmiller](https://github.com/zjmiller)) + +## 6.23.1 (2017-02-13) + +Regression: Revert https://github.com/babel/babel/pull/5306 since it made a backwards-incompatible change. + ## 6.23.0 (2017-02-13) #### :rocket: New Feature diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index e34f627436da..f979d6d797bd 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -2,13 +2,15 @@ ---- -

+

Setup | Running linting/tests | Writing tests | + Debugging code + | Internals

@@ -25,7 +27,7 @@ Contributions are always welcome, no matter how large or small. - If you aren't just making a documentation change, you'll probably want to learn a bit about a few topics. - [ASTs](https://en.wikipedia.org/wiki/Abstract_syntax_tree) (Abstract Syntax Tree): The Babel AST [spec](https://github.com/babel/babylon/blob/master/ast/spec.md) is a bit different from [ESTree](https://github.com/estree/estree). The differences are listed [here](https://github.com/babel/babylon#output). - - This repository's [`/doc`](/doc) directory for notes on Babel's internals + - This repository's [`/doc`](https://github.com/babel/babel/tree/master/doc) directory for notes on Babel's internals - Check out [the Babel Plugin Handbook](https://github.com/thejameskyle/babel-handbook/blob/master/translations/en/plugin-handbook.md#babel-plugin-handbook) - core plugins are written the same way as any other plugin! - Check out [AST Explorer](http://astexplorer.net/#/scUfOmVOG5) to learn more about ASTs or make your own plugin in the browser - When you feel ready to finally jump into the babel source code a good start is to look out for issues which are labeled with [help-wanted](https://github.com/babel/babel/issues?q=is%3Aissue+is%3Aopen+label%3A%22help+wanted%22) and/or [beginner-friendly](https://github.com/babel/babel/issues?q=is%3Aissue+is%3Aopen+label%3A%22beginner-friendly%22). @@ -42,7 +44,7 @@ Babel is built for node 0.10 and up but we develop using node 6. Make sure you a You can check this with `node -v` and `npm -v`. -#### Setup +### Setup ```sh $ git clone https://github.com/babel/babel @@ -72,7 +74,7 @@ If you wish to build a copy of Babel for distribution, then run: $ make build-dist ``` -#### Running linting/tests +### Running linting/tests You can run lint via: @@ -103,7 +105,7 @@ $ make test-only Most likely you'll want to focus in on a specific issue. -To run tests for a specific package in [packages](/packages), you can use the `TEST_ONLY` environment variable: +To run tests for a specific package in [packages](https://github.com/babel/babel/tree/master/packages), you can use the `TEST_ONLY` environment variable: ```sh $ TEST_ONLY=babel-cli make test @@ -124,30 +126,31 @@ $ TEST_DEBUG=true make test To test the code coverage, use: ```sh +$ BABEL_ENV=cov make build $ ./scripts/test-cov.sh ``` -#### Writing tests +### Writing tests -Most packages in [`/packages`](/packages) have a `test` folder, however some tests might be in other packages or in [`/packages/babel-core`](/packages/babel-core/test/fixtures). +Most packages in [`/packages`](https://github.com/babel/babel/tree/master/packages) have a `test` folder, however some tests might be in other packages or in [`/packages/babel-core`](https://github.com/babel/babel/tree/master/packages/babel-core/test/fixtures). -##### `babel-plugin-x` +#### `babel-plugin-x` All the Babel plugins (and other packages) that have a `/test/fixtures` are written in a similar way. -For example, in [`babel-plugin-transform-exponentiation-operator/test`](/packages/babel-plugin-transform-exponentiation-operator/test): +For example, in [`babel-plugin-transform-exponentiation-operator/test`](https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-exponentiation-operator/test): -- There is an `index.js` file. It imports our [test helper](/packages/babel-helper-plugin-test-runner). (You don't have to worry about this). -- There can be multiple folders under [`/fixtures`](/packages/babel-plugin-transform-exponentiation-operator/test/fixtures) - - There is an [`options.json`](/packages/babel-plugin-transform-exponentiation-operator/test/fixtures/exponentian-operator/options.json) file whose function is similar to a `.babelrc` file, allowing you to pass in the plugins and settings you need for your tests. +- There is an `index.js` file. It imports our [test helper](https://github.com/babel/babel/tree/master/packages/babel-helper-plugin-test-runner). (You don't have to worry about this). +- There can be multiple folders under [`/fixtures`](https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-exponentiation-operator/test/fixtures) + - There is an [`options.json`](https://github.com/babel/babel/blob/master/packages/babel-plugin-transform-exponentiation-operator/test/fixtures/exponentian-operator/options.json) file whose function is similar to a `.babelrc` file, allowing you to pass in the plugins and settings you need for your tests. - For this test, we only need the relevant plugin, so it's just `{ "plugins": ["transform-exponentiation-operator"] }`. - If necessary, you can have an `options.json` with different options in each subfolder. - 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`](packages/babel-plugin-transform-exponentiation-operator/test/fixtures/exponentian-operator/binary/actual.js) file and an [`expected.js`](/packages/babel-plugin-transform-exponentiation-operator/test/fixtures/exponentian-operator/binary/expected.js) file. + - 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 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`](/packages/babel-plugin-transform-exponentiation-operator/test/fixtures/exponentian-operator/comprehensive/exec.js) file. + - 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`. @@ -180,7 +183,7 @@ If you need to check for an error that is thrown you can add to the `options.jso } ``` -##### Bootstrapping expected output +#### 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. @@ -196,9 +199,46 @@ For both `babel-plugin-x` and `babylon`, you can easily generate an `expected.js - expected.json (will be generated if not created) ``` -#### Internals +### Debugging code + +A common approach to debugging JavaScript code is to walk through the code using the [Chrome DevTools](https://developers.google.com/web/tools/chrome-devtools/) debugger. +For illustration purposes, we are going to assume that we need to get a better understanding of [`Generator.generate()`](https://github.com/babel/babel/blob/b5246994b57f06af871be6a63dcc4c6fd41d94d6/packages/babel-generator/src/index.js#L32), which is responsible for generating code for a given AST. +To get a better understanding of what is actually going on for this particular piece of code, we are going to make use of breakpoints. + +```diff +generate() { ++ debugger; // breakpoint + return super.generate(this.ast); +} +``` + +To include the changes, we have to make sure to build Babel: + +```bash +$ make build +``` + +Next, we need to execute `Generator.generate()`, which can be achieved by running a test case in the `babel-generator` package. +For example, we can run the test case that tests the generation of class declarations: + +```bash +$ TEST_DEBUG=true TEST_GREP=ClassDeclaration make test-only + +./scripts/test.sh +Debugger listening on port 9229. +Warning: This is an experimental feature and could change at any time. +To start debugging, open the following URL in Chrome: + chrome-devtools://devtools/remote/serve_file/@60cd6e859b9f557d2312f5bf532f6aec5f284980/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:9229/3cdaebd2-be88-4e7b-a94b-432950ab72d0 +``` + +To start the debugging in Chrome DevTools, open the given URL. +The debugger starts at the first executed line of code, which is Mocha's first line by default. +Click _Resume script execution_ Resume script execution button. to jump to the set breakpoint. +Note that the code shown in Chrome DevTools is compiled code and therefore differs. + +## Internals - AST spec ([babylon/ast/spec.md](https://github.com/babel/babylon/blob/master/ast/spec.md)) -- Versionning ([doc/design/versioning.md](./doc/design/versioning.md)) -- Monorepo ([doc/design/monorepo.md](./doc/design/monorepo.md)) -- Compiler environment support ([doc/design/compiler-environment-support.md](./doc/design/compiler-environment-support.md)) -- Compiler assumptions ([doc/design/compiler-assumptions.md](./doc/design/compiler-assumptions.md)) +- Versioning ([doc/design/versioning.md](https://github.com/babel/babel/blob/master/doc/design/versioning.md) +- Monorepo ([doc/design/monorepo.md](https://github.com/babel/babel/blob/master/doc/design/monorepo.md)) +- Compiler environment support ([doc/design/compiler-environment-support.md](https://github.com/babel/babel/blob/master/doc/design/compiler-environment-support.md)) +- Compiler assumptions ([doc/design/compiler-assumptions.md](https://github.com/babel/babel/blob/master/doc/design/compiler-assumptions.md)) diff --git a/Makefile b/Makefile index 823878ed175a..e1abb3962bd7 100644 --- a/Makefile +++ b/Makefile @@ -49,11 +49,11 @@ test-only: test: lint test-only test-ci: - NODE_ENV=test make bootstrap + make bootstrap make test-only test-ci-coverage: - NODE_ENV=test BABEL_ENV=cov make bootstrap + BABEL_ENV=cov make bootstrap ./scripts/test-cov.sh ./node_modules/.bin/codecov -f coverage/coverage-final.json diff --git a/README.md b/README.md index 7987b5b8402d..b8fe9840efe5 100644 --- a/README.md +++ b/README.md @@ -25,13 +25,13 @@ When your supported environments don't support certain features natively, it wil ```js // ES2015 arrow function -[1,2,3].map(n => n + 1); +[1, 2, 3].map((n) => n + 1); ``` **Out** ```js -[1,2,3].map(function(n) { +[1, 2, 3].map(function(n) { return n + 1; }); ``` @@ -203,6 +203,14 @@ Babel | Daniel Tschinder | Logan Smyth | Henry Zhu | | [@kangax](https://github.com/kangax) | [@kaicataldo](https://github.com/kaicataldo) | [@motiz88](https://github.com/motiz88) | [@xtuc](https://github.com/xtuc) | | [@kangax](https://twitter.com/kangax) | [@kai_cataldo](https://twitter.com/kai_cataldo) | [@motiz88](https://twitter.com/motiz88) | [@svensauleau](https://twitter.com/svensauleau) | +### Non-Human Members + +[](https://github.com/babel-bot) | +|---| +| Babel Bot | +| [@babel-bot](https://github.com/babel-bot) | +| [@babeljs](https://twitter.com/babeljs) | + ### Inactive members [![Amjad Masad](https://avatars.githubusercontent.com/u/587518?s=64)](https://github.com/amasad) | [![James Kyle](https://avatars.githubusercontent.com/u/952783?s=64)](https://github.com/thejameskyle) | [![Jesse McCarthy](https://avatars.githubusercontent.com/u/129203?s=64)](https://github.com/jmm) | [![Sebastian McKenzie](https://avatars.githubusercontent.com/u/853712?s=64)](https://github.com/kittens) (Creator) | diff --git a/codecov.yml b/codecov.yml new file mode 100644 index 000000000000..3e31ee1814b3 --- /dev/null +++ b/codecov.yml @@ -0,0 +1,4 @@ +coverage: + parsers: + javascript: + enable_partials: yes diff --git a/lerna.json b/lerna.json index 169db035c12b..117685b8a5b6 100644 --- a/lerna.json +++ b/lerna.json @@ -1,6 +1,6 @@ { "lerna": "2.0.0-beta.23", - "version": "6.23.1", + "version": "6.24.0", "changelog": { "repo": "babel/babel", "labels": { diff --git a/package.json b/package.json index 254cad0ffb8f..2747f458d21d 100644 --- a/package.json +++ b/package.json @@ -47,6 +47,10 @@ "through2": "^2.0.0", "uglify-js": "^2.4.16" }, + "devEngines": { + "node": ">= 0.10 <= 7.x", + "npm": "2.x || 3.x || 4.x" + }, "babel": { "comments": false, "presets": [ diff --git a/packages/babel-cli/package.json b/packages/babel-cli/package.json index b2f56d6cfa94..650754193a56 100644 --- a/packages/babel-cli/package.json +++ b/packages/babel-cli/package.json @@ -1,6 +1,6 @@ { "name": "babel-cli", - "version": "6.23.0", + "version": "6.24.0", "description": "Babel command line.", "author": "Sebastian McKenzie ", "homepage": "https://babeljs.io/", @@ -16,8 +16,8 @@ "compiler" ], "dependencies": { - "babel-core": "^6.23.0", - "babel-register": "^6.23.0", + "babel-core": "^6.24.0", + "babel-register": "^6.24.0", "babel-polyfill": "^6.23.0", "babel-runtime": "^6.22.0", "commander": "^2.8.1", diff --git a/packages/babel-cli/test/fixtures/babel-node/arguments/in-files/bar.js b/packages/babel-cli/test/fixtures/babel-node/arguments/in-files/bar.js new file mode 100644 index 000000000000..13257ec5e847 --- /dev/null +++ b/packages/babel-cli/test/fixtures/babel-node/arguments/in-files/bar.js @@ -0,0 +1 @@ +console.log(process.argv[2]); diff --git a/packages/babel-cli/test/fixtures/babel-node/arguments/options.json b/packages/babel-cli/test/fixtures/babel-node/arguments/options.json new file mode 100644 index 000000000000..5aa934d5550a --- /dev/null +++ b/packages/babel-cli/test/fixtures/babel-node/arguments/options.json @@ -0,0 +1,4 @@ +{ + "args": ["bar", "foo"], + "stdout": "foo" +} diff --git a/packages/babel-core/README.md b/packages/babel-core/README.md index 5b2f4d53d17c..ebff1f183737 100644 --- a/packages/babel-core/README.md +++ b/packages/babel-core/README.md @@ -108,8 +108,8 @@ Following is a table of the options you can use: | `moduleRoot` | `(sourceRoot)` | Optional prefix for the AMD module formatter that will be prepend to the filename on module definitions. | | `only` | `null` | A [glob](https://github.com/isaacs/minimatch), regex, or mixed array of both, matching paths to **only** compile. Can also be an array of arrays containing paths to explicitly match. When attempting to compile a non-matching file it's returned verbatim. | | `parserOpts` | `{}` | An object containing the options to be passed down to the babel parser, babylon | -| `plugins` | `[]` | List of [plugins](/docs/plugins/) to load and use. | -| `presets` | `[]` | List of [presets](/docs/plugins/#presets) (a set of plugins) to load and use. | +| `plugins` | `[]` | List of [plugins](https://babeljs.io/docs/plugins/) to load and use. | +| `presets` | `[]` | List of [presets](https://babeljs.io/docs/plugins/#presets) (a set of plugins) to load and use. | | `retainLines` | `false` | Retain line numbers. This will lead to wacky code but is handy for scenarios where you can't use source maps. (**NOTE:** This will not retain the columns) | | `resolveModuleSource` | `null` | Resolve a module source ie. `import "SOURCE";` to a custom value. Called as `resolveModuleSource(source, filename)`. | | `shouldPrintComment` | `null` | An optional callback that controls whether a comment should be output or not. Called as `shouldPrintComment(commentContents)`. **NOTE:** This overrides the `comment` option when used. | diff --git a/packages/babel-core/package.json b/packages/babel-core/package.json index f9eecdac8434..782335fc4454 100644 --- a/packages/babel-core/package.json +++ b/packages/babel-core/package.json @@ -1,6 +1,6 @@ { "name": "babel-core", - "version": "6.23.1", + "version": "6.24.0", "description": "Babel compiler core.", "author": "Sebastian McKenzie ", "homepage": "https://babeljs.io/", @@ -27,12 +27,12 @@ }, "dependencies": { "babel-code-frame": "^6.22.0", - "babel-generator": "^6.23.0", + "babel-generator": "^6.24.0", "babel-helpers": "^6.23.0", "babel-messages": "^6.23.0", "babel-template": "^6.23.0", "babel-runtime": "^6.22.0", - "babel-register": "^6.23.0", + "babel-register": "^6.24.0", "babel-traverse": "^6.23.1", "babel-types": "^6.23.0", "babylon": "^6.11.0", @@ -48,7 +48,7 @@ }, "devDependencies": { "babel-helper-fixtures": "^6.22.0", - "babel-helper-transform-fixture-test-runner": "^6.23.0", + "babel-helper-transform-fixture-test-runner": "^6.24.0", "babel-polyfill": "^6.23.0" } } diff --git a/packages/babel-core/src/transformation/file/options/option-manager.js b/packages/babel-core/src/transformation/file/options/option-manager.js index f4b25206bcc5..4746260a3685 100644 --- a/packages/babel-core/src/transformation/file/options/option-manager.js +++ b/packages/babel-core/src/transformation/file/options/option-manager.js @@ -299,7 +299,7 @@ export default class OptionManager { (presetLoc || "a preset") + " which does not accept options."); } - if (typeof val === "function") val = val(context, options); + if (typeof val === "function") val = val(context, options, { dirname }); if (typeof val !== "object") { throw new Error(`Unsupported preset format: ${val}.`); diff --git a/packages/babel-core/test/fixtures/resolution/resolve-addons-relative-to-file/node_modules/addons/preset.js b/packages/babel-core/test/fixtures/resolution/resolve-addons-relative-to-file/node_modules/addons/preset.js index a1af16e41fac..e08f4b1c612d 100644 --- a/packages/babel-core/test/fixtures/resolution/resolve-addons-relative-to-file/node_modules/addons/preset.js +++ b/packages/babel-core/test/fixtures/resolution/resolve-addons-relative-to-file/node_modules/addons/preset.js @@ -1,5 +1,10 @@ -module.exports = { - plugins: [plugin], +module.exports = function preset (context, options, fileContext) { + if (/resolve-addons-relative-to-file$/.test(fileContext.dirname)) { + return { + plugins: [plugin], + }; + } + return {}; }; function plugin () { diff --git a/packages/babel-generator/README.md b/packages/babel-generator/README.md index 46a4c051b305..ff215b753f2a 100644 --- a/packages/babel-generator/README.md +++ b/packages/babel-generator/README.md @@ -39,6 +39,7 @@ quotes | `'single'` or `'double'` | autodetect based on `ast.tok filename | string | | Used in warning messages flowCommaSeparator | boolean | `false` | Set to `true` to use commas instead of semicolons as Flow property separators jsonCompatibleStrings | boolean | `false` | Set to true to run `jsesc` with "json": true to print "\u00A9" vs. "©"; + Options for source maps: name | type | default | description @@ -53,12 +54,7 @@ sourceFileName | string | | The filename for the sourc In most cases, Babel does a 1:1 transformation of input-file to output-file. However, you may be dealing with AST constructed from multiple sources - JS files, templates, etc. If this is the case, and you want the sourcemaps to reflect the correct sources, you'll need -to make some changes to your code. - -First, each node with a `loc` property (which indicates that node's original placement in the -source document) must also include a `loc.filename` property, set to the source filename. - -Second, you should pass an object to `generate` as the `code` parameter. Keys +to pass an object to `generate` as the `code` parameter. Keys should be the source filenames, and values should be the source content. Here's an example of what that might look like: @@ -69,14 +65,14 @@ import generate from 'babel-generator'; const a = 'var a = 1;'; const b = 'var b = 2;'; -const astA = parse(a, { filename: 'a.js' }); -const astB = parse(b, { filename: 'b.js' }); +const astA = parse(a, { sourceFilename: 'a.js' }); +const astB = parse(b, { sourceFilename: 'b.js' }); const ast = { type: 'Program', - body: [].concat(astA.body, ast2.body) + body: [].concat(astA.program.body, astB.program.body) }; -const { code, map } = generate(ast, { /* options */ }, { +const { code, map } = generate(ast, { sourceMaps: true }, { 'a.js': a, 'b.js': b }); diff --git a/packages/babel-generator/package.json b/packages/babel-generator/package.json index 0212353f434c..f306ab966586 100644 --- a/packages/babel-generator/package.json +++ b/packages/babel-generator/package.json @@ -1,6 +1,6 @@ { "name": "babel-generator", - "version": "6.23.0", + "version": "6.24.0", "description": "Turns an AST into code.", "author": "Sebastian McKenzie ", "homepage": "https://babeljs.io/", diff --git a/packages/babel-generator/src/generators/modules.js b/packages/babel-generator/src/generators/modules.js index 71ffe6e9c3f7..3d6fbae1e21c 100644 --- a/packages/babel-generator/src/generators/modules.js +++ b/packages/babel-generator/src/generators/modules.js @@ -45,12 +45,6 @@ export function ExportAllDeclaration(node: Object) { this.word("export"); this.space(); this.token("*"); - if (node.exported) { - this.space(); - this.word("as"); - this.space(); - this.print(node.exported, node); - } this.space(); this.word("from"); this.space(); diff --git a/packages/babel-generator/src/index.js b/packages/babel-generator/src/index.js index 138336546c50..1d511096d394 100644 --- a/packages/babel-generator/src/index.js +++ b/packages/babel-generator/src/index.js @@ -10,9 +10,7 @@ import type { Format } from "./printer"; */ class Generator extends Printer { - constructor(ast, opts, code) { - opts = opts || {}; - + constructor(ast, opts = {}, code) { const tokens = ast.tokens || []; const format = normalizeOptions(code, opts, tokens); const map = opts.sourceMaps ? new SourceMap(opts, code) : null; @@ -101,7 +99,7 @@ function findCommonStringDelimiter(code, tokens) { return DEFAULT_STRING_DELIMITER; } - const occurences = { + const occurrences = { single: 0, double: 0 }; @@ -114,15 +112,15 @@ function findCommonStringDelimiter(code, tokens) { const raw = code.slice(token.start, token.end); if (raw[0] === "'") { - occurences.single++; + occurrences.single++; } else { - occurences.double++; + occurrences.double++; } checked++; if (checked >= 3) break; } - if (occurences.single > occurences.double) { + if (occurrences.single > occurrences.double) { return "single"; } else { return "double"; diff --git a/packages/babel-generator/src/node/parentheses.js b/packages/babel-generator/src/node/parentheses.js index 558bceb14c68..f2ba48d32073 100644 --- a/packages/babel-generator/src/node/parentheses.js +++ b/packages/babel-generator/src/node/parentheses.js @@ -34,28 +34,25 @@ export function NullableTypeAnnotation(node: Object, parent: Object): boolean { export { NullableTypeAnnotation as FunctionTypeAnnotation }; export function UpdateExpression(node: Object, parent: Object): boolean { - if (t.isMemberExpression(parent) && parent.object === node) { - // (foo++).test() - return true; - } - - return false; + // (foo++).test() + return t.isMemberExpression(parent) && parent.object === node; } export function ObjectExpression(node: Object, parent: Object, printStack: Array): boolean { return isFirstInStatement(printStack, { considerArrow: true }); } -export function Binary(node: Object, parent: Object): boolean { - if ((t.isCallExpression(parent) || t.isNewExpression(parent)) && parent.callee === node) { - return true; - } - - if (t.isUnaryLike(parent)) { - return true; - } +export function DoExpression(node: Object, parent: Object, printStack: Array): boolean { + return isFirstInStatement(printStack); +} - if (t.isMemberExpression(parent) && parent.object === node) { +export function Binary(node: Object, parent: Object): boolean { + if ( + ((t.isCallExpression(parent) || t.isNewExpression(parent)) && parent.callee === node) || + t.isUnaryLike(parent) || + (t.isMemberExpression(parent) && parent.object === node) || + t.isAwaitExpression(parent) + ) { return true; } @@ -66,12 +63,11 @@ export function Binary(node: Object, parent: Object): boolean { const nodeOp = node.operator; const nodePos = PRECEDENCE[nodeOp]; - if (parentPos > nodePos) { - return true; - } - - // Logical expressions with the same precedence don't need parens. - if (parentPos === nodePos && parent.right === node && !t.isLogicalExpression(parent)) { + if ( + // Logical expressions with the same precedence don't need parens. + (parentPos === nodePos && parent.right === node && !t.isLogicalExpression(parent)) || + parentPos > nodePos + ) { return true; } } @@ -80,55 +76,27 @@ export function Binary(node: Object, parent: Object): boolean { } export function BinaryExpression(node: Object, parent: Object): boolean { - if (node.operator === "in") { - // let i = (1 in []); - if (t.isVariableDeclarator(parent)) { - return true; - } - - // for ((1 in []);;); - if (t.isFor(parent)) { - return true; - } - } - - return false; + // let i = (1 in []); + // for ((1 in []);;); + return node.operator === "in" && (t.isVariableDeclarator(parent) || t.isFor(parent)); } export function SequenceExpression(node: Object, parent: Object): boolean { - if (t.isForStatement(parent)) { + + if ( // Although parentheses wouldn"t hurt around sequence // expressions in the head of for loops, traditional style // dictates that e.g. i++, j++ should not be wrapped with // parentheses. - return false; - } - - if (t.isExpressionStatement(parent) && parent.expression === node) { - return false; - } - - if (t.isReturnStatement(parent)) { - return false; - } - - if (t.isThrowStatement(parent)) { - return false; - } - - if (t.isSwitchStatement(parent) && parent.discriminant === node) { - return false; - } - - if (t.isWhileStatement(parent) && parent.test === node) { - return false; - } - - if (t.isIfStatement(parent) && parent.test === node) { - return false; - } - - if (t.isForInStatement(parent) && parent.right === node) { + t.isForStatement(parent) || + t.isThrowStatement(parent) || + t.isReturnStatement(parent) || + (t.isIfStatement(parent) && parent.test === node) || + (t.isWhileStatement(parent) && parent.test === node) || + (t.isForInStatement(parent) && parent.right === node) || + (t.isSwitchStatement(parent) && parent.discriminant === node) || + (t.isExpressionStatement(parent) && parent.expression === node) + ) { return false; } @@ -154,15 +122,9 @@ export function ClassExpression(node: Object, parent: Object, printStack: Array< } export function UnaryLike(node: Object, parent: Object): boolean { - if (t.isMemberExpression(parent, { object: node })) { - return true; - } - - if (t.isCallExpression(parent, { callee: node }) || t.isNewExpression(parent, { callee: node })) { - return true; - } - - return false; + return t.isMemberExpression(parent, { object: node }) || + t.isCallExpression(parent, { callee: node }) || + t.isNewExpression(parent, { callee: node }); } export function FunctionExpression(node: Object, parent: Object, printStack: Array): boolean { @@ -185,19 +147,12 @@ export function ArrowFunctionExpression(node: Object, parent: Object): boolean { } export function ConditionalExpression(node: Object, parent: Object): boolean { - if (t.isUnaryLike(parent)) { - return true; - } - - if (t.isBinary(parent)) { - return true; - } - - if (t.isConditionalExpression(parent, { test: node })) { - return true; - } - - if (t.isAwaitExpression(parent)) { + if ( + t.isUnaryLike(parent) || + t.isBinary(parent) || + t.isConditionalExpression(parent, { test: node }) || + t.isAwaitExpression(parent) + ) { return true; } @@ -223,28 +178,23 @@ function isFirstInStatement(printStack: Array, { i--; let parent = printStack[i]; while (i > 0) { - if (t.isExpressionStatement(parent, { expression: node })) { - return true; - } - - if (t.isTaggedTemplateExpression(parent)) { - return true; - } - - if (considerDefaultExports && t.isExportDefaultDeclaration(parent, { declaration: node })) { - return true; - } - - if (considerArrow && t.isArrowFunctionExpression(parent, { body: node })) { + if ( + t.isExpressionStatement(parent, { expression: node }) || + t.isTaggedTemplateExpression(parent) || + considerDefaultExports && t.isExportDefaultDeclaration(parent, { declaration: node }) || + considerArrow && t.isArrowFunctionExpression(parent, { body: node }) + ) { return true; } - if ((t.isCallExpression(parent, { callee: node })) || - (t.isSequenceExpression(parent) && parent.expressions[0] === node) || - (t.isMemberExpression(parent, { object: node })) || - (t.isConditional(parent, { test: node })) || - (t.isBinary(parent, { left: node })) || - (t.isAssignmentExpression(parent, { left: node }))) { + if ( + t.isCallExpression(parent, { callee: node }) || + (t.isSequenceExpression(parent) && parent.expressions[0] === node) || + t.isMemberExpression(parent, { object: node }) || + t.isConditional(parent, { test: node }) || + t.isBinary(parent, { left: node }) || + t.isAssignmentExpression(parent, { left: node }) + ) { node = parent; i--; parent = printStack[i]; diff --git a/packages/babel-generator/src/printer.js b/packages/babel-generator/src/printer.js index 6d164fc2fece..ccb7576cd75b 100644 --- a/packages/babel-generator/src/printer.js +++ b/packages/babel-generator/src/printer.js @@ -330,9 +330,11 @@ export default class Printer { this._maybeAddAuxComment(this._insideAux && !oldInAux); let needsParens = n.needsParens(node, parent, this._printStack); - if (this.format.retainFunctionParens && - node.type === "FunctionExpression" && - node.extra && node.extra.parenthesized) { + if ( + this.format.retainFunctionParens && + node.type === "FunctionExpression" && + node.extra && node.extra.parenthesized + ) { needsParens = true; } if (needsParens) this.token("("); diff --git a/packages/babel-generator/src/whitespace.js b/packages/babel-generator/src/whitespace.js index 6a055e02e265..aabe0d123a0d 100644 --- a/packages/babel-generator/src/whitespace.js +++ b/packages/babel-generator/src/whitespace.js @@ -5,7 +5,7 @@ export default class Whitespace { constructor(tokens) { this.tokens = tokens; - this.used = {}; + this.used = {}; } /** @@ -59,7 +59,7 @@ export default class Whitespace { if (!endToken || !endToken.loc) return 0; const start = startToken ? startToken.loc.end.line : 1; - const end = endToken.loc.start.line; + const end = endToken.loc.start.line; let lines = 0; for (let line = start; line < end; line++) { diff --git a/packages/babel-generator/test/fixtures/parentheses/await-expression/actual.js b/packages/babel-generator/test/fixtures/parentheses/await-expression/actual.js index d4bdc6b74a8d..828926300767 100644 --- a/packages/babel-generator/test/fixtures/parentheses/await-expression/actual.js +++ b/packages/babel-generator/test/fixtures/parentheses/await-expression/actual.js @@ -5,6 +5,7 @@ async function asdf() { true ? (await 1) : (await 2); await (1 ? 2 : 3); await (await 1); + await (a || b); } async function a(b) { diff --git a/packages/babel-generator/test/fixtures/parentheses/await-expression/expected.js b/packages/babel-generator/test/fixtures/parentheses/await-expression/expected.js index dc2cb361e0c6..271a15791e19 100644 --- a/packages/babel-generator/test/fixtures/parentheses/await-expression/expected.js +++ b/packages/babel-generator/test/fixtures/parentheses/await-expression/expected.js @@ -5,6 +5,7 @@ async function asdf() { true ? await 1 : await 2; await (1 ? 2 : 3); await await 1; + await (a || b); } async function a(b) { diff --git a/packages/babel-generator/test/fixtures/parentheses/do-expression/actual.js b/packages/babel-generator/test/fixtures/parentheses/do-expression/actual.js new file mode 100644 index 000000000000..21177fdcef23 --- /dev/null +++ b/packages/babel-generator/test/fixtures/parentheses/do-expression/actual.js @@ -0,0 +1,3 @@ +(do { + foo; +}); diff --git a/packages/babel-generator/test/fixtures/parentheses/do-expression/expected.js b/packages/babel-generator/test/fixtures/parentheses/do-expression/expected.js new file mode 100644 index 000000000000..21177fdcef23 --- /dev/null +++ b/packages/babel-generator/test/fixtures/parentheses/do-expression/expected.js @@ -0,0 +1,3 @@ +(do { + foo; +}); diff --git a/packages/babel-generator/test/fixtures/types/DoExpression/actual.js b/packages/babel-generator/test/fixtures/types/DoExpression/actual.js new file mode 100644 index 000000000000..d6ec00f88937 --- /dev/null +++ b/packages/babel-generator/test/fixtures/types/DoExpression/actual.js @@ -0,0 +1,7 @@ +let a = do { + if (x > 10) { + 'big'; + } else { + 'small'; + } +}; diff --git a/packages/babel-generator/test/fixtures/types/DoExpression/expected.js b/packages/babel-generator/test/fixtures/types/DoExpression/expected.js new file mode 100644 index 000000000000..d6ec00f88937 --- /dev/null +++ b/packages/babel-generator/test/fixtures/types/DoExpression/expected.js @@ -0,0 +1,7 @@ +let a = do { + if (x > 10) { + 'big'; + } else { + 'small'; + } +}; diff --git a/packages/babel-generator/test/fixtures/types/LogicalExpression/actual.js b/packages/babel-generator/test/fixtures/types/LogicalExpression/actual.js new file mode 100644 index 000000000000..5df2b0728de3 --- /dev/null +++ b/packages/babel-generator/test/fixtures/types/LogicalExpression/actual.js @@ -0,0 +1,2 @@ +foo ||bar; +(x => x)|| bar; diff --git a/packages/babel-generator/test/fixtures/types/LogicalExpression/expected.js b/packages/babel-generator/test/fixtures/types/LogicalExpression/expected.js new file mode 100644 index 000000000000..91f0b20ce2b2 --- /dev/null +++ b/packages/babel-generator/test/fixtures/types/LogicalExpression/expected.js @@ -0,0 +1,2 @@ +foo || bar; +(x => x) || bar; diff --git a/packages/babel-helper-builder-react-jsx/package.json b/packages/babel-helper-builder-react-jsx/package.json index 6fed8d9b0d9d..92fb33891d60 100644 --- a/packages/babel-helper-builder-react-jsx/package.json +++ b/packages/babel-helper-builder-react-jsx/package.json @@ -8,7 +8,6 @@ "dependencies": { "babel-runtime": "^6.22.0", "babel-types": "^6.23.0", - "esutils": "^2.0.0", - "lodash": "^4.2.0" + "esutils": "^2.0.0" } } diff --git a/packages/babel-helper-plugin-test-runner/package.json b/packages/babel-helper-plugin-test-runner/package.json index c8b0dfa1f28f..8e305b69649b 100644 --- a/packages/babel-helper-plugin-test-runner/package.json +++ b/packages/babel-helper-plugin-test-runner/package.json @@ -1,12 +1,12 @@ { "name": "babel-helper-plugin-test-runner", - "version": "6.22.0", + "version": "6.24.0", "description": "Helper function to support test runner", "repository": "https://github.com/babel/babel/tree/master/packages/babel-helper-plugin-test-runner", "license": "MIT", "main": "lib/index.js", "dependencies": { "babel-runtime": "^6.22.0", - "babel-helper-transform-fixture-test-runner": "^6.22.0" + "babel-helper-transform-fixture-test-runner": "^6.24.0" } } diff --git a/packages/babel-helper-transform-fixture-test-runner/package.json b/packages/babel-helper-transform-fixture-test-runner/package.json index 6b4b60f922b7..1f3b785bf573 100644 --- a/packages/babel-helper-transform-fixture-test-runner/package.json +++ b/packages/babel-helper-transform-fixture-test-runner/package.json @@ -1,6 +1,6 @@ { "name": "babel-helper-transform-fixture-test-runner", - "version": "6.23.0", + "version": "6.24.0", "description": "Transform test runner for babel-helper-fixtures module", "author": "Sebastian McKenzie ", "homepage": "https://babeljs.io/", @@ -9,7 +9,7 @@ "main": "lib/index.js", "dependencies": { "babel-runtime": "^6.22.0", - "babel-core": "^6.23.0", + "babel-core": "^6.24.0", "babel-polyfill": "^6.23.0", "babel-helper-fixtures": "^6.22.0", "source-map": "^0.5.0", diff --git a/packages/babel-plugin-transform-async-to-module-method/README.md b/packages/babel-plugin-transform-async-to-module-method/README.md index 268271a16398..116da364f5d1 100644 --- a/packages/babel-plugin-transform-async-to-module-method/README.md +++ b/packages/babel-plugin-transform-async-to-module-method/README.md @@ -34,13 +34,17 @@ npm install --save-dev babel-plugin-transform-async-to-module-method **.babelrc** -```js -// without options +Without options: + +```json { "plugins": ["transform-async-to-module-method"] } +``` + +With options: -// with options +```json { "plugins": [ ["transform-async-to-module-method", { diff --git a/packages/babel-plugin-transform-es2015-arrow-functions/README.md b/packages/babel-plugin-transform-es2015-arrow-functions/README.md index 658f413f6cb2..9a5ab5b2f5f3 100644 --- a/packages/babel-plugin-transform-es2015-arrow-functions/README.md +++ b/packages/babel-plugin-transform-es2015-arrow-functions/README.md @@ -27,12 +27,12 @@ console.log(bob.printFriends()); **Out** ```javascript -var a = function a() {}; -var a = function a(b) { +var a = function () {}; +var a = function (b) { return b; }; -var double = [1, 2, 3].map(function (num) { +const double = [1, 2, 3].map(function (num) { return num * 2; }); console.log(double); // [2,4,6] @@ -40,7 +40,7 @@ console.log(double); // [2,4,6] var bob = { _name: "Bob", _friends: ["Sally", "Tom"], - printFriends: function printFriends() { + printFriends() { var _this = this; this._friends.forEach(function (f) { diff --git a/packages/babel-plugin-transform-es2015-computed-properties/README.md b/packages/babel-plugin-transform-es2015-computed-properties/README.md index 573996ff5b9b..4a7c74d026fb 100644 --- a/packages/babel-plugin-transform-es2015-computed-properties/README.md +++ b/packages/babel-plugin-transform-es2015-computed-properties/README.md @@ -57,13 +57,17 @@ npm install --save-dev babel-plugin-transform-es2015-computed-properties **.babelrc** -```js -// without options +Without options: + +```json { "plugins": ["transform-es2015-computed-properties"] } +``` + +With options: -// with options +```json { "plugins": [ ["transform-es2015-computed-properties", { diff --git a/packages/babel-plugin-transform-es2015-for-of/README.md b/packages/babel-plugin-transform-es2015-for-of/README.md index 7aff9a00f2e8..3e9efe20d90b 100644 --- a/packages/babel-plugin-transform-es2015-for-of/README.md +++ b/packages/babel-plugin-transform-es2015-for-of/README.md @@ -49,13 +49,17 @@ npm install --save-dev babel-plugin-transform-es2015-for-of **.babelrc** +Without options: + ```js -// without options { "plugins": ["transform-es2015-for-of"] } +``` + +With options: -// with options +```json { "plugins": [ ["transform-es2015-for-of", { diff --git a/packages/babel-plugin-transform-es2015-function-name/test/fixtures/function-name/await/actual.js b/packages/babel-plugin-transform-es2015-function-name/test/fixtures/function-name/await/actual.js new file mode 100644 index 000000000000..8f9c647db8ff --- /dev/null +++ b/packages/babel-plugin-transform-es2015-function-name/test/fixtures/function-name/await/actual.js @@ -0,0 +1,3 @@ +export {}; + +var obj = { await: function () {} }; diff --git a/packages/babel-plugin-transform-es2015-function-name/test/fixtures/function-name/await/expected.js b/packages/babel-plugin-transform-es2015-function-name/test/fixtures/function-name/await/expected.js new file mode 100644 index 000000000000..4289b26aa575 --- /dev/null +++ b/packages/babel-plugin-transform-es2015-function-name/test/fixtures/function-name/await/expected.js @@ -0,0 +1,3 @@ +export {}; + +var obj = { await: function _await() {} }; \ No newline at end of file diff --git a/packages/babel-plugin-transform-es2015-modules-amd/README.md b/packages/babel-plugin-transform-es2015-modules-amd/README.md index 04455655accf..bf54c82a7579 100644 --- a/packages/babel-plugin-transform-es2015-modules-amd/README.md +++ b/packages/babel-plugin-transform-es2015-modules-amd/README.md @@ -55,3 +55,7 @@ require("babel-core").transform("code", { plugins: ["transform-es2015-modules-amd"] }); ``` + +### Options + +See options for `babel-plugin-transform-es2015-commonjs`. diff --git a/packages/babel-plugin-transform-es2015-modules-amd/package.json b/packages/babel-plugin-transform-es2015-modules-amd/package.json index 81560058ad47..d25079b7a3d2 100644 --- a/packages/babel-plugin-transform-es2015-modules-amd/package.json +++ b/packages/babel-plugin-transform-es2015-modules-amd/package.json @@ -1,12 +1,12 @@ { "name": "babel-plugin-transform-es2015-modules-amd", - "version": "6.22.0", + "version": "6.24.0", "description": "This plugin transforms ES2015 modules to AMD", "repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-es2015-modules-amd", "license": "MIT", "main": "lib/index.js", "dependencies": { - "babel-plugin-transform-es2015-modules-commonjs": "^6.22.0", + "babel-plugin-transform-es2015-modules-commonjs": "^6.24.0", "babel-template": "^6.22.0", "babel-runtime": "^6.22.0" }, @@ -14,6 +14,6 @@ "babel-plugin" ], "devDependencies": { - "babel-helper-plugin-test-runner": "^6.22.0" + "babel-helper-plugin-test-runner": "^6.24.0" } } diff --git a/packages/babel-plugin-transform-es2015-modules-amd/test/fixtures/amd/noInterop-export-from/actual.js b/packages/babel-plugin-transform-es2015-modules-amd/test/fixtures/amd/noInterop-export-from/actual.js new file mode 100644 index 000000000000..7d2c9b022e26 --- /dev/null +++ b/packages/babel-plugin-transform-es2015-modules-amd/test/fixtures/amd/noInterop-export-from/actual.js @@ -0,0 +1 @@ +export { default } from 'foo'; diff --git a/packages/babel-plugin-transform-es2015-modules-amd/test/fixtures/amd/noInterop-export-from/expected.js b/packages/babel-plugin-transform-es2015-modules-amd/test/fixtures/amd/noInterop-export-from/expected.js new file mode 100644 index 000000000000..cd5f804ab644 --- /dev/null +++ b/packages/babel-plugin-transform-es2015-modules-amd/test/fixtures/amd/noInterop-export-from/expected.js @@ -0,0 +1,13 @@ +define(['exports', 'foo'], function (exports, _foo) { + 'use strict'; + + Object.defineProperty(exports, "__esModule", { + value: true + }); + Object.defineProperty(exports, 'default', { + enumerable: true, + get: function () { + return _foo.default; + } + }); +}); diff --git a/packages/babel-plugin-transform-es2015-modules-amd/test/fixtures/amd/noInterop-export-from/options.json b/packages/babel-plugin-transform-es2015-modules-amd/test/fixtures/amd/noInterop-export-from/options.json new file mode 100644 index 000000000000..096d2b9404ce --- /dev/null +++ b/packages/babel-plugin-transform-es2015-modules-amd/test/fixtures/amd/noInterop-export-from/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["external-helpers", ["transform-es2015-modules-amd", { "noInterop": true }]] +} diff --git a/packages/babel-plugin-transform-es2015-modules-amd/test/fixtures/amd/noInterop-import-default-only/actual.js b/packages/babel-plugin-transform-es2015-modules-amd/test/fixtures/amd/noInterop-import-default-only/actual.js new file mode 100644 index 000000000000..44567cb185be --- /dev/null +++ b/packages/babel-plugin-transform-es2015-modules-amd/test/fixtures/amd/noInterop-import-default-only/actual.js @@ -0,0 +1,3 @@ +import foo from "foo"; + +foo; diff --git a/packages/babel-plugin-transform-es2015-modules-amd/test/fixtures/amd/noInterop-import-default-only/expected.js b/packages/babel-plugin-transform-es2015-modules-amd/test/fixtures/amd/noInterop-import-default-only/expected.js new file mode 100644 index 000000000000..acdfb3b580f5 --- /dev/null +++ b/packages/babel-plugin-transform-es2015-modules-amd/test/fixtures/amd/noInterop-import-default-only/expected.js @@ -0,0 +1,5 @@ +define(["foo"], function (_foo) { + "use strict"; + + _foo.default; +}); diff --git a/packages/babel-plugin-transform-es2015-modules-amd/test/fixtures/amd/noInterop-import-default-only/options.json b/packages/babel-plugin-transform-es2015-modules-amd/test/fixtures/amd/noInterop-import-default-only/options.json new file mode 100644 index 000000000000..096d2b9404ce --- /dev/null +++ b/packages/babel-plugin-transform-es2015-modules-amd/test/fixtures/amd/noInterop-import-default-only/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["external-helpers", ["transform-es2015-modules-amd", { "noInterop": true }]] +} diff --git a/packages/babel-plugin-transform-es2015-modules-commonjs/README.md b/packages/babel-plugin-transform-es2015-modules-commonjs/README.md index 3a802ab57173..bdad88967004 100644 --- a/packages/babel-plugin-transform-es2015-modules-commonjs/README.md +++ b/packages/babel-plugin-transform-es2015-modules-commonjs/README.md @@ -82,10 +82,53 @@ Object.defineProperty(exports, "__esModule", { }); ``` -In environments that don't support this you can enable loose mode on `es6.modules` +In environments that don't support this you can enable loose mode on `babel-plugin-transform-es2015-modules-commonjs` and instead of using `Object.defineProperty` an assignment will be used instead. ```javascript var foo = exports.foo = 5; exports.__esModule = true; ``` + +### `strict` + +`boolean`, defaults to `false` + +By default, when using exports with babel a non-enumerable `__esModule` property +is exported. In some cases this property is used to determine if the import _is_ the +default export or if it _contains_ the default export. + +```javascript +var foo = exports.foo = 5; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +``` + +In order to prevent the `__esModule` property from being exported, you can set +the `strict` option to `true`. + +### `noInterop` + +`boolean`, defaults to `false` + +By default, when using exports with babel a non-enumerable `__esModule` property +is exported. This property is then used to determine if the import _is_ the default +export or if it _contains_ the default export. + +```javascript +"use strict"; + +var _foo = require("foo"); + +var _foo2 = _interopRequireDefault(_foo); + +function _interopRequireDefault(obj) { + return obj && obj.__esModule ? obj : { default: obj }; +} +``` + +In cases where the auto-unwrapping of `default` is not needed, you can set the +`noInterop` option to `true` to avoid the usage of the `interopRequireDefault` +helper (shown in inline form above). diff --git a/packages/babel-plugin-transform-es2015-modules-commonjs/package.json b/packages/babel-plugin-transform-es2015-modules-commonjs/package.json index 574befaf5f48..148504224f91 100644 --- a/packages/babel-plugin-transform-es2015-modules-commonjs/package.json +++ b/packages/babel-plugin-transform-es2015-modules-commonjs/package.json @@ -1,6 +1,6 @@ { "name": "babel-plugin-transform-es2015-modules-commonjs", - "version": "6.23.0", + "version": "6.24.0", "description": "This plugin transforms ES2015 modules to CommonJS", "repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-es2015-modules-commonjs", "license": "MIT", @@ -15,6 +15,6 @@ "babel-plugin" ], "devDependencies": { - "babel-helper-plugin-test-runner": "^6.22.0" + "babel-helper-plugin-test-runner": "^6.24.0" } } 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 74af212c55db..2afc2d6fbbf2 100644 --- a/packages/babel-plugin-transform-es2015-modules-commonjs/src/index.js +++ b/packages/babel-plugin-transform-es2015-modules-commonjs/src/index.js @@ -150,6 +150,7 @@ export default function () { this.ranCommonJS = true; const strict = !!this.opts.strict; + const noInterop = !!this.opts.noInterop; const { scope } = path; @@ -326,7 +327,7 @@ export default function () { } else if (specifier.isExportDefaultSpecifier()) { // todo } else if (specifier.isExportSpecifier()) { - if (specifier.node.local.name === "default") { + if (!noInterop && specifier.node.local.name === "default") { topNodes.push(buildExportsFrom(t.stringLiteral(specifier.node.exported.name), t.memberExpression( t.callExpression(this.addHelper("interopRequireDefault"), [ref]), @@ -370,7 +371,7 @@ export default function () { for (let i = 0; i < specifiers.length; i++) { const specifier = specifiers[i]; if (t.isImportNamespaceSpecifier(specifier)) { - if (strict) { + if (strict || noInterop) { remaps[specifier.local.name] = uid; } else { const varDecl = t.variableDeclaration("var", [ @@ -401,7 +402,7 @@ export default function () { if (specifier.imported.name === "default") { if (wildcard) { target = wildcard; - } else { + } else if (!noInterop) { target = wildcard = path.scope.generateUidIdentifier(uid.name); const varDecl = t.variableDeclaration("var", [ t.variableDeclarator( diff --git a/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/noInterop/export-from/actual.js b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/noInterop/export-from/actual.js new file mode 100644 index 000000000000..7d2c9b022e26 --- /dev/null +++ b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/noInterop/export-from/actual.js @@ -0,0 +1 @@ +export { default } from 'foo'; diff --git a/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/noInterop/export-from/expected.js b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/noInterop/export-from/expected.js new file mode 100644 index 000000000000..c986dfedbbef --- /dev/null +++ b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/noInterop/export-from/expected.js @@ -0,0 +1,14 @@ +'use strict'; + +Object.defineProperty(exports, "__esModule", { + value: true +}); + +var _foo = require('foo'); + +Object.defineProperty(exports, 'default', { + enumerable: true, + get: function () { + return _foo.default; + } +}); diff --git a/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/noInterop/import-default-only/actual.js b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/noInterop/import-default-only/actual.js new file mode 100644 index 000000000000..65b75b7293b4 --- /dev/null +++ b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/noInterop/import-default-only/actual.js @@ -0,0 +1,3 @@ +import foo from "foo"; + +foo(); diff --git a/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/noInterop/import-default-only/expected.js b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/noInterop/import-default-only/expected.js new file mode 100644 index 000000000000..1bc59ef949a6 --- /dev/null +++ b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/noInterop/import-default-only/expected.js @@ -0,0 +1,5 @@ +"use strict"; + +var _foo = require("foo"); + +(0, _foo.default)(); diff --git a/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/noInterop/import-wildcard/actual.js b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/noInterop/import-wildcard/actual.js new file mode 100644 index 000000000000..bf67ef46a2d0 --- /dev/null +++ b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/noInterop/import-wildcard/actual.js @@ -0,0 +1,4 @@ +import * as foo from 'foo'; + +foo.bar(); +foo.baz(); diff --git a/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/noInterop/import-wildcard/expected.js b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/noInterop/import-wildcard/expected.js new file mode 100644 index 000000000000..284db9decde0 --- /dev/null +++ b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/noInterop/import-wildcard/expected.js @@ -0,0 +1,6 @@ +'use strict'; + +var _foo = require('foo'); + +_foo.bar(); +_foo.baz(); diff --git a/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/noInterop/options.json b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/noInterop/options.json new file mode 100644 index 000000000000..b2a35b9fc8cf --- /dev/null +++ b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/noInterop/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["external-helpers", ["transform-es2015-modules-commonjs", { "noInterop": true }]] +} diff --git a/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/import-wildcard/actual.js b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/import-wildcard/actual.js new file mode 100644 index 000000000000..bf67ef46a2d0 --- /dev/null +++ b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/import-wildcard/actual.js @@ -0,0 +1,4 @@ +import * as foo from 'foo'; + +foo.bar(); +foo.baz(); diff --git a/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/import-wildcard/expected.js b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/import-wildcard/expected.js new file mode 100644 index 000000000000..284db9decde0 --- /dev/null +++ b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/import-wildcard/expected.js @@ -0,0 +1,6 @@ +'use strict'; + +var _foo = require('foo'); + +_foo.bar(); +_foo.baz(); diff --git a/packages/babel-plugin-transform-es2015-modules-systemjs/README.md b/packages/babel-plugin-transform-es2015-modules-systemjs/README.md index ca205005d9be..554421de63b8 100644 --- a/packages/babel-plugin-transform-es2015-modules-systemjs/README.md +++ b/packages/babel-plugin-transform-es2015-modules-systemjs/README.md @@ -35,13 +35,17 @@ npm install --save-dev babel-plugin-transform-es2015-modules-systemjs **.babelrc** -```javascript -// without options +Without options: + +```json { "plugins": ["transform-es2015-modules-systemjs"] } +``` + +With options: -// with options +```json { "plugins": [ ["transform-es2015-modules-systemjs", { diff --git a/packages/babel-plugin-transform-es2015-modules-umd/package.json b/packages/babel-plugin-transform-es2015-modules-umd/package.json index 8c424e9f1e20..15e7dd4244ef 100644 --- a/packages/babel-plugin-transform-es2015-modules-umd/package.json +++ b/packages/babel-plugin-transform-es2015-modules-umd/package.json @@ -1,12 +1,12 @@ { "name": "babel-plugin-transform-es2015-modules-umd", - "version": "6.23.0", + "version": "6.24.0", "description": "This plugin transforms ES2015 modules to UMD", "repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-es2015-modules-umd", "license": "MIT", "main": "lib/index.js", "dependencies": { - "babel-plugin-transform-es2015-modules-amd": "^6.22.0", + "babel-plugin-transform-es2015-modules-amd": "^6.24.0", "babel-template": "^6.23.0", "babel-runtime": "^6.22.0" }, @@ -14,6 +14,6 @@ "babel-plugin" ], "devDependencies": { - "babel-helper-plugin-test-runner": "^6.22.0" + "babel-helper-plugin-test-runner": "^6.24.0" } } diff --git a/packages/babel-plugin-transform-es2015-shorthand-properties/README.md b/packages/babel-plugin-transform-es2015-shorthand-properties/README.md index 171aa1ead7c9..95c87e2fa320 100644 --- a/packages/babel-plugin-transform-es2015-shorthand-properties/README.md +++ b/packages/babel-plugin-transform-es2015-shorthand-properties/README.md @@ -13,7 +13,7 @@ var o = { a, b, c }; **Out** ```js -var o = { a: a, b: b, c:c }; +var o = { a: a, b: b, c: c }; ``` **In** diff --git a/packages/babel-plugin-transform-es2015-spread/README.md b/packages/babel-plugin-transform-es2015-spread/README.md index af75353e36df..ad1fd70d1683 100644 --- a/packages/babel-plugin-transform-es2015-spread/README.md +++ b/packages/babel-plugin-transform-es2015-spread/README.md @@ -9,31 +9,13 @@ ```js var a = ['a', 'b', 'c']; var b = [...a, 'foo']; - -var c = { foo: 'bar', baz: 42 }; -var d = {...c, a: 2}; ``` **Out** ```js -var _extends = Object.assign || function (target) { - for (var i = 1; i < arguments.length; i++) { - var source = arguments[i]; - for (var key in source) { - if (Object.prototype.hasOwnProperty.call(source, key)) { - target[key] = source[key]; - } - } - } - return target; -} - var a = [ 'a', 'b', 'c' ]; var b = [].concat(a, [ 'foo' ]); - -var c = { foo: 'bar', baz: 42 }; -var d = _extends({}, c, { a: 2 }); ``` ## Installation @@ -48,13 +30,17 @@ npm install --save-dev babel-plugin-transform-es2015-spread **.babelrc** -```js -// without options +Without options: + +```json { "plugins": ["transform-es2015-spread"] } +``` + +With options: -// with options +```json { "plugins": [ ["transform-es2015-spread", { diff --git a/packages/babel-plugin-transform-es2015-template-literals/README.md b/packages/babel-plugin-transform-es2015-template-literals/README.md index fd0f57bf1898..477b11cce60b 100644 --- a/packages/babel-plugin-transform-es2015-template-literals/README.md +++ b/packages/babel-plugin-transform-es2015-template-literals/README.md @@ -28,13 +28,17 @@ npm install --save-dev babel-plugin-transform-es2015-template-literals **.babelrc** -```js -// without options +Without options: + +```json { "plugins": ["transform-es2015-template-literals"] } +``` + +With options: -// with options +```json { "plugins": [ ["transform-es2015-template-literals", { diff --git a/packages/babel-plugin-transform-object-rest-spread/README.md b/packages/babel-plugin-transform-object-rest-spread/README.md index b8f365d7afe8..79f2432d03f8 100644 --- a/packages/babel-plugin-transform-object-rest-spread/README.md +++ b/packages/babel-plugin-transform-object-rest-spread/README.md @@ -4,19 +4,22 @@ ## Example +### Rest Properties + ```js -// Rest properties let { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 }; console.log(x); // 1 console.log(y); // 2 console.log(z); // { a: 3, b: 4 } +``` + +### Spread Properties -// Spread properties +```js let n = { x, y, ...z }; console.log(n); // { x: 1, y: 2, a: 3, b: 4 } ``` - ## Installation ```sh @@ -35,37 +38,48 @@ npm install --save-dev babel-plugin-transform-object-rest-spread } ``` +### Via CLI + +```sh +babel --plugins transform-object-rest-spread script.js +``` + +### Via Node API + +```javascript +require("babel-core").transform("code", { + plugins: ["transform-object-rest-spread"] +}); +``` + ## Options -This plugin will use babel's `extends` helper, which will polyfill `Object.assign` by default. +### `useBuiltIns` -* `useBuiltIns` - Do not use Babel's helper's and just transform to use the built-in method (Disabled by default). +`boolean`, defaults to `false`. -```js +By default, this plugin uses Babel's `extends` helper which polyfills `Object.assign`. Enabling this option will use `Object.assign` directly. + +**.babelrc** + +```json { "plugins": [ ["transform-object-rest-spread", { "useBuiltIns": true }] ] } - -// source -z = { x, ...y }; -// compiled -z = Object.assign({ x }, y); ``` -### Via CLI +**In** -```sh -babel --plugins transform-object-rest-spread script.js +```js +z = { x, ...y }; ``` -### Via Node API +**Out** -```javascript -require("babel-core").transform("code", { - plugins: ["transform-object-rest-spread"] -}); +```js +z = Object.assign({ x }, y); ``` ## References diff --git a/packages/babel-plugin-transform-react-jsx/README.md b/packages/babel-plugin-transform-react-jsx/README.md index 44a20066dd3b..128536db1eac 100644 --- a/packages/babel-plugin-transform-react-jsx/README.md +++ b/packages/babel-plugin-transform-react-jsx/README.md @@ -64,12 +64,17 @@ npm install --save-dev babel-plugin-transform-react-jsx **.babelrc** -```js -// without options +Without options: + +```json { "plugins": ["transform-react-jsx"] } -// with options +``` + +With options: + +```json { "plugins": [ ["transform-react-jsx", { diff --git a/packages/babel-plugin-transform-runtime/README.md b/packages/babel-plugin-transform-runtime/README.md index ff42d7e9af98..2ab8ae89204f 100644 --- a/packages/babel-plugin-transform-runtime/README.md +++ b/packages/babel-plugin-transform-runtime/README.md @@ -1,8 +1,8 @@ # babel-plugin-transform-runtime -> Externalise references to helpers and builtins, automatically polyfilling your code without polluting globals. (This plugin is recommended in a library/tool) +> Externalise references to helpers and built-ins, automatically polyfilling your code without polluting globals. (This plugin is recommended in a library/tool) -NOTE: Instance methods such as `"foobar".includes("foo")` will not work since that would require modification of existing builtins (Use [`babel-polyfill`](http://babeljs.io/docs/usage/polyfill) for that). +NOTE: Instance methods such as `"foobar".includes("foo")` will not work since that would require modification of existing built-ins (Use [`babel-polyfill`](http://babeljs.io/docs/usage/polyfill) for that). ## Why? @@ -40,20 +40,24 @@ The transformation plugin is typically used only in development, but the runtime Add the following line to your `.babelrc` file: -```js -// without options +Without options: + +```json { "plugins": ["transform-runtime"] } +``` + +With options: -// with options +```json { "plugins": [ ["transform-runtime", { - "helpers": false, // defaults to true - "polyfill": false, // defaults to true - "regenerator": true, // defaults to true - "moduleName": "babel-runtime" // defaults to "babel-runtime" + "helpers": false, + "polyfill": false, + "regenerator": true, + "moduleName": "babel-runtime" }] ] } @@ -73,15 +77,59 @@ require("babel-core").transform("code", { }); ``` +## Options + +### `helpers` + +`boolean`, defaults to `true`. + +Toggles whether or not inlined Babel helpers (`classCallCheck`, `extends`, etc.) are replaced with calls to `moduleName`. + +For more information, see [Helper aliasing](#helper-aliasing). + +### `polyfill` + +`boolean`, defaults to `true`. + +Toggles whether or not new built-ins (`Promise`, `Set`, `Map`, etc.) are transformed to use a non-global polluting polyfill. + +For more information, see [`core-js` aliasing](#core-js-aliasing). + +### `regenerator` + +`boolean`, defaults to `true`. + +Toggles whether or not generator functions are transformed to use a regenerator runtime that does not pollute the global scope. + +For more information, see [Regenerator aliasing](#regenerator-aliasing). + +### `moduleName` + +`string`, defaults to `"babel-runtime"`. + +Sets the name/path of the module used when importing helpers. + +Example: + +```json +{ + "moduleName": "flavortown/runtime" +} +``` + +```js +import extends from 'flavortown/runtime/helpers/extends'; +``` + ## Technical details The `runtime` transformer plugin does three things: * Automatically requires `babel-runtime/regenerator` when you use generators/async functions. * Automatically requires `babel-runtime/core-js` and maps ES6 static methods and built-ins. -* Removes the inline babel helpers and uses the module `babel-runtime/helpers` instead. +* Removes the inline Babel helpers and uses the module `babel-runtime/helpers` instead. -What does this actually mean though? Basically, you can use built-ins such as `Promise`, `Set`, `Symbol` etc as well use all the Babel features that require a polyfill seamlessly, without global pollution, making it extremely suitable for libraries. +What does this actually mean though? Basically, you can use built-ins such as `Promise`, `Set`, `Symbol`, etc., as well use all the Babel features that require a polyfill seamlessly, without global pollution, making it extremely suitable for libraries. Make sure you include `babel-runtime` as a dependency. @@ -190,7 +238,7 @@ without worrying about where they come from. ### Helper aliasing -Usually babel will place helpers at the top of your file to do common tasks to avoid +Usually Babel will place helpers at the top of your file to do common tasks to avoid duplicating the code around in the current file. Sometimes these helpers can get a little bulky and add unnecessary duplication across files. The `runtime` transformer replaces all the helper calls to a module. diff --git a/packages/babel-plugin-transform-strict-mode/README.md b/packages/babel-plugin-transform-strict-mode/README.md index d6c258d3008a..08c5afe40508 100644 --- a/packages/babel-plugin-transform-strict-mode/README.md +++ b/packages/babel-plugin-transform-strict-mode/README.md @@ -34,13 +34,17 @@ npm install --save-dev babel-plugin-transform-strict-mode **.babelrc** -```js -// without options +Without options: + +```json { "plugins": ["transform-strict-mode"] } +``` + +With options: -// with options +```json { "plugins": [ ["transform-strict-mode", { diff --git a/packages/babel-preset-es2015/package.json b/packages/babel-preset-es2015/package.json index 78d66ad21726..15cccddece09 100644 --- a/packages/babel-preset-es2015/package.json +++ b/packages/babel-preset-es2015/package.json @@ -1,6 +1,6 @@ { "name": "babel-preset-es2015", - "version": "6.22.0", + "version": "6.24.0", "description": "Babel preset for all es2015 plugins.", "author": "Sebastian McKenzie ", "homepage": "https://babeljs.io/", @@ -19,10 +19,10 @@ "babel-plugin-transform-es2015-for-of": "^6.22.0", "babel-plugin-transform-es2015-function-name": "^6.22.0", "babel-plugin-transform-es2015-literals": "^6.22.0", - "babel-plugin-transform-es2015-modules-amd": "^6.22.0", - "babel-plugin-transform-es2015-modules-commonjs": "^6.22.0", + "babel-plugin-transform-es2015-modules-amd": "^6.24.0", + "babel-plugin-transform-es2015-modules-commonjs": "^6.24.0", "babel-plugin-transform-es2015-modules-systemjs": "^6.22.0", - "babel-plugin-transform-es2015-modules-umd": "^6.22.0", + "babel-plugin-transform-es2015-modules-umd": "^6.24.0", "babel-plugin-transform-es2015-object-super": "^6.22.0", "babel-plugin-transform-es2015-parameters": "^6.22.0", "babel-plugin-transform-es2015-shorthand-properties": "^6.22.0", @@ -34,7 +34,7 @@ "babel-plugin-transform-regenerator": "^6.22.0" }, "devDependencies": { - "babel-helper-transform-fixture-test-runner": "^6.22.0", - "babel-helper-plugin-test-runner": "^6.22.0" + "babel-helper-transform-fixture-test-runner": "^6.24.0", + "babel-helper-plugin-test-runner": "^6.24.0" } } diff --git a/packages/babel-preset-latest/README.md b/packages/babel-preset-latest/README.md index 056d95645ded..ec4e16769dc5 100644 --- a/packages/babel-preset-latest/README.md +++ b/packages/babel-preset-latest/README.md @@ -42,7 +42,7 @@ require("babel-core").transform("code", { Toggles including plugins from the [es2015 preset](https://babeljs.io/docs/plugins/preset-es2015/). -```js +```json { "presets": [ ["latest", { @@ -54,7 +54,7 @@ Toggles including plugins from the [es2015 preset](https://babeljs.io/docs/plugi You can also pass options down to the `es2015` preset. -```js +```json { "presets": [ ["latest", { diff --git a/packages/babel-preset-latest/package.json b/packages/babel-preset-latest/package.json index f5a306981de5..85791245c0f4 100644 --- a/packages/babel-preset-latest/package.json +++ b/packages/babel-preset-latest/package.json @@ -1,6 +1,6 @@ { "name": "babel-preset-latest", - "version": "6.22.0", + "version": "6.24.0", "description": "Babel preset including es2015+", "author": "Sebastian McKenzie ", "homepage": "https://babeljs.io/", @@ -8,11 +8,11 @@ "repository": "https://github.com/babel/babel/tree/master/packages/babel-preset-latest", "main": "lib/index.js", "dependencies": { - "babel-preset-es2015": "^6.22.0", + "babel-preset-es2015": "^6.24.0", "babel-preset-es2016": "^6.22.0", "babel-preset-es2017": "^6.22.0" }, "devDependencies": { - "babel-helper-plugin-test-runner": "^6.22.0" + "babel-helper-plugin-test-runner": "^6.24.0" } } diff --git a/packages/babel-register/README.md b/packages/babel-register/README.md index 2f05d6a7f716..413e7a6349e5 100644 --- a/packages/babel-register/README.md +++ b/packages/babel-register/README.md @@ -5,7 +5,7 @@ One of the ways you can use Babel is through the require hook. The require hook will bind itself to node's `require` and automatically compile files on the fly. This is equivalent to CoffeeScript's -[coffee-script/register](http://coffeescript.org/documentation/docs/register.html). +[coffee-script/register](http://coffeescript.org/v2/annotated-source/register.html). ## Install @@ -53,7 +53,7 @@ require("babel-register")({ // Ignore can also be specified as a function. ignore: function(filename) { - if (filename === '/path/to/es6-file.js') { + if (filename === "/path/to/es6-file.js") { return false; } else { return true; @@ -66,7 +66,10 @@ require("babel-register")({ // Setting this will remove the currently hooked extensions of .es6, `.es`, `.jsx` // and .js so you'll have to add them back if you want them to be used again. - extensions: [".es6", ".es", ".jsx", ".js"] + extensions: [".es6", ".es", ".jsx", ".js"], + + // Setting this to false will disable the cache. + cache: true }); ``` diff --git a/packages/babel-register/package.json b/packages/babel-register/package.json index 762d9a336071..2d1e7c766822 100644 --- a/packages/babel-register/package.json +++ b/packages/babel-register/package.json @@ -1,6 +1,6 @@ { "name": "babel-register", - "version": "6.23.0", + "version": "6.24.0", "description": "babel require hook", "license": "MIT", "repository": "https://github.com/babel/babel/tree/master/packages/babel-register", @@ -8,7 +8,7 @@ "main": "lib/node.js", "browser": "lib/browser.js", "dependencies": { - "babel-core": "^6.23.0", + "babel-core": "^6.24.0", "babel-runtime": "^6.22.0", "core-js": "^2.4.0", "home-or-tmp": "^2.0.0", diff --git a/packages/babel-template/README.md b/packages/babel-template/README.md index 9eeb0adf3f62..c73507af5945 100644 --- a/packages/babel-template/README.md +++ b/packages/babel-template/README.md @@ -13,24 +13,24 @@ npm install --save-dev babel-template ## Usage ```js -import template from 'babel-template'; -import generate from 'babel-generator'; -import * as t from 'babel-types'; +import template from "babel-template"; +import generate from "babel-generator"; +import * as t from "babel-types"; const buildRequire = template(` var IMPORT_NAME = require(SOURCE); `); const ast = buildRequire({ - IMPORT_NAME: t.identifier('myModule'), - SOURCE: t.stringLiteral('my-module') + IMPORT_NAME: t.identifier("myModule"), + SOURCE: t.stringLiteral("my-module") }); console.log(generate(ast).code); ``` ```js -var myModule = require('my-module'); +const myModule = require("my-module"); ``` ## API diff --git a/packages/babel-traverse/src/path/replacement.js b/packages/babel-traverse/src/path/replacement.js index 47e706d0d2bb..8ac89eaaf780 100644 --- a/packages/babel-traverse/src/path/replacement.js +++ b/packages/babel-traverse/src/path/replacement.js @@ -125,7 +125,8 @@ export function replaceWith(replacement) { if (this.isNodeType("Statement") && t.isExpression(replacement)) { if ( !this.canHaveVariableDeclarationOrExpression() && - !this.canSwapBetweenExpressionAndStatement(replacement) + !this.canSwapBetweenExpressionAndStatement(replacement) && + !this.parentPath.isExportDefaultDeclaration() ) { // replacing a statement with an expression so wrap it in an expression statement replacement = t.expressionStatement(replacement); diff --git a/packages/babel-traverse/test/evaluation.js b/packages/babel-traverse/test/evaluation.js index 148173fa7d01..4b0c01c0828d 100644 --- a/packages/babel-traverse/test/evaluation.js +++ b/packages/babel-traverse/test/evaluation.js @@ -61,6 +61,27 @@ describe("evaluation", function () { ); }); + it("should evaluate template literals", function () { + assert.strictEqual( + getPath("var x = 8; var y = 1; var z = `value is ${x >>> y}`") + .get("body.2.declarations.0.init").evaluate().value, + "value is 4" + ); + }); + + it("should evaluate member expressions", function () { + assert.strictEqual( + getPath("var x = 'foo'.length") + .get("body.0.declarations.0.init").evaluate().value, + 3 + ); + const member_expr = getPath("var x = Math.min(2,Math.max(3,4));var y = Math.random();"); + const eval_member_expr = member_expr.get("body.0.declarations.0.init").evaluate(); + const eval_invalid_call = member_expr.get("body.1.declarations.0.init").evaluate(); + assert.strictEqual(eval_member_expr.value, 2); + assert.strictEqual(eval_invalid_call.confident, false); + }); + it("it should not deopt vars in different scope", function () { const input = "var a = 5; function x() { var a = 5; var b = a + 1; } var b = a + 2"; assert.strictEqual( @@ -88,6 +109,12 @@ describe("evaluation", function () { getPath(constExample).get("body.1.consequent.body.1").evaluate().value, false ); + const test_alternate = "var y = (3 < 4)? 3 + 4: 3 + 4;"; + assert.strictEqual( + getPath(test_alternate) + .get("body.0.declarations.0.init.alternate").evaluate().value, + 7 + ); }); it("should deopt ids that are referenced before the bindings", function () { diff --git a/packages/babel-traverse/test/replacement.js b/packages/babel-traverse/test/replacement.js new file mode 100644 index 000000000000..eb29364f2fb4 --- /dev/null +++ b/packages/babel-traverse/test/replacement.js @@ -0,0 +1,28 @@ +import traverse from "../lib"; +import assert from "assert"; +import { parse } from "babylon"; +import * as t from "babel-types"; + +describe("path/replacement", function () { + describe("replaceWith", function () { + const ast = parse("export default function() {};", { sourceType: "module" }); + + it("replaces declaration in ExportDefaultDeclaration node", function() { + traverse(ast, { + FunctionDeclaration(path) { + path.replaceWith(t.arrayExpression([ + t.functionExpression( + path.node.id, + path.node.params, + path.node.body, + path.node.generator, + path.node.async + ), + ])); + }, + }); + + assert(ast.program.body[0].declaration.type == "ArrayExpression"); + }); + }); +}); diff --git a/packages/babel-types/src/validators.js b/packages/babel-types/src/validators.js index c5f25327bdbb..dc8143ae9d92 100644 --- a/packages/babel-types/src/validators.js +++ b/packages/babel-types/src/validators.js @@ -167,6 +167,9 @@ export function isReferenced(node: Object, parent: Object): boolean { export function isValidIdentifier(name: string): boolean { if (typeof name !== "string" || esutils.keyword.isReservedWordES6(name, true)) { return false; + } else if (name === "await") { + // invalid in module, valid in script; better be safe (see #4952) + return false; } else { return esutils.keyword.isIdentifierNameES6(name); } diff --git a/packages/babel-types/test/validators.js b/packages/babel-types/test/validators.js index 2e7e55d08567..ccc83f665b52 100644 --- a/packages/babel-types/test/validators.js +++ b/packages/babel-types/test/validators.js @@ -26,5 +26,9 @@ suite("validators", function () { assert(t.isNodesEquivalent(parse(program), parse(program2)) === false); }); + + it("rejects 'await' as an identifier", function () { + assert(t.isValidIdentifier("await") === false); + }); }); });