diff --git a/packages/babel-helper-evaluate-path/package.json b/packages/babel-helper-evaluate-path/package.json index 43c90182e..d6b24d825 100644 --- a/packages/babel-helper-evaluate-path/package.json +++ b/packages/babel-helper-evaluate-path/package.json @@ -11,5 +11,8 @@ "license": "MIT", "author": "boopathi", "main": "lib/index.js", - "repository": "https://github.com/babel/minify/tree/master/packages/babel-helper-evaluate-path" + "repository": "https://github.com/babel/minify/tree/master/packages/babel-helper-evaluate-path", + "dependencies": { + "@babel/types": "^7.0.0-beta.46" + } } diff --git a/packages/babel-helper-evaluate-path/src/index.js b/packages/babel-helper-evaluate-path/src/index.js index 751d4d88f..8b723144b 100644 --- a/packages/babel-helper-evaluate-path/src/index.js +++ b/packages/babel-helper-evaluate-path/src/index.js @@ -1,5 +1,7 @@ "use strict"; +const t = require("@babel/types"); + module.exports = function evaluate(path, { tdz = false } = {}) { if (!tdz && !path.isReferencedIdentifier()) { return baseEvaluate(path); @@ -120,15 +122,14 @@ function evaluateBasedOnControlFlow(binding, refPath) { // early-exit const declaration = binding.path.parentPath; - if (declaration.parentPath.removed) { - return { confident: true, value: void 0 }; - } - if ( - declaration.parentPath.isIfStatement() || - declaration.parentPath.isLoop() || - declaration.parentPath.isSwitchCase() + t.isIfStatement(declaration.parentPath) || + t.isLoop(declaration.parentPath) || + t.isSwitchCase(declaration.parentPath) ) { + if (declaration.parentPath.removed) { + return { confident: true, value: void 0 }; + } return { shouldDeopt: true }; } @@ -180,10 +181,9 @@ function evaluateBasedOnControlFlow(binding, refPath) { const declaration = declarator.parentPath; if ( - declaration.parentPath && - (declaration.parentPath.isIfStatement() || - declaration.parentPath.isLoop() || - declaration.parentPath.isSwitchCase()) + t.isIfStatement(declaration.parentPath) || + t.isLoop(declaration.parentPath) || + t.isSwitchCase(declaration.parentPath) ) { return { shouldDeopt: true }; } diff --git a/packages/babel-plugin-minify-constant-folding/__tests__/fixtures/issue-844/actual.js b/packages/babel-plugin-minify-constant-folding/__tests__/fixtures/issue-844/actual.js new file mode 100644 index 000000000..270c51440 --- /dev/null +++ b/packages/babel-plugin-minify-constant-folding/__tests__/fixtures/issue-844/actual.js @@ -0,0 +1,2 @@ +class MyComponent {} +MyComponent.propTypes = { userName: 123 }; diff --git a/packages/babel-plugin-minify-constant-folding/__tests__/fixtures/issue-844/expected.js b/packages/babel-plugin-minify-constant-folding/__tests__/fixtures/issue-844/expected.js new file mode 100644 index 000000000..96c1a00d1 --- /dev/null +++ b/packages/babel-plugin-minify-constant-folding/__tests__/fixtures/issue-844/expected.js @@ -0,0 +1,5 @@ +class MyComponent {} + +MyComponent.propTypes = { + userName: 123 +}; \ No newline at end of file diff --git a/packages/babel-preset-minify/__tests__/minify-env-tests.js b/packages/babel-preset-minify/__tests__/minify-env-tests.js index 003ec25ef..99c6036ea 100644 --- a/packages/babel-preset-minify/__tests__/minify-env-tests.js +++ b/packages/babel-preset-minify/__tests__/minify-env-tests.js @@ -251,19 +251,4 @@ describe("preset along with env", () => { } ` ); - thePlugin( - "should fix issue#844", - ` - class MyComponent { - } - MyComponent.propTypes = { - userName: 123 - } - `, - ` - "use strict"; - class a {} - a.propTypes = { userName: 123 }; - ` - ); }); diff --git a/yarn.lock b/yarn.lock index a2fcbaa14..41a90d83b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -504,7 +504,7 @@ invariant "^2.2.0" lodash "^4.2.0" -"@babel/types@7.0.0-beta.46": +"@babel/types@7.0.0-beta.46", "@babel/types@^7.0.0-beta.46": version "7.0.0-beta.46" resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.0.0-beta.46.tgz#eb84399a699af9fcb244440cce78e1acbeb40e0c" dependencies: