Skip to content

Commit ef9b3d2

Browse files
eps1lonoliviertassinari
authored andcommitted
Fix extraneous semicolon (#168)
This was caused because the transformed AST was invalid when VariableDeclarator was wrapped because it returned an AssignmentExpression that was used as the variableDeclarator for a VariableDeclaration.
1 parent 5fe753a commit ef9b3d2

File tree

10 files changed

+48
-34
lines changed

10 files changed

+48
-34
lines changed

src/index.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,23 @@ export default function(api) {
152152
`,
153153
{ placeholderPattern: /^NODE$/ }
154154
),
155-
wrapTemplate: template(
156-
`
157-
LEFT = process.env.NODE_ENV !== "production" ? RIGHT : {}
158-
`,
159-
{ placeholderPattern: /^(LEFT|RIGHT)$/ }
160-
),
155+
wrapTemplate: ({ LEFT, RIGHT }, options = {}) => {
156+
const { as = 'assignmentExpression' } = options
157+
const right = template.expression(
158+
`
159+
process.env.NODE_ENV !== "production" ? RIGHT : {}
160+
`,
161+
{ placeholderPattern: /^(LEFT|RIGHT)$/ }
162+
)({ RIGHT })
163+
switch (as) {
164+
case 'variableDeclarator':
165+
return types.variableDeclarator(LEFT, right)
166+
case 'assignmentExpression':
167+
return types.assignmentExpression('=', LEFT, right)
168+
default:
169+
throw new Error(`unrecognized template type ${as}`)
170+
}
171+
},
161172
mode: state.opts.mode || 'remove',
162173
ignoreFilenames,
163174
types,

src/remove.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,13 @@ export default function remove(path, globalOptions, options) {
113113
)
114114
} else {
115115
path.replaceWith(
116-
wrapTemplate({
117-
LEFT: path.node.id,
118-
RIGHT: path.node.init,
119-
})
116+
wrapTemplate(
117+
{
118+
LEFT: path.node.id,
119+
RIGHT: path.node.init,
120+
},
121+
{ as: 'variableDeclarator' }
122+
)
120123
)
121124
}
122125
path.node[visitedKey] = true

test/fixtures/const-in-anonymous-validator/expected-wrap-es5.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var referencedPropTypes = process.env.NODE_ENV !== "production" ? {
1616
var willBeWrapped = 1;
1717
return null;
1818
}
19-
} : {};;
19+
} : {};
2020
Component.propTypes = process.env.NODE_ENV !== "production" ? babelHelpers.objectSpread({
2121
variant1: function variant1(props) {
2222
var variants = [null];

test/fixtures/const-in-anonymous-validator/expected-wrap-es6.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const referencedPropTypes = process.env.NODE_ENV !== "production" ? {
77
const willBeWrapped = 1;
88
return null;
99
}
10-
} : {};;
10+
} : {};
1111
Component.propTypes = process.env.NODE_ENV !== "production" ? babelHelpers.objectSpread({
1212
variant1: props => {
1313
const variants = [null];

test/fixtures/es-class-assign-property-variable/expected-wrap-es5.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
var propTypes = process.env.NODE_ENV !== "production" ? {
44
foo: PropTypes.string
5-
} : {};;
5+
} : {};
66

77
var Foo =
88
/*#__PURE__*/

test/fixtures/es-class-assign-property-variable/expected-wrap-es6.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const propTypes = process.env.NODE_ENV !== "production" ? {
22
foo: PropTypes.string
3-
} : {};;
3+
} : {};
44

55
class Foo extends React.Component {
66
render() {}

test/fixtures/variable-assignment-member-expressions/expected-wrap-es5.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
var shapePropType = process.env.NODE_ENV !== "production" ? PropTypes.shape({
44
foo: PropTypes.string
5-
}) : {};;
5+
}) : {};
66

77
var ComponentA = function ComponentA() {
88
return React.createElement("div", null);
@@ -14,7 +14,7 @@ ComponentA.propTypes = process.env.NODE_ENV !== "production" ? {
1414
var somePropTypes = process.env.NODE_ENV !== "production" ? {
1515
foo: PropTypes.string,
1616
bar: PropTypes.number
17-
} : {};;
17+
} : {};
1818

1919
var ComponentB = function ComponentB() {
2020
return React.createElement("div", null);
@@ -26,7 +26,7 @@ ComponentB.propTypes = process.env.NODE_ENV !== "production" ? {
2626
var somePropTypesC = process.env.NODE_ENV !== "production" ? {
2727
foo: PropTypes.string,
2828
bar: PropTypes.number
29-
} : {};;
29+
} : {};
3030

3131
var ComponentC = function ComponentC() {
3232
return React.createElement("div", null);
@@ -38,7 +38,7 @@ ComponentC.propTypes = process.env.NODE_ENV !== "production" ? {
3838
var somePropTypesD = process.env.NODE_ENV !== "production" ? {
3939
foo: PropTypes.string,
4040
bar: PropTypes.number
41-
} : {};;
41+
} : {};
4242

4343
var ComponentD = function ComponentD() {
4444
return React.createElement("div", null);

test/fixtures/variable-assignment-member-expressions/expected-wrap-es6.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const shapePropType = process.env.NODE_ENV !== "production" ? PropTypes.shape({
22
foo: PropTypes.string
3-
}) : {};;
3+
}) : {};
44

55
const ComponentA = () => <div />;
66

@@ -10,7 +10,7 @@ ComponentA.propTypes = process.env.NODE_ENV !== "production" ? {
1010
const somePropTypes = process.env.NODE_ENV !== "production" ? {
1111
foo: PropTypes.string,
1212
bar: PropTypes.number
13-
} : {};;
13+
} : {};
1414

1515
const ComponentB = () => <div />;
1616

@@ -20,7 +20,7 @@ ComponentB.propTypes = process.env.NODE_ENV !== "production" ? {
2020
const somePropTypesC = process.env.NODE_ENV !== "production" ? {
2121
foo: PropTypes.string,
2222
bar: PropTypes.number
23-
} : {};;
23+
} : {};
2424

2525
const ComponentC = () => <div />;
2626

@@ -30,7 +30,7 @@ ComponentC.propTypes = process.env.NODE_ENV !== "production" ? {
3030
const somePropTypesD = process.env.NODE_ENV !== "production" ? {
3131
foo: PropTypes.string,
3232
bar: PropTypes.number
33-
} : {};;
33+
} : {};
3434

3535
const ComponentD = () => <div />;
3636

test/fixtures/variable-assignment/expected-wrap-es5.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", {
66
exports.propTypesExported = void 0;
77
var propTypesBasic = process.env.NODE_ENV !== "production" ? {
88
foo: PropTypes.string
9-
} : {};;
9+
} : {};
1010

1111
var FooBasic = function FooBasic() {
1212
return React.createElement("div", null);
@@ -15,10 +15,10 @@ var FooBasic = function FooBasic() {
1515
FooBasic.propTypes = process.env.NODE_ENV !== "production" ? propTypesBasic : {};
1616
var extraReference = process.env.NODE_ENV !== "production" ? {
1717
bar: PropTypes.string
18-
} : {};;
18+
} : {};
1919
var propTypesWithExtraReference = process.env.NODE_ENV !== "production" ? Object.assign({}, extraReference, {
2020
foo: PropTypes.string
21-
}) : {};;
21+
}) : {};
2222

2323
var FooExtraReference = function FooExtraReference() {
2424
return React.createElement("div", null);
@@ -29,7 +29,7 @@ var propTypesWithExtraReferenceSpread = process.env.NODE_ENV !== "production" ?
2929
foo: PropTypes.string
3030
}, {
3131
bar: PropTypes.string
32-
}) : {};;
32+
}) : {};
3333

3434
var FooExtraReferenceSpread = function FooExtraReferenceSpread() {
3535
return React.createElement("div", null);
@@ -38,7 +38,7 @@ var FooExtraReferenceSpread = function FooExtraReferenceSpread() {
3838
FooExtraReferenceSpread.propTypes = process.env.NODE_ENV !== "production" ? propTypesWithExtraReferenceSpread : {};
3939
var propTypesWrapped = process.env.NODE_ENV !== "production" ? {
4040
foo: PropTypes.string
41-
} : {};;
41+
} : {};
4242

4343
var FooWrapped = function FooWrapped() {
4444
return React.createElement("div", null);
@@ -68,7 +68,7 @@ var FooExported = function FooExported() {
6868
FooExported.propTypes = process.env.NODE_ENV !== "production" ? propTypesExported : {};
6969
var propTypesCreateClass = process.env.NODE_ENV !== "production" ? {
7070
foo: PropTypes.string
71-
} : {};;
71+
} : {};
7272
var FooCreateClass = createReactClass({
7373
displayName: "FooCreateClass",
7474
propTypes: propTypesCreateClass

test/fixtures/variable-assignment/expected-wrap-es6.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
const propTypesBasic = process.env.NODE_ENV !== "production" ? {
22
foo: PropTypes.string
3-
} : {};;
3+
} : {};
44

55
const FooBasic = () => <div />;
66

77
FooBasic.propTypes = process.env.NODE_ENV !== "production" ? propTypesBasic : {};
88
const extraReference = process.env.NODE_ENV !== "production" ? {
99
bar: PropTypes.string
10-
} : {};;
10+
} : {};
1111
const propTypesWithExtraReference = process.env.NODE_ENV !== "production" ? Object.assign({}, extraReference, {
1212
foo: PropTypes.string
13-
}) : {};;
13+
}) : {};
1414

1515
const FooExtraReference = () => <div />;
1616

@@ -19,14 +19,14 @@ const propTypesWithExtraReferenceSpread = process.env.NODE_ENV !== "production"
1919
foo: PropTypes.string
2020
}, {
2121
bar: PropTypes.string
22-
}) : {};;
22+
}) : {};
2323

2424
const FooExtraReferenceSpread = () => <div />;
2525

2626
FooExtraReferenceSpread.propTypes = process.env.NODE_ENV !== "production" ? propTypesWithExtraReferenceSpread : {};
2727
const propTypesWrapped = process.env.NODE_ENV !== "production" ? {
2828
foo: PropTypes.string
29-
} : {};;
29+
} : {};
3030

3131
const FooWrapped = () => <div />;
3232

@@ -47,7 +47,7 @@ const FooExported = () => <div />;
4747
FooExported.propTypes = process.env.NODE_ENV !== "production" ? propTypesExported : {};
4848
const propTypesCreateClass = process.env.NODE_ENV !== "production" ? {
4949
foo: PropTypes.string
50-
} : {};;
50+
} : {};
5151
const FooCreateClass = createReactClass({
5252
propTypes: propTypesCreateClass
5353
});

0 commit comments

Comments
 (0)