From bc9c1ab61e7de9b4aabea709c3979ba7ed59c698 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Mon, 16 Mar 2015 13:07:28 +1100 Subject: [PATCH 1/7] handle comments and use strict directives better - fixes #1030 --- .../transformation/transformers/internal/strict.js | 13 ++++++++++++- .../transformation/transformers/other/strict.js | 4 ++-- .../array-unpack-optimisation/expected.js | 2 +- .../es6-tail-call/ignore-reassigned/expected.js | 4 ++-- .../strict/leading-comments-with-existing/actual.js | 4 ++++ .../leading-comments-with-existing/expected.js | 4 ++++ .../strict/leading-comments/actual.js | 3 +++ .../strict/leading-comments/expected.js | 5 +++++ 8 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 test/fixtures/transformation/strict/leading-comments-with-existing/actual.js create mode 100644 test/fixtures/transformation/strict/leading-comments-with-existing/expected.js create mode 100644 test/fixtures/transformation/strict/leading-comments/actual.js create mode 100644 test/fixtures/transformation/strict/leading-comments/expected.js diff --git a/src/babel/transformation/transformers/internal/strict.js b/src/babel/transformation/transformers/internal/strict.js index 84a1ee0499..861988daae 100644 --- a/src/babel/transformation/transformers/internal/strict.js +++ b/src/babel/transformation/transformers/internal/strict.js @@ -2,6 +2,17 @@ import * as t from "../../../types"; export function Program(program, parent, scope, file) { if (file.transformers.strict.canRun()) { - program.body.unshift(t.expressionStatement(t.literal("use strict"))); + var directive = file.get("existingStrictDirective"); + + if (!directive) { + directive = t.expressionStatement(t.literal("use strict")); + var first = program.body[0]; + if (first) { + directive.leadingComments = first.leadingComments; + first.leadingComments = []; + } + } + + program.body.unshift(directive); } } diff --git a/src/babel/transformation/transformers/other/strict.js b/src/babel/transformation/transformers/other/strict.js index db7849059a..1ebde501e6 100644 --- a/src/babel/transformation/transformers/other/strict.js +++ b/src/babel/transformation/transformers/other/strict.js @@ -1,10 +1,10 @@ import * as messages from "../../../messages"; import * as t from "../../../types"; -export function Program(program) { +export function Program(program, parent, scope, file) { var first = program.body[0]; if (t.isExpressionStatement(first) && t.isLiteral(first.expression, { value: "use strict" })) { - program.body.shift(); + file.set("existingStrictDirective", program.body.shift()); } } diff --git a/test/fixtures/transformation/es6-destructuring/array-unpack-optimisation/expected.js b/test/fixtures/transformation/es6-destructuring/array-unpack-optimisation/expected.js index ad9f44331c..d1aaf9d67a 100644 --- a/test/fixtures/transformation/es6-destructuring/array-unpack-optimisation/expected.js +++ b/test/fixtures/transformation/es6-destructuring/array-unpack-optimisation/expected.js @@ -1,6 +1,6 @@ +// opt "use strict"; -// opt var a = 1; var b = 2; var a = 1; diff --git a/test/fixtures/transformation/es6-tail-call/ignore-reassigned/expected.js b/test/fixtures/transformation/es6-tail-call/ignore-reassigned/expected.js index f658848e82..66d333d054 100644 --- a/test/fixtures/transformation/es6-tail-call/ignore-reassigned/expected.js +++ b/test/fixtures/transformation/es6-tail-call/ignore-reassigned/expected.js @@ -1,8 +1,8 @@ -"use strict"; - // we need to deopt `test` if it's reassigned as we can't be certain of it's // state, ie. it could have been rebound or dereferenced +"use strict"; + function test(exit) { if (exit) { return this.x; diff --git a/test/fixtures/transformation/strict/leading-comments-with-existing/actual.js b/test/fixtures/transformation/strict/leading-comments-with-existing/actual.js new file mode 100644 index 0000000000..078ae074be --- /dev/null +++ b/test/fixtures/transformation/strict/leading-comments-with-existing/actual.js @@ -0,0 +1,4 @@ +// comments +"use strict"; + +module.exports = {}; diff --git a/test/fixtures/transformation/strict/leading-comments-with-existing/expected.js b/test/fixtures/transformation/strict/leading-comments-with-existing/expected.js new file mode 100644 index 0000000000..078ae074be --- /dev/null +++ b/test/fixtures/transformation/strict/leading-comments-with-existing/expected.js @@ -0,0 +1,4 @@ +// comments +"use strict"; + +module.exports = {}; diff --git a/test/fixtures/transformation/strict/leading-comments/actual.js b/test/fixtures/transformation/strict/leading-comments/actual.js new file mode 100644 index 0000000000..51a1ff892d --- /dev/null +++ b/test/fixtures/transformation/strict/leading-comments/actual.js @@ -0,0 +1,3 @@ +// comments + +module.exports = {}; diff --git a/test/fixtures/transformation/strict/leading-comments/expected.js b/test/fixtures/transformation/strict/leading-comments/expected.js new file mode 100644 index 0000000000..e6091249bb --- /dev/null +++ b/test/fixtures/transformation/strict/leading-comments/expected.js @@ -0,0 +1,5 @@ +// comments + +"use strict"; + +module.exports = {}; From 67efb1b4278f4bc6b93f2df59f1aba14958e753d Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Tue, 17 Mar 2015 02:20:54 +1100 Subject: [PATCH 2/7] ignore this expressions in Scope#generateTempBasedOnNode - fixes #1033 --- src/babel/traversal/scope.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/babel/traversal/scope.js b/src/babel/traversal/scope.js index 5c006703b8..5126e57de5 100644 --- a/src/babel/traversal/scope.js +++ b/src/babel/traversal/scope.js @@ -174,6 +174,10 @@ export default class Scope { */ generateTempBasedOnNode(node: Object): ?Object { + if (t.isThisExpression(node)) { + return null; + } + if (t.isIdentifier(node) && this.hasBinding(node.name)) { return null; } From 4b5ba6c8c64b2c2573516f9c8efb944ecd68ad3b Mon Sep 17 00:00:00 2001 From: Brian Donovan Date: Mon, 16 Mar 2015 08:40:32 -0700 Subject: [PATCH 3/7] Add a test for the this-spread fix in 976e8c1. Addresses #1033. --- .../transformation/es6-spread/this-context/actual.js | 6 ++++++ .../transformation/es6-spread/this-context/expected.js | 8 ++++++++ 2 files changed, 14 insertions(+) create mode 100644 test/fixtures/transformation/es6-spread/this-context/actual.js create mode 100644 test/fixtures/transformation/es6-spread/this-context/expected.js diff --git a/test/fixtures/transformation/es6-spread/this-context/actual.js b/test/fixtures/transformation/es6-spread/this-context/actual.js new file mode 100644 index 0000000000..9473736d36 --- /dev/null +++ b/test/fixtures/transformation/es6-spread/this-context/actual.js @@ -0,0 +1,6 @@ +var obj = { + foo: function foo() { + this.bar(...arguments) + this.blah(...arguments) + } +} diff --git a/test/fixtures/transformation/es6-spread/this-context/expected.js b/test/fixtures/transformation/es6-spread/this-context/expected.js new file mode 100644 index 0000000000..3a9d917b5a --- /dev/null +++ b/test/fixtures/transformation/es6-spread/this-context/expected.js @@ -0,0 +1,8 @@ +"use strict"; + +var obj = { + foo: function foo() { + this.bar.apply(this, arguments); + this.blah.apply(this, arguments); + } +}; From 9b586c7587071ec059725ccfbd728303ef0e0baa Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Tue, 17 Mar 2015 12:03:13 +1100 Subject: [PATCH 4/7] add support for left side patterns in assignment pattern destructuring - fixes #1037 --- .../transformers/es6/destructuring.js | 23 ++++++++++++------- .../assignment-expression-pattern/actual.js | 2 ++ .../assignment-expression-pattern/expected.js | 6 +++++ 3 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 test/fixtures/transformation/es6-destructuring/assignment-expression-pattern/actual.js create mode 100644 test/fixtures/transformation/es6-destructuring/assignment-expression-pattern/expected.js diff --git a/src/babel/transformation/transformers/es6/destructuring.js b/src/babel/transformation/transformers/es6/destructuring.js index ea9e3bdaf3..8fb8bc5ab5 100644 --- a/src/babel/transformation/transformers/es6/destructuring.js +++ b/src/babel/transformation/transformers/es6/destructuring.js @@ -290,14 +290,21 @@ class DestructuringTransformer { // - this.nodes.push(this.buildVariableAssignment( - pattern.left, - t.conditionalExpression( - t.binaryExpression("===", tempValueRef, t.identifier("undefined")), - pattern.right, - tempValueRef - ) - )); + var tempConditional = t.conditionalExpression( + t.binaryExpression("===", tempValueRef, t.identifier("undefined")), + pattern.right, + tempValueRef + ); + + var left = pattern.left; + if (t.isPattern(left)) { + this.nodes.push(t.expressionStatement( + t.assignmentExpression("=", tempValueRef, tempConditional) + )); + this.push(left, tempValueRef); + } else { + this.nodes.push(this.buildVariableAssignment(left, tempConditional)); + } } pushObjectSpread(pattern, objRef, spreadProp, spreadPropIndex) { diff --git a/test/fixtures/transformation/es6-destructuring/assignment-expression-pattern/actual.js b/test/fixtures/transformation/es6-destructuring/assignment-expression-pattern/actual.js new file mode 100644 index 0000000000..95a96ebeb9 --- /dev/null +++ b/test/fixtures/transformation/es6-destructuring/assignment-expression-pattern/actual.js @@ -0,0 +1,2 @@ +var z = {}; +var { x: { y } = {} } = z; diff --git a/test/fixtures/transformation/es6-destructuring/assignment-expression-pattern/expected.js b/test/fixtures/transformation/es6-destructuring/assignment-expression-pattern/expected.js new file mode 100644 index 0000000000..6a3de508c1 --- /dev/null +++ b/test/fixtures/transformation/es6-destructuring/assignment-expression-pattern/expected.js @@ -0,0 +1,6 @@ +"use strict"; + +var z = {}; +var _z$x = z.x; +_z$x = _z$x === undefined ? {} : _z$x; +var y = _z$x.y; From ebbdb95278d49adee8715c03504e934121d51457 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Tue, 17 Mar 2015 12:09:16 +1100 Subject: [PATCH 5/7] add 4.7.13 changelog --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9af45c0359..ec4543fcfe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,14 @@ _Note: Gaps between patch versions are faulty/broken releases._ See [CHANGELOG - 6to5](CHANGELOG-6to5.md) for the pre-4.0.0 version changelog. +## 4.7.13 + + * **Bug Fix** + * Handle comments on use strict directives. + * Fix assignment patterns with a left side pattern. + * **Polish** + * Special case `this` when doing expression memoisation. + ## 4.7.12 * **Bug Fix** From 977290c7e458d08febea20dca8097ffd45c5ec88 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Tue, 17 Mar 2015 12:11:26 +1100 Subject: [PATCH 6/7] v4.7.13 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 086793830b..85dafed642 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "babel", "description": "Turn ES6 code into readable vanilla ES5 with source maps", - "version": "4.7.12", + "version": "4.7.13", "author": "Sebastian McKenzie ", "homepage": "https://babeljs.io/", "repository": "babel/babel", From 7f9fe4af15e2e59e0e6b5c7bb7fae5b719e40fcd Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Tue, 17 Mar 2015 12:12:51 +1100 Subject: [PATCH 7/7] 4.7.13 --- packages/babel-runtime/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/babel-runtime/package.json b/packages/babel-runtime/package.json index 327320ef59..2a9557a226 100644 --- a/packages/babel-runtime/package.json +++ b/packages/babel-runtime/package.json @@ -1,7 +1,7 @@ { "name": "babel-runtime", "description": "babel selfContained runtime", - "version": "4.7.12", + "version": "4.7.13", "repository": "babel/babel", "author": "Sebastian McKenzie ", "dependencies": {