diff --git a/build-system/babel-config/post-closure-config.js b/build-system/babel-config/post-closure-config.js index 17444080cdca..4fcc105fd072 100644 --- a/build-system/babel-config/post-closure-config.js +++ b/build-system/babel-config/post-closure-config.js @@ -32,7 +32,6 @@ function getPostClosureConfig() { './build-system/babel-plugins/babel-plugin-const-transformer', './build-system/babel-plugins/babel-plugin-transform-remove-directives', './build-system/babel-plugins/babel-plugin-transform-stringish-literals', - './build-system/babel-plugins/babel-plugin-transform-block-scoping', ]; return { compact: false, diff --git a/build-system/babel-plugins/babel-plugin-transform-block-scoping/index.js b/build-system/babel-plugins/babel-plugin-transform-block-scoping/index.js deleted file mode 100644 index 3b6a89a4ec99..000000000000 --- a/build-system/babel-plugins/babel-plugin-transform-block-scoping/index.js +++ /dev/null @@ -1,121 +0,0 @@ -/** - * Copyright 2020 The AMP HTML Authors. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS-IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -// Transforms let scoped variables into var variables, including class -// declarations. -// -// Let variables inside loops that are referenced inside a -// closure are left alone. -// -// Re: https://github.com/evanw/esbuild/issues/478 -// Re: https://gist.github.com/jridgewell/7a6468f61eecb467b8de265f04963286 -// -// Input: -// ``` -// let x = 0; -// () => { -// let y = 1; -// } -// -// for (let i = 0; i < 1; i++) { -// i; -// } -// -// for (let i = 0; i < 1; i++) { -// () => i; -// } -// -// class Foo {} -// ``` -// -// Output: -// ``` -// var x = 0; -// () => { -// var y = 1; -// } -// -// for (var i = 0; i < 1; i++) { -// i; -// } -// -// for (let i = 0; i < 1; i++) { -// () => i; -// } -// -// var Foo = class {} -// ``` -module.exports = function ({template, types: t}) { - return { - name: 'block-scoping', // not required - visitor: { - ClassDeclaration(path) { - const {node} = path; - const {id} = node; - if (!id) { - return; - } - - const {name} = id; - node.type = 'ClassExpression'; - node.id = null; - path.replaceWith(template.statement.ast`let ${name} = ${node}`); - }, - - VariableDeclaration(path) { - const {node, scope} = path; - if (node.kind !== 'let') { - return; - } - - // We're looking for the function scope that would inherit this var - // declaration, and whether there is a loop scope in between. - const parent = path.findParent((p) => { - return p.isFunction() || p.isProgram() || p.isLoop(); - }); - const bindings = Object.keys( - t.getBindingIdentifiers(node, false, true) - ); - - // If the let is contained in a loop scope, then there's the - // possibility that a reference will be closed over in a nested - // function. Each loop iteration will requires its own value, which - // means we have to use another function to emulate the behavior. - if (parent.isLoop()) { - for (const name of bindings) { - const references = scope.getBinding(name).referencePaths; - for (const ref of references) { - const p = ref.findParent((p) => { - return p === parent || p.isFunction(); - }); - - if (p.isFunction()) { - // The let variable is closed over. In this case, just leave - // the let alone instead of trying to transform it. - return; - } - } - } - } - - for (const name of bindings) { - scope.rename(name); - } - node.kind = 'var'; - }, - }, - }; -}; diff --git a/build-system/babel-plugins/babel-plugin-transform-block-scoping/test/fixtures/transform-class-declaration/transform-class/input.js b/build-system/babel-plugins/babel-plugin-transform-block-scoping/test/fixtures/transform-class-declaration/transform-class/input.js deleted file mode 100644 index 911611306e64..000000000000 --- a/build-system/babel-plugins/babel-plugin-transform-block-scoping/test/fixtures/transform-class-declaration/transform-class/input.js +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright 2020 The AMP HTML Authors. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS-IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -class A { - method1() {} - method2() {} - static staticMethod1() {} -} - -{ - new A(); - class A { - method1() {} - method2() {} - static staticMethod1() {} - } - new A(); -} - -function hello() { - new A(); - class A { - method1() {} - method2() {} - static staticMethod1() {} - } - new A(); -} - -new A(); diff --git a/build-system/babel-plugins/babel-plugin-transform-block-scoping/test/fixtures/transform-class-declaration/transform-class/options.json b/build-system/babel-plugins/babel-plugin-transform-block-scoping/test/fixtures/transform-class-declaration/transform-class/options.json deleted file mode 100644 index a47282a6be9a..000000000000 --- a/build-system/babel-plugins/babel-plugin-transform-block-scoping/test/fixtures/transform-class-declaration/transform-class/options.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "plugins": ["../../../.."] -} diff --git a/build-system/babel-plugins/babel-plugin-transform-block-scoping/test/fixtures/transform-class-declaration/transform-class/output.js b/build-system/babel-plugins/babel-plugin-transform-block-scoping/test/fixtures/transform-class-declaration/transform-class/output.js deleted file mode 100644 index 6f6eeb3ec6db..000000000000 --- a/build-system/babel-plugins/babel-plugin-transform-block-scoping/test/fixtures/transform-class-declaration/transform-class/output.js +++ /dev/null @@ -1,55 +0,0 @@ -/** - * Copyright 2020 The AMP HTML Authors. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS-IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -var _A = class { - method1() {} - - method2() {} - - static staticMethod1() {} - -}; - -{ - new _A2(); - - var _A2 = class { - method1() {} - - method2() {} - - static staticMethod1() {} - - }; - - new _A2(); -} - -function hello() { - new _A3(); - - var _A3 = class { - method1() {} - - method2() {} - - static staticMethod1() {} - - }; - - new _A3(); -} - -new _A(); diff --git a/build-system/babel-plugins/babel-plugin-transform-block-scoping/test/fixtures/transform-variable-declaration/no-transform-const/input.js b/build-system/babel-plugins/babel-plugin-transform-block-scoping/test/fixtures/transform-variable-declaration/no-transform-const/input.js deleted file mode 100644 index f80b4d4893e0..000000000000 --- a/build-system/babel-plugins/babel-plugin-transform-block-scoping/test/fixtures/transform-variable-declaration/no-transform-const/input.js +++ /dev/null @@ -1,38 +0,0 @@ -/** - * Copyright 2020 The AMP HTML Authors. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS-IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -const a = 'abc'; - -{ - const a = 'xyz'; -} - -function test() { - const a = 1; - { - const a = 2; - const b = 2; - } -} - -const z = []; - -for (const i = 0; i < 10; i++) { - z.push(function() { - return function() { - console.log(i); - }; - }); -} diff --git a/build-system/babel-plugins/babel-plugin-transform-block-scoping/test/fixtures/transform-variable-declaration/no-transform-const/options.json b/build-system/babel-plugins/babel-plugin-transform-block-scoping/test/fixtures/transform-variable-declaration/no-transform-const/options.json deleted file mode 100644 index a47282a6be9a..000000000000 --- a/build-system/babel-plugins/babel-plugin-transform-block-scoping/test/fixtures/transform-variable-declaration/no-transform-const/options.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "plugins": ["../../../.."] -} diff --git a/build-system/babel-plugins/babel-plugin-transform-block-scoping/test/fixtures/transform-variable-declaration/no-transform-const/output.js b/build-system/babel-plugins/babel-plugin-transform-block-scoping/test/fixtures/transform-variable-declaration/no-transform-const/output.js deleted file mode 100644 index db44958fd3c7..000000000000 --- a/build-system/babel-plugins/babel-plugin-transform-block-scoping/test/fixtures/transform-variable-declaration/no-transform-const/output.js +++ /dev/null @@ -1,37 +0,0 @@ -/** - * Copyright 2020 The AMP HTML Authors. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS-IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -const a = 'abc'; -{ - const a = 'xyz'; -} - -function test() { - const a = 1; - { - const a = 2; - const b = 2; - } -} - -const z = []; - -for (const i = 0; i < 10; i++) { - z.push(function () { - return function () { - console.log(i); - }; - }); -} diff --git a/build-system/babel-plugins/babel-plugin-transform-block-scoping/test/fixtures/transform-variable-declaration/transform-let/input.js b/build-system/babel-plugins/babel-plugin-transform-block-scoping/test/fixtures/transform-variable-declaration/transform-let/input.js deleted file mode 100644 index 2a72fa28cdca..000000000000 --- a/build-system/babel-plugins/babel-plugin-transform-block-scoping/test/fixtures/transform-variable-declaration/transform-let/input.js +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Copyright 2020 The AMP HTML Authors. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS-IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -let a = 'abc'; -{ - let a = 'xyz'; - console.log(a); -} -console.log(a); - -function test() { - let a = 1; - { - let a = 2; - console.log(a); - } - console.log(a); -} - -let z = []; - -for (let i = 0; i < 10; i++) { - z.push(function() { - console.log(i); - }); - - let x = i; - z.push(function() { - console.log(x); - }); -} diff --git a/build-system/babel-plugins/babel-plugin-transform-block-scoping/test/fixtures/transform-variable-declaration/transform-let/options.json b/build-system/babel-plugins/babel-plugin-transform-block-scoping/test/fixtures/transform-variable-declaration/transform-let/options.json deleted file mode 100644 index a47282a6be9a..000000000000 --- a/build-system/babel-plugins/babel-plugin-transform-block-scoping/test/fixtures/transform-variable-declaration/transform-let/options.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "plugins": ["../../../.."] -} diff --git a/build-system/babel-plugins/babel-plugin-transform-block-scoping/test/fixtures/transform-variable-declaration/transform-let/output.js b/build-system/babel-plugins/babel-plugin-transform-block-scoping/test/fixtures/transform-variable-declaration/transform-let/output.js deleted file mode 100644 index 9732c34fe3f0..000000000000 --- a/build-system/babel-plugins/babel-plugin-transform-block-scoping/test/fixtures/transform-variable-declaration/transform-let/output.js +++ /dev/null @@ -1,44 +0,0 @@ -/** - * Copyright 2020 The AMP HTML Authors. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS-IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -var _a = 'abc'; -{ - var _a2 = 'xyz'; - console.log(_a2); -} -console.log(_a); - -function test() { - var _a3 = 1; - { - var _a4 = 2; - console.log(_a4); - } - console.log(_a3); -} - -var _z = []; - -for (let i = 0; i < 10; i++) { - _z.push(function () { - console.log(i); - }); - - let x = i; - - _z.push(function () { - console.log(x); - }); -} diff --git a/build-system/babel-plugins/babel-plugin-transform-block-scoping/test/index.js b/build-system/babel-plugins/babel-plugin-transform-block-scoping/test/index.js deleted file mode 100644 index bb2e50c56b6a..000000000000 --- a/build-system/babel-plugins/babel-plugin-transform-block-scoping/test/index.js +++ /dev/null @@ -1,19 +0,0 @@ -/** - * Copyright 2020 The AMP HTML Authors. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS-IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -const runner = require('@babel/helper-plugin-test-runner').default; - -runner(__dirname); diff --git a/build-system/compile/post-closure-babel.js b/build-system/compile/post-closure-babel.js index 1bf5cdc38731..2d068dae8eb5 100644 --- a/build-system/compile/post-closure-babel.js +++ b/build-system/compile/post-closure-babel.js @@ -30,7 +30,7 @@ const {debug, CompilationLifecycles} = require('./debug-compilation-lifecycle'); */ async function terserMinify(code) { const minified = await terser.minify(code, { - mangle: true, + mangle: false, compress: { defaults: false, unused: true,