Skip to content

Commit a100538

Browse files
eps1lonoliviertassinari
authored andcommitted
Fix babel readonly (#167)
* Add failing test case for const scoped * Fix babel read-only error * try something else * add a new unit test
1 parent 6ed3b51 commit a100538

File tree

12 files changed

+138
-21
lines changed

12 files changed

+138
-21
lines changed

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
"test:unit": "mocha",
99
"test:watch": "mocha -w",
1010
"prettier": "find . -name \"*.js\" | grep -v -f .eslintignore | xargs prettier --write",
11-
"test": "npm run lint && npm run test:unit && npm run flow",
11+
"test": "npm run lint && npm run test:unit",
1212
"prebuild": "rm -rf lib/",
1313
"build": "babel src --out-dir lib",
14-
"flow": "flow",
1514
"version": "npm run build && pkgfiles"
1615
},
1716
"repository": {
@@ -53,7 +52,6 @@
5352
"eslint-plugin-mocha": "^4.11.0",
5453
"eslint-plugin-prettier": "^2.3.1",
5554
"eslint-plugin-react": "^7.4.0",
56-
"flow-bin": "^0.59.0",
5755
"mocha": "^4.0.1",
5856
"path-exists": "^3.0.0",
5957
"pkgfiles": "^2.3.2",

src/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// @flow weak
21
/* eslint-disable global-require, import/no-dynamic-require */
32

43
// import generate from 'babel-generator';
@@ -288,6 +287,11 @@ export default function(api) {
288287
let skippedIdentifiers = 0
289288
const removeNewlyUnusedIdentifiers = {
290289
VariableDeclarator(path) {
290+
// Only consider the top level scope.
291+
if (path.scope.block.type !== 'Program') {
292+
return
293+
}
294+
291295
if (['ObjectPattern', 'ArrayPattern'].includes(path.node.id.type)) {
292296
// Object or Array destructuring, so we will want to capture all
293297
// the names created by the destructuring. This currently doesn't
@@ -309,7 +313,7 @@ export default function(api) {
309313
// removedPaths Set. We need to do this in order to support the wrap
310314
// option, which doesn't actually remove the references.
311315
const hasRemainingReferencePaths = referencePaths.some(referencePath => {
312-
const found = referencePath.find(p => removedPaths.has(p))
316+
const found = referencePath.find(path2 => removedPaths.has(path2))
313317
return !found
314318
})
315319

src/isAnnotatedForRemoval.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
// @flow weak
2-
3-
export default function(node) {
1+
export default function isAnnotatedForRemoval(node) {
42
const comments = node.trailingComments || []
53

6-
return !!comments.find(({ value }) => value.trim() === 'remove-proptypes')
4+
return Boolean(comments.find(({ value }) => value.trim() === 'remove-proptypes'))
75
}

src/isStatelessComponent.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// @flow weak
2-
31
const traversed = Symbol('traversed')
42

53
function isJSXElementOrReactCreateElement(path) {

src/remove.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// @flow weak
21
/* eslint-disable no-param-reassign */
32

43
function isInside(scope, regex) {

test/fixtures.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('fixtures', () => {
2323
const fixtureDir = path.join(fixturesDir, caseName)
2424

2525
// Only run a specific test
26-
// if (caseName !== 'recursive') {
26+
// if (caseName !== 'const-in-anonymous-validator') {
2727
// return
2828
// }
2929

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import React, {PropTypes} from 'react';
2+
3+
const Component = () => (
4+
<div />
5+
);
6+
7+
const referencedPropTypes = {
8+
variant3: () => {
9+
const willBeWrapped = 1;
10+
return null;
11+
}
12+
}
13+
14+
Component.propTypes = {
15+
variant1: props => {
16+
const variants = [null];
17+
return variantſ[0];
18+
},
19+
variant2: chainPropTypes(
20+
PropTypes.oneOf(['foo']),
21+
props => {
22+
const deprecatedVariants = [
23+
'display4',
24+
'display3',
25+
'display2',
26+
'display1',
27+
'headline',
28+
'title',
29+
'subheading',
30+
];
31+
32+
return null;
33+
},
34+
),
35+
...referencedPropTypes,
36+
};
37+
38+
export default Component;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"use strict";
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.default = void 0;
7+
8+
var _react = babelHelpers.interopRequireWildcard(require("react"));
9+
10+
var Component = function Component() {
11+
return _react.default.createElement("div", null);
12+
};
13+
14+
var _default = Component;
15+
exports.default = _default;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import React, { PropTypes } from 'react';
2+
3+
const Component = () => <div />;
4+
5+
export default Component;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"use strict";
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.default = void 0;
7+
8+
var _react = babelHelpers.interopRequireWildcard(require("react"));
9+
10+
var Component = function Component() {
11+
return _react.default.createElement("div", null);
12+
};
13+
14+
var referencedPropTypes = process.env.NODE_ENV !== "production" ? {
15+
variant3: function variant3() {
16+
var willBeWrapped = 1;
17+
return null;
18+
}
19+
} : {};;
20+
Component.propTypes = process.env.NODE_ENV !== "production" ? babelHelpers.objectSpread({
21+
variant1: function variant1(props) {
22+
var variants = [null];
23+
return variantſ[0];
24+
},
25+
variant2: chainPropTypes(_react.PropTypes.oneOf(['foo']), function (props) {
26+
var deprecatedVariants = ['display4', 'display3', 'display2', 'display1', 'headline', 'title', 'subheading'];
27+
return null;
28+
})
29+
}, referencedPropTypes) : {};
30+
var _default = Component;
31+
exports.default = _default;

0 commit comments

Comments
 (0)