Skip to content

Commit 66dbc1c

Browse files
committed
Use babel7 and make it a peer dep
1 parent eac5b1d commit 66dbc1c

File tree

4 files changed

+172
-122
lines changed

4 files changed

+172
-122
lines changed

.babelrc

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"presets": ["es2015"],
3-
"plugins": ["transform-react-jsx"]
2+
"presets": [["@babel/preset-env", { "targets": { "node": 6 } }]],
3+
"plugins": ["@babel/plugin-transform-react-jsx"]
44
}

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
node_modules
2+
lib
23
.DS_Store
34
.idea
45
.vscode

package.json

+12-6
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,25 @@
2121
"repository": "git://github.com/coderas/babel-plugin-jsx-remove-data-test-id.git",
2222
"scripts": {
2323
"build": "babel src --out-dir lib",
24-
"prepublish": "babel src --out-dir lib",
25-
"test": "mocha \"**/*.spec.js\" --compilers js:babel-core/register --reporter spec"
24+
"prepare": "npm test && npm run build",
25+
"test": "mocha \"**/*.spec.js\" --compilers js:@babel/register --reporter spec"
2626
},
2727
"author": "Rich Gorman",
2828
"license": "MIT",
29-
"dependencies": {
30-
"babel-cli": "^6.26.0",
31-
"babel-core": "^6.26.0"
29+
"peerDependencies": {
30+
"@babel/core": "^7.0.0"
3231
},
3332
"devDependencies": {
33+
"@babel/cli": "^7.2.3",
34+
"@babel/core": "^7.2.2",
35+
"@babel/plugin-transform-arrow-functions": "^7.2.0",
36+
"@babel/plugin-transform-react-jsx": "^7.3.0",
37+
"@babel/preset-env": "^7.3.1",
38+
"@babel/register": "^7.0.0",
39+
"babel-core": "^6.26.3",
3440
"babel-plugin-transform-es2015-arrow-functions": "^6.22.0",
3541
"babel-plugin-transform-react-jsx": "^6.8.0",
36-
"babel-preset-es2015": "^6.16.0",
42+
"babel-preset-env": "^1.7.0",
3743
"chai": "^3.5.0",
3844
"mocha": "^3.1.2",
3945
"uglify-js": "^2.7.3"

tests/remove-data-test-id.spec.js

+157-114
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { transform } from 'babel-core';
1+
import * as babel6 from 'babel-core';
2+
import * as babel7 from '@babel/core';
23
import { expect } from 'chai';
34
import { minify } from 'uglify-js';
45

@@ -7,128 +8,170 @@ const uglify = code => minify(code, {
78
mangle: false
89
}).code;
910

10-
const config = {
11-
plugins: [
12-
'./src',
13-
['transform-react-jsx', { pragma: 'j' }],
14-
['transform-es2015-arrow-functions', {}]
15-
]
16-
};
11+
const runTests = (label, transform) => {
12+
describe(label, () => {
13+
describe('jsx-remove-data-test-id', () => {
14+
it('does not replace data-something-else', () => {
15+
const code = '<p data-something-else="cake-day">hi, finally it is cake time</p>';
16+
const actual = transform(code, { usePlugin: true });
17+
const expected = transform(code);
18+
expect(uglify(actual)).to.equal(uglify(expected));
19+
});
1720

18-
const configWithoutPlugin = {
19-
plugins: [
20-
['transform-react-jsx', { pragma: 'j' }],
21-
['transform-es2015-arrow-functions', {}]
22-
]
23-
};
21+
it('does not remove attributes that contain "data-test-id" in part only', () => {
22+
const code = '<p data-test-id-not="not-test-id">hi, finally it is cake time</p>';
23+
const actual = transform(code, { usePlugin: true });
24+
const expected = transform(code);
25+
expect(uglify(actual)).to.equal(uglify(expected));
26+
});
2427

25-
describe('jsx-remove-data-test-id', () => {
26-
it('does not replace data-something-else', () => {
27-
const code = '<p data-something-else="cake-day">hi, finally it is cake time</p>';
28-
const actual = transform(code, config).code;
29-
const expected = transform(code, configWithoutPlugin).code;
30-
expect(uglify(actual)).to.equal(uglify(expected));
31-
});
28+
it('removes data-test-id', () => {
29+
const code = '<p data-test-id="test-id"></p>';
30+
const expectedCode = '<p></p>';
31+
const actual = transform(code, { usePlugin: true });
32+
const expected = transform(expectedCode);
33+
expect(uglify(actual)).to.equal(uglify(expected));
34+
});
3235

33-
it('does not remove attributes that contain "data-test-id" in part only', () => {
34-
const code = '<p data-test-id-not="not-test-id">hi, finally it is cake time</p>';
35-
const actual = transform(code, config).code;
36-
const expected = transform(code, configWithoutPlugin).code;
37-
expect(uglify(actual)).to.equal(uglify(expected));
38-
});
36+
it('removes data-test-id funcs', () => {
37+
const code = '<p data-test-id={() => {}}></p>';
38+
const expectedCode = '<p></p>';
39+
const actual = transform(code, { usePlugin: true });
40+
const expected = transform(expectedCode);
41+
expect(uglify(actual)).to.equal(uglify(expected));
42+
});
3943

40-
it('removes data-test-id', () => {
41-
const code = '<p data-test-id="test-id"></p>';
42-
const expectedCode = '<p></p>';
43-
const actual = transform(code, config).code;
44-
const expected = transform(expectedCode, configWithoutPlugin).code;
45-
expect(uglify(actual)).to.equal(uglify(expected));
46-
});
44+
it('removes data-test-id bools', () => {
45+
const code = '<p data-test-id={false}></p>';
46+
const expectedCode = '<p></p>';
47+
const actual = transform(code, { usePlugin: true });
48+
const expected = transform(expectedCode);
49+
expect(uglify(actual)).to.equal(uglify(expected));
50+
});
4751

48-
it('removes data-test-id funcs', () => {
49-
const code = '<p data-test-id={() => {}}></p>';
50-
const expectedCode = '<p></p>';
51-
const actual = transform(code, config).code;
52-
const expected = transform(expectedCode, configWithoutPlugin).code;
53-
expect(uglify(actual)).to.equal(uglify(expected));
54-
});
52+
describe('with invalid options.attributes', () => {
53+
it('throws error when attributes is empty string', () => {
54+
const code = '<p selenium-id={false}></p>';
55+
const expectedCode = '<p></p>';
56+
const action = () => transform(code, {
57+
useErroneousAttributes: true,
58+
attributes: ''
59+
});
60+
expect(action).to.throw();
61+
});
5562

56-
it('removes data-test-id bools', () => {
57-
const code = '<p data-test-id={false}></p>';
58-
const expectedCode = '<p></p>';
59-
const actual = transform(code, config).code;
60-
const expected = transform(expectedCode, configWithoutPlugin).code;
61-
expect(uglify(actual)).to.equal(uglify(expected));
62-
});
63+
it('throws error when attributes is empty array', () => {
64+
const code = '<p selenium-id={false}></p>';
65+
const expectedCode = '<p></p>';
66+
const action = () => transform(code, {
67+
useErroneousAttributes: true,
68+
attributes: []
69+
});
70+
expect(action).to.throw();
71+
});
72+
})
6373

64-
describe('with invalid options.attributes', () => {
65-
it('throws error when attributes is empty string', () => {
66-
const configWithErroneousAttributesOption = {
67-
plugins: [
68-
['./src', {attributes: ''}],
69-
['transform-react-jsx', { pragma: 'j' }],
70-
['transform-es2015-arrow-functions', {}]
71-
]
72-
};
73-
const code = '<p selenium-id={false}></p>';
74-
const expectedCode = '<p></p>';
75-
const action = () => transform(code, configWithErroneousAttributesOption);
76-
expect(action).to.throw();
77-
});
74+
describe('with valid options.attributes', () => {
75+
it('does not remove attributes that match options.attributes in part only', () => {
76+
const code = '<p selenium-id-not="not-test-id" no-useless-attr="useless">hi, finally it is cake time</p>';
77+
const actual = transform(code, { useValidAttributes: true });
78+
const expected = transform(code);
79+
expect(uglify(actual)).to.equal(uglify(expected));
80+
});
7881

79-
it('throws error when attributes is empty array', () => {
80-
const configWithErroneousAttributesOption = {
81-
plugins: [
82-
['./src', {attributes: []}],
83-
['transform-react-jsx', { pragma: 'j' }],
84-
['transform-es2015-arrow-functions', {}]
85-
]
86-
};
87-
const code = '<p selenium-id={false}></p>';
88-
const expectedCode = '<p></p>';
89-
const action = () => transform(code, configWithErroneousAttributesOption);
90-
expect(action).to.throw();
91-
});
92-
})
82+
it('removes options.attributes', () => {
83+
const code = '<p selenium-id="test-id" useless-attr="useless"></p>';
84+
const expectedCode = '<p></p>';
85+
const actual = transform(code, { useValidAttributes: true });
86+
const expected = transform(expectedCode);
87+
expect(uglify(actual)).to.equal(uglify(expected));
88+
});
9389

94-
describe('with valid options.attributes', () => {
95-
const configWithValidAttributesOption = {
96-
plugins: [
97-
['./src', { attributes: [ 'selenium-id', 'useless-attr' ] }],
98-
['transform-react-jsx', { pragma: 'j' }],
99-
['transform-es2015-arrow-functions', {}]
100-
]
101-
};
90+
it('removes options.attributes funcs', () => {
91+
const code = '<p selenium-id={() => {}} useless-attr={() => {}}></p>';
92+
const expectedCode = '<p></p>';
93+
const actual = transform(code, { useValidAttributes: true });
94+
const expected = transform(expectedCode);
95+
expect(uglify(actual)).to.equal(uglify(expected));
96+
});
10297

103-
it('does not remove attributes that match options.attributes in part only', () => {
104-
const code = '<p selenium-id-not="not-test-id" no-useless-attr="useless">hi, finally it is cake time</p>';
105-
const actual = transform(code, configWithValidAttributesOption).code;
106-
const expected = transform(code, configWithoutPlugin).code;
107-
expect(uglify(actual)).to.equal(uglify(expected));
108-
});
109-
110-
it('removes options.attributes', () => {
111-
const code = '<p selenium-id="test-id" useless-attr="useless"></p>';
112-
const expectedCode = '<p></p>';
113-
const actual = transform(code, configWithValidAttributesOption).code;
114-
const expected = transform(expectedCode, configWithoutPlugin).code;
115-
expect(uglify(actual)).to.equal(uglify(expected));
116-
});
117-
118-
it('removes options.attributes funcs', () => {
119-
const code = '<p selenium-id={() => {}} useless-attr={() => {}}></p>';
120-
const expectedCode = '<p></p>';
121-
const actual = transform(code, configWithValidAttributesOption).code;
122-
const expected = transform(expectedCode, configWithoutPlugin).code;
123-
expect(uglify(actual)).to.equal(uglify(expected));
124-
});
125-
126-
it('removes options.attributes bools', () => {
127-
const code = '<p selenium-id={false} useless-attr={true}></p>';
128-
const expectedCode = '<p></p>';
129-
const actual = transform(code, configWithValidAttributesOption).code;
130-
const expected = transform(expectedCode, configWithoutPlugin).code;
131-
expect(uglify(actual)).to.equal(uglify(expected));
98+
it('removes options.attributes bools', () => {
99+
const code = '<p selenium-id={false} useless-attr={true}></p>';
100+
const expectedCode = '<p></p>';
101+
const actual = transform(code, { useValidAttributes: true });
102+
const expected = transform(expectedCode);
103+
expect(uglify(actual)).to.equal(uglify(expected));
104+
});
105+
})
132106
});
133107
})
134-
});
108+
}
109+
110+
runTests(
111+
"babel6",
112+
(
113+
code,
114+
{
115+
useErroneousAttributes = false,
116+
useValidAttributes = false,
117+
usePlugin = false,
118+
attributes
119+
} = {}
120+
) => {
121+
let plugins;
122+
if (useErroneousAttributes) {
123+
plugins = [
124+
["./src", { attributes }],
125+
["transform-react-jsx", { pragma: "j" }],
126+
["transform-es2015-arrow-functions", {}]
127+
];
128+
} else if (useValidAttributes) {
129+
plugins = [
130+
["./src", { attributes: ["selenium-id", "useless-attr"] }],
131+
["transform-react-jsx", { pragma: "j" }],
132+
["transform-es2015-arrow-functions", {}]
133+
];
134+
} else {
135+
plugins = [
136+
usePlugin && "./src",
137+
["transform-react-jsx", { pragma: "j" }],
138+
["transform-es2015-arrow-functions", {}]
139+
].filter(Boolean);
140+
}
141+
return babel6.transform(code, { plugins }).code;
142+
}
143+
);
144+
runTests(
145+
"babel7",
146+
(
147+
code,
148+
{
149+
useErroneousAttributes = false,
150+
useValidAttributes = false,
151+
usePlugin = false,
152+
attributes
153+
} = {}
154+
) => {
155+
let plugins
156+
if (useErroneousAttributes) {
157+
plugins = [
158+
['./src', { attributes }],
159+
["@babel/transform-react-jsx", { pragma: "j" }],
160+
["@babel/transform-arrow-functions", {}]
161+
]
162+
} else if (useValidAttributes) {
163+
plugins = [
164+
['./src', { attributes: ['selenium-id', 'useless-attr'] }],
165+
["@babel/transform-react-jsx", { pragma: "j" }],
166+
["@babel/transform-arrow-functions", {}]
167+
]
168+
} else {
169+
plugins = [
170+
usePlugin && "./src",
171+
["@babel/transform-react-jsx", { pragma: "j" }],
172+
["@babel/transform-arrow-functions", {}]
173+
].filter(Boolean)
174+
}
175+
return babel7.transformSync(code, { plugins }).code;
176+
}
177+
);

0 commit comments

Comments
 (0)