Skip to content
This repository was archived by the owner on Feb 9, 2023. It is now read-only.

Commit 299a9b6

Browse files
authored
Fix babel config issues (#258)
* Update Babel * Fix Babel fail if Babel config is present. Most likely fixes #247 and #250 * Use non-mutable operation
1 parent c44f6b1 commit 299a9b6

15 files changed

+754
-512
lines changed

.eslintignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
test/fixtures/*
1+
**/fixtures/**
22
coverage/**
3-
.nyc_output/**
3+
.nyc_output/**

.prettierignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
test/fixtures/*
1+
**/fixtures/**
22
coverage/**
33
.nyc_output/**

extract.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,15 @@ function loadBabelOpts(opts) {
144144

145145
if (Array.isArray(fileOpts[key]) && Array.isArray(opts.parserOpts[key])) {
146146
// combine arrays for plugins
147-
opts.parserOpts[key] = opts.parserOpts[key].concat(fileOpts[key]);
147+
// plugins in fileOpts could be string, array or object
148+
for (const plugin of fileOpts[key]) {
149+
const option =
150+
Array.isArray(plugin) || typeof plugin === 'string'
151+
? plugin
152+
: [plugin.key, plugin.options];
153+
154+
opts.parserOpts[key] = [...opts.parserOpts[key], option];
155+
}
148156
} else {
149157
// because some options need to be passed to parser also
150158
opts.parserOpts[key] = fileOpts[key];

package-lock.json

Lines changed: 361 additions & 344 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,12 @@
6767
"!**/.{prettier,eslint,mocha}rc.{js,cjs}"
6868
],
6969
"testMatch": [
70-
"**/test/*.js"
70+
"**/test/*.js",
71+
"**/test/**/*.test.js"
7172
]
7273
},
7374
"dependencies": {
74-
"@babel/core": ">=7.15.5"
75+
"@babel/core": "^7.17.9"
7576
},
7677
"devDependencies": {
7778
"@stylelint/prettier-config": "^2.0.0",

test/babel-config/.babelrc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"plugins": ["./babel-plugin-my-plugin.js"]
3+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
const syntax = require('../../');
5+
6+
it('works with babel config present', () => {
7+
const file = require.resolve('./fixtures/styled-components');
8+
let code = fs.readFileSync(file);
9+
10+
const document = syntax.parse(code, {
11+
from: file,
12+
});
13+
14+
code = code.toString();
15+
expect(document.toString()).toBe(code);
16+
expect(document.source).toHaveProperty('lang', 'jsx');
17+
18+
expect(document.nodes).toHaveLength(1);
19+
expect(document.first.nodes).toHaveLength(8);
20+
21+
expect(document.first.nodes[0]).toHaveProperty('type', 'comment');
22+
expect(document.first.nodes[1]).toHaveProperty('type', 'decl');
23+
expect(document.first.nodes[2]).toHaveProperty('type', 'decl');
24+
expect(document.first.nodes[3]).toHaveProperty('type', 'decl');
25+
expect(document.first.nodes[4]).toHaveProperty('type', 'decl');
26+
expect(document.first.nodes[5]).toHaveProperty('type', 'decl');
27+
expect(document.first.nodes[6]).toHaveProperty('type', 'decl');
28+
expect(document.first.nodes[7]).toHaveProperty('type', 'decl');
29+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
module.exports = function () {
4+
return {
5+
visitor: {
6+
Identifier() {},
7+
},
8+
};
9+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"use strict";
2+
const styled = require("styled-components");
3+
const Button = styled.button`
4+
/* Adapt the colours based on primary prop */
5+
background: ${props => props.primary ? "palevioletred" : "white"};
6+
color: ${props => props.primary ? "white" : "palevioletred"};
7+
8+
font-size: 1em;
9+
margin: 1em;
10+
padding: 0.25em 1em;
11+
border: 2px solid palevioletred;
12+
border-radius: 3px;
13+
`;

test/fixtures/emotion-10.jsx.json

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,13 @@
147147
},
148148
"start": {
149149
"line": 13,
150-
"column": 2
150+
"column": 2,
151+
"index": 251
151152
},
152153
"end": {
153154
"line": 13,
154-
"column": 18
155+
"column": 18,
156+
"index": 267
155157
}
156158
}
157159
}
@@ -162,11 +164,13 @@
162164
},
163165
"start": {
164166
"line": 12,
165-
"column": 1
167+
"column": 1,
168+
"index": 247
166169
},
167170
"end": {
168171
"line": 14,
169-
"column": 2
172+
"column": 2,
173+
"index": 271
170174
}
171175
}
172176
}
@@ -222,11 +226,13 @@
222226
},
223227
"start": {
224228
"line": 15,
225-
"column": 13
229+
"column": 13,
230+
"index": 286
226231
},
227232
"end": {
228233
"line": 15,
229-
"column": 29
234+
"column": 29,
235+
"index": 302
230236
}
231237
}
232238
}
@@ -237,11 +243,13 @@
237243
},
238244
"start": {
239245
"line": 15,
240-
"column": 11
246+
"column": 11,
247+
"index": 284
241248
},
242249
"end": {
243250
"line": 15,
244-
"column": 31
251+
"column": 31,
252+
"index": 304
245253
}
246254
}
247255
}
@@ -340,11 +348,13 @@
340348
},
341349
"start": {
342350
"line": 27,
343-
"column": 4
351+
"column": 4,
352+
"index": 476
344353
},
345354
"end": {
346355
"line": 27,
347-
"column": 23
356+
"column": 23,
357+
"index": 495
348358
}
349359
}
350360
}
@@ -355,11 +365,13 @@
355365
},
356366
"start": {
357367
"line": 26,
358-
"column": 14
368+
"column": 14,
369+
"index": 470
359370
},
360371
"end": {
361372
"line": 28,
362-
"column": 4
373+
"column": 4,
374+
"index": 501
363375
}
364376
}
365377
}

0 commit comments

Comments
 (0)