Skip to content

Commit 218a73c

Browse files
authored
Update babel. catch @babel/traverse error. (stylelint#27)
1 parent d706415 commit 218a73c

File tree

3 files changed

+35
-19
lines changed

3 files changed

+35
-19
lines changed

extract.js

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
"use strict";
2-
const traverse = require("@babel/traverse").default;
3-
const t = require("@babel/types");
4-
const parse = require("@babel/parser").parse;
2+
const {
3+
parse,
4+
types,
5+
traverse,
6+
loadOptions,
7+
} = require("@babel/core");
58
const getTemplate = require("./get-template");
69
const loadSyntax = require("postcss-syntax/load-syntax");
710

@@ -67,7 +70,7 @@ function getSourceType (filename) {
6770
return "module";
6871
}
6972
try {
70-
return require("@babel/core").loadOptions({
73+
return loadOptions({
7174
filename,
7275
}).sourceType;
7376
} catch (ex) {
@@ -82,13 +85,19 @@ function getOptions (opts) {
8285
sourceFilename: filename,
8386
sourceType: getSourceType(filename) || "unambiguous",
8487
plugins,
88+
allowImportExportEverywhere: true,
89+
allowAwaitOutsideFunction: true,
90+
allowReturnOutsideFunction: true,
91+
allowSuperOutsideMethod: true,
8592
};
8693
}
8794

8895
function literalParser (source, opts, styles) {
8996
let ast;
9097
try {
91-
ast = parse(source, getOptions(opts));
98+
ast = parse(source, {
99+
parserOpts: getOptions(opts),
100+
});
92101
} catch (ex) {
93102
// console.error(ex);
94103
return styles || [];
@@ -134,12 +143,12 @@ function literalParser (source, opts, styles) {
134143
}
135144

136145
function setSpecifier (id, nameSpace) {
137-
if (t.isIdentifier(id)) {
146+
if (types.isIdentifier(id)) {
138147
specifiers.set(id.name, nameSpace);
139148
specifiers.set(id, nameSpace);
140-
} else if (t.isObjectPattern(id)) {
149+
} else if (types.isObjectPattern(id)) {
141150
id.properties.forEach(property => {
142-
if (t.isObjectProperty(property)) {
151+
if (types.isObjectProperty(property)) {
143152
const key = property.key;
144153
nameSpace = nameSpace.concat(key.name || key.value);
145154
id = property.value;
@@ -148,7 +157,7 @@ function literalParser (source, opts, styles) {
148157
}
149158
setSpecifier(id, nameSpace);
150159
});
151-
} else if (t.isArrayPattern(id)) {
160+
} else if (types.isArrayPattern(id)) {
152161
id.elements.forEach((element, i) => {
153162
setSpecifier(element, nameSpace.concat(String(i)));
154163
});
@@ -226,7 +235,7 @@ function literalParser (source, opts, styles) {
226235
variableDeclarator.set(path.node.id, path.node.init ? [path.get("init")] : []);
227236
},
228237
AssignmentExpression: (path) => {
229-
if (t.isIdentifier(path.node.left) && t.isObjectExpression(path.node.right)) {
238+
if (types.isIdentifier(path.node.left) && types.isObjectExpression(path.node.right)) {
230239
const identifier = path.scope.getBindingIdentifier(path.node.left.name);
231240
const variable = variableDeclarator.get(identifier);
232241
const valuePath = path.get("right");
@@ -239,8 +248,8 @@ function literalParser (source, opts, styles) {
239248
},
240249
CallExpression: (path) => {
241250
const callee = path.node.callee;
242-
if (t.isIdentifier(callee, { name: "require" }) && !path.scope.getBindingIdentifier(callee.name)) {
243-
path.node.arguments.filter(t.isStringLiteral).forEach(arg => {
251+
if (types.isIdentifier(callee, { name: "require" }) && !path.scope.getBindingIdentifier(callee.name)) {
252+
path.node.arguments.filter(types.isStringLiteral).forEach(arg => {
244253
const moduleId = arg.value;
245254
const nameSpace = [moduleId];
246255
let currPath = path;
@@ -251,7 +260,7 @@ function literalParser (source, opts, styles) {
251260
if (id) {
252261
id = path.scope.getBindingIdentifier(id.name) || id;
253262
} else {
254-
if (t.isIdentifier(currPath.parent.property)) {
263+
if (types.isIdentifier(currPath.parent.property)) {
255264
nameSpace.push(currPath.parent.property.name);
256265
}
257266
currPath = currPath.parentPath;

package.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@
4141
"test": "nyc mocha --no-timeouts"
4242
},
4343
"dependencies": {
44-
"@babel/generator": "^7.0.0-beta.54",
45-
"@babel/parser": "^7.0.0-beta.54",
46-
"@babel/traverse": "^7.0.0-beta.54",
47-
"@babel/types": "^7.0.0-beta.54"
44+
"@babel/core": "^7.0.0-rc.1"
4845
},
4946
"optionalDependencies": {
5047
"postcss-styled": ">=0.32.0"
@@ -54,13 +51,13 @@
5451
"postcss-syntax": ">=0.32.0"
5552
},
5653
"devDependencies": {
57-
"autoprefixer": "^9.0.1",
54+
"autoprefixer": "^9.1.1",
5855
"chai": "^4.1.2",
5956
"codecov": "^3.0.4",
6057
"json5": "^1.0.1",
6158
"mocha": "^5.2.0",
6259
"nyc": "^12.0.2",
63-
"postcss": "^7.0.1",
60+
"postcss": "^7.0.2",
6461
"postcss-parser-tests": "^6.3.0",
6562
"postcss-safe-parser": "^4.0.1",
6663
"postcss-syntax": ">=0.32.0"

test/styled-components.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ describe("styled-components", () => {
5656
expect(document.nodes).to.have.lengthOf(0);
5757
});
5858

59+
it("skip @babel/traverse error", () => {
60+
const code = "let a;let a";
61+
const document = syntax.parse(code, {
62+
from: "traverse_error.js",
63+
});
64+
expect(document.toString()).to.equal(code);
65+
expect(document.source).to.haveOwnProperty("lang", "jsx");
66+
expect(document.nodes).to.have.lengthOf(0);
67+
});
68+
5969
it("illegal template literal", () => {
6070
const code = [
6171
"const styled = require(\"styled-components\");",

0 commit comments

Comments
 (0)