@next-core/cook 1.4.1-alpha.0 → 1.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +36 -2
- package/dist/cjs/AnalysisContext.js.map +1 -1
- package/dist/cjs/index.js +39 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/lint.js +79 -78
- package/dist/cjs/lint.js.map +1 -1
- package/dist/cjs/parse.js +21 -0
- package/dist/cjs/parse.js.map +1 -1
- package/dist/cjs/precook.js +76 -77
- package/dist/cjs/precook.js.map +1 -1
- package/dist/esm/AnalysisContext.js.map +1 -1
- package/dist/esm/cook.js +6 -5
- package/dist/esm/cook.js.map +1 -1
- package/dist/esm/index.js +3 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/lint.js +84 -82
- package/dist/esm/lint.js.map +1 -1
- package/dist/esm/parse.js +26 -3
- package/dist/esm/parse.js.map +1 -1
- package/dist/esm/precook.js +83 -82
- package/dist/esm/precook.js.map +1 -1
- package/dist/esm/precookFunction.js +3 -2
- package/dist/esm/precookFunction.js.map +1 -1
- package/dist/types/AnalysisContext.d.ts +2 -1
- package/dist/types/index.d.ts +3 -0
- package/dist/types/lint.d.ts +3 -2
- package/dist/types/parse.d.ts +8 -1
- package/dist/types/precook.d.ts +14 -3
- package/package.json +5 -5
package/CHANGELOG.md
CHANGED
|
@@ -3,11 +3,45 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
-
## [1.
|
|
6
|
+
## [1.6.2](https://github.com/easyops-cn/next-core/compare/@next-core/cook@1.6.1...@next-core/cook@1.6.2) (2021-12-17)
|
|
7
7
|
|
|
8
8
|
**Note:** Version bump only for package @next-core/cook
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
## [1.6.1](https://github.com/easyops-cn/next-core/compare/@next-core/cook@1.6.0...@next-core/cook@1.6.1) (2021-12-03)
|
|
15
|
+
|
|
16
|
+
**Note:** Version bump only for package @next-core/cook
|
|
17
|
+
|
|
18
|
+
# [1.6.0](https://github.com/easyops-cn/next-core/compare/@next-core/cook@1.5.0...@next-core/cook@1.6.0) (2021-11-29)
|
|
19
|
+
|
|
20
|
+
### Features
|
|
21
|
+
|
|
22
|
+
- support visit node with parent ([80f1ada](https://github.com/easyops-cn/next-core/commit/80f1ada97e1b720f787753fab4eedf618c08f36b))
|
|
23
|
+
|
|
24
|
+
# [1.5.0](https://github.com/easyops-cn/next-core/compare/@next-core/cook@1.4.0...@next-core/cook@1.5.0) (2021-11-26)
|
|
25
|
+
|
|
26
|
+
### Features
|
|
27
|
+
|
|
28
|
+
- support hooks for before visit global or unknown nodes ([50d770c](https://github.com/easyops-cn/next-core/commit/50d770c45a8194c12c3764f53d75a1a9519b790e))
|
|
29
|
+
|
|
30
|
+
# [1.4.0](https://github.com/easyops-cn/next-core/compare/@next-core/cook@1.3.9...@next-core/cook@1.4.0) (2021-11-25)
|
|
31
|
+
|
|
32
|
+
### Features
|
|
33
|
+
|
|
34
|
+
- support parseForAnalysis ([681de0a](https://github.com/easyops-cn/next-core/commit/681de0aef914b4e30d428180cad54cc0f9882b59))
|
|
35
|
+
|
|
36
|
+
## [1.3.9](https://github.com/easyops-cn/next-core/compare/@next-core/cook@1.3.8...@next-core/cook@1.3.9) (2021-11-23)
|
|
37
|
+
|
|
38
|
+
**Note:** Version bump only for package @next-core/cook
|
|
39
|
+
|
|
40
|
+
## [1.3.8](https://github.com/easyops-cn/next-core/compare/@next-core/cook@1.3.7...@next-core/cook@1.3.8) (2021-11-18)
|
|
41
|
+
|
|
42
|
+
**Note:** Version bump only for package @next-core/cook
|
|
43
|
+
|
|
44
|
+
## [1.3.7](https://github.com/easyops-cn/next-core/compare/@next-core/cook@1.3.6...@next-core/cook@1.3.7) (2021-11-11)
|
|
11
45
|
|
|
12
46
|
**Note:** Version bump only for package @next-core/cook
|
|
13
47
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/AnalysisContext.ts"],"names":["AnalysisContext","AnalysisEnvironment","constructor","outer","Set","OuterEnv","HasBinding","name","bindingSet","has","CreateBinding","add"],"mappings":";;;;;;;;;;;
|
|
1
|
+
{"version":3,"sources":["../../src/AnalysisContext.ts"],"names":["AnalysisContext","AnalysisEnvironment","constructor","outer","Set","OuterEnv","HasBinding","name","bindingSet","has","CreateBinding","add"],"mappings":";;;;;;;;;;;AAQA;AACO,MAAMA,eAAN,CAAsB;AAAA;AAAA;AAAA;AAAA;;AAAA,C,CAK7B;;;;;AACO,MAAMC,mBAAN,CAA0B;AAI/BC,EAAAA,WAAW,CAACC,KAAD,EAA6B;AAAA;AAAA,sDAFV,IAAIC,GAAJ,EAEU;AACtC,SAAKC,QAAL,GAAgBF,KAAhB;AACD;;AAEDG,EAAAA,UAAU,CAACC,IAAD,EAAwB;AAChC,WAAO,KAAKC,UAAL,CAAgBC,GAAhB,CAAoBF,IAApB,CAAP;AACD;;AAEDG,EAAAA,aAAa,CAACH,IAAD,EAAqB;AAChC,SAAKC,UAAL,CAAgBG,GAAhB,CAAoBJ,IAApB;AACD;;AAd8B","sourcesContent":["import {\n ArrowFunctionExpression,\n Expression,\n FunctionDeclaration,\n FunctionExpression,\n Statement,\n} from \"@babel/types\";\n\n// https://tc39.es/ecma262/#sec-execution-contexts\nexport class AnalysisContext {\n VariableEnvironment: AnalysisEnvironment;\n LexicalEnvironment: AnalysisEnvironment;\n}\n\n// https://tc39.es/ecma262/#sec-environment-records\nexport class AnalysisEnvironment {\n readonly OuterEnv: AnalysisEnvironment;\n private readonly bindingSet = new Set<string>();\n\n constructor(outer: AnalysisEnvironment) {\n this.OuterEnv = outer;\n }\n\n HasBinding(name: string): boolean {\n return this.bindingSet.has(name);\n }\n\n CreateBinding(name: string): void {\n this.bindingSet.add(name);\n }\n}\n\nexport interface AnalysisFunctionObject {\n Function: FunctionDeclaration | FunctionExpression | ArrowFunctionExpression;\n FormalParameters: FunctionDeclaration[\"params\"];\n ECMAScriptCode: Statement[] | Expression;\n Environment: AnalysisEnvironment;\n}\n"],"file":"AnalysisContext.js"}
|
package/dist/cjs/index.js
CHANGED
|
@@ -3,11 +3,28 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
+
var _exportNames = {
|
|
7
|
+
parseForAnalysis: true,
|
|
8
|
+
tokTypes: true
|
|
9
|
+
};
|
|
10
|
+
Object.defineProperty(exports, "parseForAnalysis", {
|
|
11
|
+
enumerable: true,
|
|
12
|
+
get: function () {
|
|
13
|
+
return _parse.parseForAnalysis;
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
Object.defineProperty(exports, "tokTypes", {
|
|
17
|
+
enumerable: true,
|
|
18
|
+
get: function () {
|
|
19
|
+
return _parser.tokTypes;
|
|
20
|
+
}
|
|
21
|
+
});
|
|
6
22
|
|
|
7
23
|
var _cook = require("./cook");
|
|
8
24
|
|
|
9
25
|
Object.keys(_cook).forEach(function (key) {
|
|
10
26
|
if (key === "default" || key === "__esModule") return;
|
|
27
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
11
28
|
if (key in exports && exports[key] === _cook[key]) return;
|
|
12
29
|
Object.defineProperty(exports, key, {
|
|
13
30
|
enumerable: true,
|
|
@@ -21,6 +38,7 @@ var _lint = require("./lint");
|
|
|
21
38
|
|
|
22
39
|
Object.keys(_lint).forEach(function (key) {
|
|
23
40
|
if (key === "default" || key === "__esModule") return;
|
|
41
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
24
42
|
if (key in exports && exports[key] === _lint[key]) return;
|
|
25
43
|
Object.defineProperty(exports, key, {
|
|
26
44
|
enumerable: true,
|
|
@@ -30,10 +48,27 @@ Object.keys(_lint).forEach(function (key) {
|
|
|
30
48
|
});
|
|
31
49
|
});
|
|
32
50
|
|
|
51
|
+
var _parse = require("./parse");
|
|
52
|
+
|
|
53
|
+
var _precook = require("./precook");
|
|
54
|
+
|
|
55
|
+
Object.keys(_precook).forEach(function (key) {
|
|
56
|
+
if (key === "default" || key === "__esModule") return;
|
|
57
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
58
|
+
if (key in exports && exports[key] === _precook[key]) return;
|
|
59
|
+
Object.defineProperty(exports, key, {
|
|
60
|
+
enumerable: true,
|
|
61
|
+
get: function () {
|
|
62
|
+
return _precook[key];
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
33
67
|
var _precookFunction = require("./precookFunction");
|
|
34
68
|
|
|
35
69
|
Object.keys(_precookFunction).forEach(function (key) {
|
|
36
70
|
if (key === "default" || key === "__esModule") return;
|
|
71
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
37
72
|
if (key in exports && exports[key] === _precookFunction[key]) return;
|
|
38
73
|
Object.defineProperty(exports, key, {
|
|
39
74
|
enumerable: true,
|
|
@@ -47,6 +82,7 @@ var _preevaluate = require("./preevaluate");
|
|
|
47
82
|
|
|
48
83
|
Object.keys(_preevaluate).forEach(function (key) {
|
|
49
84
|
if (key === "default" || key === "__esModule") return;
|
|
85
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
50
86
|
if (key in exports && exports[key] === _preevaluate[key]) return;
|
|
51
87
|
Object.defineProperty(exports, key, {
|
|
52
88
|
enumerable: true,
|
|
@@ -60,6 +96,7 @@ var _interfaces = require("./interfaces");
|
|
|
60
96
|
|
|
61
97
|
Object.keys(_interfaces).forEach(function (key) {
|
|
62
98
|
if (key === "default" || key === "__esModule") return;
|
|
99
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
63
100
|
if (key in exports && exports[key] === _interfaces[key]) return;
|
|
64
101
|
Object.defineProperty(exports, key, {
|
|
65
102
|
enumerable: true,
|
|
@@ -68,4 +105,6 @@ Object.keys(_interfaces).forEach(function (key) {
|
|
|
68
105
|
}
|
|
69
106
|
});
|
|
70
107
|
});
|
|
108
|
+
|
|
109
|
+
var _parser = require("@babel/parser");
|
|
71
110
|
//# sourceMappingURL=index.js.map
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;AACA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;AACA;;AACA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;AACA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;AACA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;AACA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;AACA","sourcesContent":["export * from \"./cook\";\nexport * from \"./lint\";\nexport { parseForAnalysis } from \"./parse\";\nexport * from \"./precook\";\nexport * from \"./precookFunction\";\nexport * from \"./preevaluate\";\nexport * from \"./interfaces\";\nexport { tokTypes } from \"@babel/parser\";\n"],"file":"index.js"}
|
package/dist/cjs/lint.js
CHANGED
|
@@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.lint = lint;
|
|
7
7
|
|
|
8
|
-
var
|
|
8
|
+
var _parse = require("./parse");
|
|
9
9
|
|
|
10
10
|
var _precook = require("./precook");
|
|
11
11
|
|
|
@@ -15,17 +15,11 @@ function lint(source, {
|
|
|
15
15
|
rules
|
|
16
16
|
} = {}) {
|
|
17
17
|
const errors = [];
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
strictMode: true,
|
|
24
|
-
attachComment: false,
|
|
25
|
-
// Allow export/import declarations to make linter handle errors.
|
|
26
|
-
sourceType: "unambiguous"
|
|
27
|
-
});
|
|
28
|
-
} catch (e) {
|
|
18
|
+
const file = typeof source === "string" ? (0, _parse.parseForAnalysis)(source, {
|
|
19
|
+
typescript
|
|
20
|
+
}) : source;
|
|
21
|
+
|
|
22
|
+
if (!file) {
|
|
29
23
|
// Return no errors if parse failed.
|
|
30
24
|
return errors;
|
|
31
25
|
}
|
|
@@ -81,93 +75,100 @@ function lint(source, {
|
|
|
81
75
|
}
|
|
82
76
|
});
|
|
83
77
|
} else {
|
|
84
|
-
const FunctionVisitor = node => {
|
|
85
|
-
if (node.async || node.generator) {
|
|
86
|
-
errors.push({
|
|
87
|
-
type: "SyntaxError",
|
|
88
|
-
message: `${node.async ? "Async" : "Generator"} function is not allowed`,
|
|
89
|
-
loc: node.loc
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
};
|
|
93
|
-
|
|
94
78
|
(0, _precook.precook)(func, {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
if (node.value === null) {
|
|
103
|
-
errors.push({
|
|
104
|
-
type: "SyntaxError",
|
|
105
|
-
message: "Invalid regular expression",
|
|
106
|
-
loc: node.loc
|
|
107
|
-
});
|
|
108
|
-
} else if (node.regex.flags.includes("u")) {
|
|
109
|
-
errors.push({
|
|
110
|
-
type: "SyntaxError",
|
|
111
|
-
message: "Unsupported unicode flag in regular expression",
|
|
112
|
-
loc: node.loc
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
},
|
|
117
|
-
|
|
118
|
-
ObjectExpression(node) {
|
|
119
|
-
for (const prop of node.properties) {
|
|
120
|
-
if (prop.type === "Property") {
|
|
121
|
-
if (prop.kind !== "init") {
|
|
79
|
+
hooks: {
|
|
80
|
+
beforeVisit(node) {
|
|
81
|
+
switch (node.type) {
|
|
82
|
+
case "ArrowFunctionExpression":
|
|
83
|
+
case "FunctionDeclaration":
|
|
84
|
+
case "FunctionExpression":
|
|
85
|
+
if (node.async || node.generator) {
|
|
122
86
|
errors.push({
|
|
123
87
|
type: "SyntaxError",
|
|
124
|
-
message: "
|
|
125
|
-
loc:
|
|
88
|
+
message: `${node.async ? "Async" : "Generator"} function is not allowed`,
|
|
89
|
+
loc: node.loc
|
|
126
90
|
});
|
|
127
|
-
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
break;
|
|
94
|
+
|
|
95
|
+
case "Literal":
|
|
96
|
+
if (node.regex) {
|
|
97
|
+
if (node.value === null) {
|
|
98
|
+
errors.push({
|
|
99
|
+
type: "SyntaxError",
|
|
100
|
+
message: "Invalid regular expression",
|
|
101
|
+
loc: node.loc
|
|
102
|
+
});
|
|
103
|
+
} else if (node.regex.flags.includes("u")) {
|
|
104
|
+
errors.push({
|
|
105
|
+
type: "SyntaxError",
|
|
106
|
+
message: "Unsupported unicode flag in regular expression",
|
|
107
|
+
loc: node.loc
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
break;
|
|
113
|
+
|
|
114
|
+
case "ObjectExpression":
|
|
115
|
+
for (const prop of node.properties) {
|
|
116
|
+
if (prop.type === "Property") {
|
|
117
|
+
if (prop.kind !== "init") {
|
|
118
|
+
errors.push({
|
|
119
|
+
type: "SyntaxError",
|
|
120
|
+
message: "Unsupported object getter/setter property",
|
|
121
|
+
loc: prop.loc
|
|
122
|
+
});
|
|
123
|
+
} else if (!prop.computed && prop.key.type === "Identifier" && prop.key.name === "__proto__") {
|
|
124
|
+
errors.push({
|
|
125
|
+
type: "TypeError",
|
|
126
|
+
message: "Setting '__proto__' property is not allowed",
|
|
127
|
+
loc: prop.key.loc
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
break;
|
|
134
|
+
|
|
135
|
+
case "VariableDeclaration":
|
|
136
|
+
if (node.kind === "var" && rules !== null && rules !== void 0 && rules.noVar) {
|
|
128
137
|
errors.push({
|
|
129
|
-
type: "
|
|
130
|
-
message: "
|
|
131
|
-
loc:
|
|
138
|
+
type: "SyntaxError",
|
|
139
|
+
message: "Var declaration is not recommended, use `let` or `const` instead",
|
|
140
|
+
loc: {
|
|
141
|
+
start: node.loc.start,
|
|
142
|
+
end: {
|
|
143
|
+
line: node.loc.end.line,
|
|
144
|
+
// Only decorate the "var".
|
|
145
|
+
column: node.loc.start.column + 3
|
|
146
|
+
}
|
|
147
|
+
}
|
|
132
148
|
});
|
|
133
149
|
}
|
|
134
|
-
|
|
150
|
+
|
|
151
|
+
break;
|
|
135
152
|
}
|
|
136
153
|
},
|
|
137
154
|
|
|
138
|
-
|
|
139
|
-
if (node.
|
|
155
|
+
beforeVisitGlobal(node) {
|
|
156
|
+
if (node.name === "arguments") {
|
|
140
157
|
errors.push({
|
|
141
158
|
type: "SyntaxError",
|
|
142
|
-
message: "
|
|
143
|
-
loc:
|
|
144
|
-
start: node.loc.start,
|
|
145
|
-
end: {
|
|
146
|
-
line: node.loc.end.line,
|
|
147
|
-
// Only decorate the "var".
|
|
148
|
-
column: node.loc.start.column + 3
|
|
149
|
-
}
|
|
150
|
-
}
|
|
159
|
+
message: "Use the rest parameters instead of 'arguments'",
|
|
160
|
+
loc: node.loc
|
|
151
161
|
});
|
|
152
162
|
}
|
|
153
163
|
},
|
|
154
164
|
|
|
155
|
-
|
|
165
|
+
beforeVisitUnknown(node) {
|
|
156
166
|
errors.push({
|
|
157
167
|
type: "SyntaxError",
|
|
158
168
|
message: `Unsupported syntax: \`${node.type}\``,
|
|
159
169
|
loc: node.loc
|
|
160
170
|
});
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
__GlobalVariable(node) {
|
|
164
|
-
if (node.name === "arguments") {
|
|
165
|
-
errors.push({
|
|
166
|
-
type: "SyntaxError",
|
|
167
|
-
message: "Use the rest parameters instead of 'arguments'",
|
|
168
|
-
loc: node.loc
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
+
return true;
|
|
171
172
|
}
|
|
172
173
|
|
|
173
174
|
}
|
package/dist/cjs/lint.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/lint.ts"],"names":["lint","source","typescript","rules","errors","file","plugins","filter","Boolean","strictMode","attachComment","sourceType","e","body","program","jsNodes","node","type","startsWith","test","push","message","loc","func","isFunctionDeclaration","unshift","start","line","column","end","FunctionVisitor","async","generator","visitors","ArrowFunctionExpression","FunctionDeclaration","FunctionExpression","Literal","regex","value","flags","includes","ObjectExpression","prop","properties","kind","computed","key","name","VariableDeclaration","noVar","__UnknownNode","__GlobalVariable"],"mappings":";;;;;;;AAAA;;AAkBA;;AAaA;AACO,SAASA,IAAT,CACLC,MADK,EAEL;AAAEC,EAAAA,UAAF;AAAcC,EAAAA;AAAd,IAAqC,EAFhC,EAGQ;AACb,QAAMC,MAAmB,GAAG,EAA5B;AACA,MAAIC,IAAJ;;AACA,MAAI;AACFA,IAAAA,IAAI,GAAG,mBAAMJ,MAAN,EAAc;AACnBK,MAAAA,OAAO,EAAE,CAAC,QAAD,EAAWJ,UAAU,IAAI,YAAzB,EAAuCK,MAAvC,CACPC,OADO,CADU;AAInBC,MAAAA,UAAU,EAAE,IAJO;AAKnBC,MAAAA,aAAa,EAAE,KALI;AAMnB;AACAC,MAAAA,UAAU,EAAE;AAPO,KAAd,CAAP;AASD,GAVD,CAUE,OAAOC,CAAP,EAAU;AACV;AACA,WAAOR,MAAP;AACD;;AACD,QAAMS,IAAI,GAAGR,IAAI,CAACS,OAAL,CAAaD,IAA1B;AACA,QAAME,OAAoB,GAAG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AeE,MAAf,GAAwB;AAH7B;AAFF;AAJK,aAAZ;AAaD;AACF,SA5DO;;AA6DRuB,QAAAA,aAAa,CAACnC,IAAD,EAAmB;AAC9BZ,UAAAA,MAAM,CAACgB,IAAP,CAAY;AACVH,YAAAA,IAAI,EAAE,aADI;AAEVI,YAAAA,OAAO,EAAG,yBAAwBL,IAAI,CAACC,IAAK,IAFlC;AAGVK,YAAAA,GAAG,EAAEN,IAAI,CAACM;AAHA,WAAZ;AAKD,SAnEO;;AAoER8B,QAAAA,gBAAgB,CAACpC,IAAD,EAAmB;AACjC,cAAIA,IAAI,CAACgC,IAAL,KAAc,WAAlB,EAA+B;AAC7B5C,YAAAA,MAAM,CAACgB,IAAP,CAAY;AACVH,cAAAA,IAAI,EAAE,aADI;AAEVI,cAAAA,OAAO,EAAE,gDAFC;AAGVC,cAAAA,GAAG,EAAEN,IAAI,CAACM;AAHA,aAAZ;AAKD;AACF;;AA5EO;AADE,KAAd;AAgFD;;AACD,SAAOlB,MAAP;AACD","sourcesContent":["import { parse, ParseResult, ParserPlugin } from \"@babel/parser\";\nimport {\n ArrowFunctionExpression,\n File,\n FunctionDeclaration,\n FunctionExpression,\n Identifier,\n SourceLocation,\n Statement,\n VariableDeclaration,\n} from \"@babel/types\";\nimport {\n EstreeLiteral,\n CookRules,\n EstreeObjectExpression,\n EstreeVisitorFn,\n EstreeNode,\n} from \"./interfaces\";\nimport { precook } from \"./precook\";\n\nexport interface LintOptions {\n typescript?: boolean;\n rules?: CookRules;\n}\n\nexport interface LintError {\n type: \"SyntaxError\" | \"TypeError\";\n message: string;\n loc: SourceLocation;\n}\n\n/** For next-core internal or devtools usage only. */\nexport function lint(\n source: string,\n { typescript, rules }: LintOptions = {}\n): LintError[] {\n const errors: LintError[] = [];\n let file: ParseResult<File>;\n try {\n file = parse(source, {\n plugins: [\"estree\", typescript && \"typescript\"].filter(\n Boolean\n ) as ParserPlugin[],\n strictMode: true,\n attachComment: false,\n // Allow export/import declarations to make linter handle errors.\n sourceType: \"unambiguous\",\n });\n } catch (e) {\n // Return no errors if parse failed.\n return errors;\n }\n const body = file.program.body;\n const jsNodes: Statement[] = typescript ? [] : body;\n if (typescript) {\n for (const node of body) {\n if (node.type.startsWith(\"TS\")) {\n if (/Enum|Import|Export/.test(node.type)) {\n errors.push({\n type: \"SyntaxError\",\n message: `Unsupported TypeScript syntax: \\`${node.type}\\``,\n loc: node.loc,\n });\n }\n } else {\n jsNodes.push(node);\n }\n }\n }\n let func: FunctionDeclaration;\n for (const node of jsNodes) {\n const isFunctionDeclaration = node.type === \"FunctionDeclaration\";\n if (isFunctionDeclaration && !func) {\n func = node;\n } else {\n errors.push({\n type: \"SyntaxError\",\n message: isFunctionDeclaration\n ? \"Expect a single function declaration\"\n : `\\`${node.type}\\` is not allowed in top level`,\n loc: node.loc,\n });\n }\n }\n if (!func) {\n errors.unshift({\n type: \"SyntaxError\",\n message: \"Function declaration not found\",\n loc: {\n start: { line: 1, column: 0 },\n end: { line: 1, column: 0 },\n },\n });\n } else {\n const FunctionVisitor: EstreeVisitorFn = (\n node: FunctionDeclaration | FunctionExpression | ArrowFunctionExpression\n ) => {\n if (node.async || node.generator) {\n errors.push({\n type: \"SyntaxError\",\n message: `${\n node.async ? \"Async\" : \"Generator\"\n } function is not allowed`,\n loc: node.loc,\n });\n }\n };\n precook(func, {\n visitors: {\n ArrowFunctionExpression: FunctionVisitor,\n FunctionDeclaration: FunctionVisitor,\n FunctionExpression: FunctionVisitor,\n Literal(node: EstreeLiteral) {\n if (node.regex) {\n if (node.value === null) {\n errors.push({\n type: \"SyntaxError\",\n message: \"Invalid regular expression\",\n loc: node.loc,\n });\n } else if (node.regex.flags.includes(\"u\")) {\n errors.push({\n type: \"SyntaxError\",\n message: \"Unsupported unicode flag in regular expression\",\n loc: node.loc,\n });\n }\n }\n },\n ObjectExpression(node: EstreeObjectExpression) {\n for (const prop of node.properties) {\n if (prop.type === \"Property\") {\n if (prop.kind !== \"init\") {\n errors.push({\n type: \"SyntaxError\",\n message: \"Unsupported object getter/setter property\",\n loc: prop.loc,\n });\n } else if (\n !prop.computed &&\n prop.key.type === \"Identifier\" &&\n prop.key.name === \"__proto__\"\n ) {\n errors.push({\n type: \"TypeError\",\n message: \"Setting '__proto__' property is not allowed\",\n loc: prop.key.loc,\n });\n }\n }\n }\n },\n VariableDeclaration(node: VariableDeclaration) {\n if (node.kind === \"var\" && rules?.noVar) {\n errors.push({\n type: \"SyntaxError\",\n message:\n \"Var declaration is not recommended, use `let` or `const` instead\",\n loc: {\n start: node.loc.start,\n end: {\n line: node.loc.end.line,\n // Only decorate the \"var\".\n column: node.loc.start.column + 3,\n },\n },\n });\n }\n },\n __UnknownNode(node: EstreeNode) {\n errors.push({\n type: \"SyntaxError\",\n message: `Unsupported syntax: \\`${node.type}\\``,\n loc: node.loc,\n });\n },\n __GlobalVariable(node: Identifier) {\n if (node.name === \"arguments\") {\n errors.push({\n type: \"SyntaxError\",\n message: \"Use the rest parameters instead of 'arguments'\",\n loc: node.loc,\n });\n }\n },\n },\n });\n }\n return errors;\n}\n"],"file":"lint.js"}
|
|
1
|
+
{"version":3,"sources":["../../src/lint.ts"],"names":["lint","source","typescript","rules","errors","file","body","program","jsNodes","node","type","startsWith","test","push","message","loc","func","isFunctionDeclaration","unshift","start","line","column","end","hooks","beforeVisit","async","generator","regex","value","flags","includes","prop","properties","kind","computed","key","name","noVar","beforeVisitGlobal","beforeVisitUnknown"],"mappings":";;;;;;;AAQA;;AACA;;AAaA;AACO,SAASA,IAAT,CACLC,MADK,EAEL;AAAEC,EAAAA,UAAF;AAAcC,EAAAA;AAAd,IAAqC,EAFhC,EAGQ;AACb,QAAMC,MAAmB,GAAG,EAA5B;AACA,QAAMC,IAAI,GACR,OAAOJ,MAAP,KAAkB,QAAlB,GACI,6BAAiBA,MAAjB,EAAyB;AAAEC,IAAAA;AAAF,GAAzB,CADJ,GAEID,MAHN;;AAIA,MAAI,CAACI,IAAL,EAAW;AACT;AACA,WAAOD,MAAP;A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F;AAJK,iBAAZ;AAaD;;AACD;AAtEJ;AAwED,SA1EI;;AA2ELiB,QAAAA,iBAAiB,CAAC7B,IAAD,EAAO;AACtB,cAAIA,IAAI,CAAC2B,IAAL,KAAc,WAAlB,EAA+B;AAC7BhC,YAAAA,MAAM,CAACS,IAAP,CAAY;AACVH,cAAAA,IAAI,EAAE,aADI;AAEVI,cAAAA,OAAO,EAAE,gDAFC;AAGVC,cAAAA,GAAG,EAAEN,IAAI,CAACM;AAHA,aAAZ;AAKD;AACF,SAnFI;;AAoFLwB,QAAAA,kBAAkB,CAAC9B,IAAD,EAAO;AACvBL,UAAAA,MAAM,CAACS,IAAP,CAAY;AACVH,YAAAA,IAAI,EAAE,aADI;AAEVI,YAAAA,OAAO,EAAG,yBAAwBL,IAAI,CAACC,IAAK,IAFlC;AAGVK,YAAAA,GAAG,EAAEN,IAAI,CAACM;AAHA,WAAZ;AAKA,iBAAO,IAAP;AACD;;AA3FI;AADK,KAAd;AA+FD;;AACD,SAAOX,MAAP;AACD","sourcesContent":["import { ParseResult } from \"@babel/parser\";\nimport {\n File,\n FunctionDeclaration,\n SourceLocation,\n Statement,\n} from \"@babel/types\";\nimport { CookRules } from \"./interfaces\";\nimport { parseForAnalysis } from \"./parse\";\nimport { precook } from \"./precook\";\n\nexport interface LintOptions {\n typescript?: boolean;\n rules?: CookRules;\n}\n\nexport interface LintError {\n type: \"SyntaxError\" | \"TypeError\";\n message: string;\n loc: SourceLocation;\n}\n\n/** For next-core internal or devtools usage only. */\nexport function lint(\n source: string | ParseResult<File>,\n { typescript, rules }: LintOptions = {}\n): LintError[] {\n const errors: LintError[] = [];\n const file =\n typeof source === \"string\"\n ? parseForAnalysis(source, { typescript })\n : source;\n if (!file) {\n // Return no errors if parse failed.\n return errors;\n }\n const body = file.program.body;\n const jsNodes: Statement[] = typescript ? [] : body;\n if (typescript) {\n for (const node of body) {\n if (node.type.startsWith(\"TS\")) {\n if (/Enum|Import|Export/.test(node.type)) {\n errors.push({\n type: \"SyntaxError\",\n message: `Unsupported TypeScript syntax: \\`${node.type}\\``,\n loc: node.loc,\n });\n }\n } else {\n jsNodes.push(node);\n }\n }\n }\n let func: FunctionDeclaration;\n for (const node of jsNodes) {\n const isFunctionDeclaration = node.type === \"FunctionDeclaration\";\n if (isFunctionDeclaration && !func) {\n func = node;\n } else {\n errors.push({\n type: \"SyntaxError\",\n message: isFunctionDeclaration\n ? \"Expect a single function declaration\"\n : `\\`${node.type}\\` is not allowed in top level`,\n loc: node.loc,\n });\n }\n }\n if (!func) {\n errors.unshift({\n type: \"SyntaxError\",\n message: \"Function declaration not found\",\n loc: {\n start: { line: 1, column: 0 },\n end: { line: 1, column: 0 },\n },\n });\n } else {\n precook(func, {\n hooks: {\n beforeVisit(node) {\n switch (node.type) {\n case \"ArrowFunctionExpression\":\n case \"FunctionDeclaration\":\n case \"FunctionExpression\":\n if (node.async || node.generator) {\n errors.push({\n type: \"SyntaxError\",\n message: `${\n node.async ? \"Async\" : \"Generator\"\n } function is not allowed`,\n loc: node.loc,\n });\n }\n break;\n case \"Literal\":\n if (node.regex) {\n if (node.value === null) {\n errors.push({\n type: \"SyntaxError\",\n message: \"Invalid regular expression\",\n loc: node.loc,\n });\n } else if (node.regex.flags.includes(\"u\")) {\n errors.push({\n type: \"SyntaxError\",\n message: \"Unsupported unicode flag in regular expression\",\n loc: node.loc,\n });\n }\n }\n break;\n case \"ObjectExpression\":\n for (const prop of node.properties) {\n if (prop.type === \"Property\") {\n if (prop.kind !== \"init\") {\n errors.push({\n type: \"SyntaxError\",\n message: \"Unsupported object getter/setter property\",\n loc: prop.loc,\n });\n } else if (\n !prop.computed &&\n prop.key.type === \"Identifier\" &&\n prop.key.name === \"__proto__\"\n ) {\n errors.push({\n type: \"TypeError\",\n message: \"Setting '__proto__' property is not allowed\",\n loc: prop.key.loc,\n });\n }\n }\n }\n break;\n case \"VariableDeclaration\":\n if (node.kind === \"var\" && rules?.noVar) {\n errors.push({\n type: \"SyntaxError\",\n message:\n \"Var declaration is not recommended, use `let` or `const` instead\",\n loc: {\n start: node.loc.start,\n end: {\n line: node.loc.end.line,\n // Only decorate the \"var\".\n column: node.loc.start.column + 3,\n },\n },\n });\n }\n break;\n }\n },\n beforeVisitGlobal(node) {\n if (node.name === \"arguments\") {\n errors.push({\n type: \"SyntaxError\",\n message: \"Use the rest parameters instead of 'arguments'\",\n loc: node.loc,\n });\n }\n },\n beforeVisitUnknown(node) {\n errors.push({\n type: \"SyntaxError\",\n message: `Unsupported syntax: \\`${node.type}\\``,\n loc: node.loc,\n });\n return true;\n },\n },\n });\n }\n return errors;\n}\n"],"file":"lint.js"}
|
package/dist/cjs/parse.js
CHANGED
|
@@ -5,6 +5,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.parseAsEstree = parseAsEstree;
|
|
7
7
|
exports.parseAsEstreeExpression = parseAsEstreeExpression;
|
|
8
|
+
exports.parseForAnalysis = parseForAnalysis;
|
|
8
9
|
|
|
9
10
|
var _parser = require("@babel/parser");
|
|
10
11
|
|
|
@@ -50,4 +51,24 @@ function parseAsEstree(source, {
|
|
|
50
51
|
|
|
51
52
|
return jsNodes[0];
|
|
52
53
|
}
|
|
54
|
+
|
|
55
|
+
/** For next-core internal or devtools usage only. */
|
|
56
|
+
function parseForAnalysis(source, {
|
|
57
|
+
typescript,
|
|
58
|
+
tokens
|
|
59
|
+
} = {}) {
|
|
60
|
+
try {
|
|
61
|
+
return (0, _parser.parse)(source, {
|
|
62
|
+
plugins: ["estree", typescript && "typescript"].filter(Boolean),
|
|
63
|
+
strictMode: true,
|
|
64
|
+
attachComment: false,
|
|
65
|
+
// Allow export/import declarations to make analyser handle errors.
|
|
66
|
+
sourceType: "unambiguous",
|
|
67
|
+
tokens
|
|
68
|
+
});
|
|
69
|
+
} catch (e) {
|
|
70
|
+
// Return no errors if parse failed.
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
53
74
|
//# sourceMappingURL=parse.js.map
|
package/dist/cjs/parse.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/parse.ts"],"names":["parseAsEstreeExpression","source","plugins","proposal","attachComment","parseAsEstree","typescript","file","filter","Boolean","strictMode","body","program","jsNodes","node","type","startsWith","test","SyntaxError","push","length","map","join"],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../../src/parse.ts"],"names":["parseAsEstreeExpression","source","plugins","proposal","attachComment","parseAsEstree","typescript","file","filter","Boolean","strictMode","body","program","jsNodes","node","type","startsWith","test","SyntaxError","push","length","map","join","parseForAnalysis","tokens","sourceType","e"],"mappings":";;;;;;;;;AAAA;;AAQO,SAASA,uBAAT,CAAiCC,MAAjC,EAA6D;AAClE,SAAO,6BAAgBA,MAAhB,EAAwB;AAC7BC,IAAAA,OAAO,EAAE,CAAC,QAAD,EAAW,CAAC,kBAAD,EAAqB;AAAEC,MAAAA,QAAQ,EAAE;AAAZ,KAArB,CAAX,CADoB;AAE7BC,IAAAA,aAAa,EAAE;AAFc,GAAxB,CAAP;AAID;;AAMM,SAASC,aAAT,CACLJ,MADK,EAEL;AAAEK,EAAAA;AAAF,IAAqC,EAFhC,EAGgB;AACrB,QAAMC,IAAI,GAAG,mBAAMN,MAAN,EAAc;AACzBC,IAAAA,OAAO,EAAE,CAAC,QAAD,EAAWI,UAAU,IAAI,YAAzB,EAAuCE,MAAvC,CACPC,OADO,CADgB;AAIzBC,IAAAA,UAAU,EAAE,IAJa;AAKzBN,IAAAA,aAAa,EAAE;AALU,GAAd,CAAb;AAOA,QAAMO,IAAI,GAAGJ,IAAI,CAACK,OAAL,CAAaD,IAA1B;AACA,QAAME,OAAoB,GAAGP,UAAU,GAAG,EAAH,GAAQK,IAA/C;;AACA,MAAIL,UAAJ,EAAgB;AACd,SAAK,MAAMQ,IAAX,IAAmBH,IAAnB,EAAyB;AACvB,UAAIG,IAAI,CAACC,IAAL,CAAUC,UAAV,CAAqB,IAArB,CAAJ,EAAgC;AAC9B,YAAI,qBAAqBC,IAArB,CAA0BH,IAAI,CAACC,IAA/B,CAAJ,EAA0C;AACxC,gBAAM,IAAIG,WAAJ,CAAiB,kCAAiCJ,IAAI,CAACC,IAAK,EAA5D,CAAN;AACD;AACF,OAJD,MAIO;AACLF,QAAAA,OAAO,CAACM,IAAR,CAAaL,IAAb;AACD;AACF;AACF;;AACD,MAAID,OAAO,CAACO,MAAR,KAAmB,CAAvB,EAA0B;AACxB,UAAM,IAAIF,WAAJ,CAAgB,gCAAhB,CAAN;AACD;;AACD,MAAIL,OAAO,CAACO,MAAR,GAAiB,CAAjB,IAAsBP,OAAO,CAAC,CAAD,CAAP,CAAWE,IAAX,KAAoB,qBAA9C,EAAqE;AACnE,UAAM,IAAIG,WAAJ,CACH,oEAAmEL,OAAO,CACxEQ,GADiE,CAC5DP,IAAD,IAAW,IAAGA,IAAI,CAACC,IAAK,GADqC,EAEjEO,IAFiE,CAE5D,IAF4D,CAEtD,EAHV,CAAN;AAKD;;AACD,SAAOT,OAAO,CAAC,CAAD,CAAd;AACD;;AAOD;AACO,SAASU,gBAAT,CACLtB,MADK,EAEL;AAAEK,EAAAA,UAAF;AAAckB,EAAAA;AAAd,IAA0C,EAFrC,EAGc;AACnB,MAAI;AACF,WAAO,mBAAMvB,MAAN,EAAc;AACnBC,MAAAA,OAAO,EAAE,CAAC,QAAD,EAAWI,UAAU,IAAI,YAAzB,EAAuCE,MAAvC,CACPC,OADO,CADU;AAInBC,MAAAA,UAAU,EAAE,IAJO;AAKnBN,MAAAA,aAAa,EAAE,KALI;AAMnB;AACAqB,MAAAA,UAAU,EAAE,aAPO;AAQnBD,MAAAA;AARmB,KAAd,CAAP;AAUD,GAXD,CAWE,OAAOE,CAAP,EAAU;AACV;AACA,WAAO,IAAP;AACD;AACF","sourcesContent":["import {\n parse,\n parseExpression,\n ParseResult,\n ParserPlugin,\n} from \"@babel/parser\";\nimport { Expression, File, FunctionDeclaration, Statement } from \"@babel/types\";\n\nexport function parseAsEstreeExpression(source: string): Expression {\n return parseExpression(source, {\n plugins: [\"estree\", [\"pipelineOperator\", { proposal: \"minimal\" }]],\n attachComment: false,\n });\n}\n\nexport interface ParseEstreeOptions {\n typescript?: boolean;\n}\n\nexport function parseAsEstree(\n source: string,\n { typescript }: ParseEstreeOptions = {}\n): FunctionDeclaration {\n const file = parse(source, {\n plugins: [\"estree\", typescript && \"typescript\"].filter(\n Boolean\n ) as ParserPlugin[],\n strictMode: true,\n attachComment: false,\n });\n const body = file.program.body;\n const jsNodes: Statement[] = typescript ? [] : body;\n if (typescript) {\n for (const node of body) {\n if (node.type.startsWith(\"TS\")) {\n if (/Enum|Import|Export/.test(node.type)) {\n throw new SyntaxError(`Unsupported TypeScript syntax: ${node.type}`);\n }\n } else {\n jsNodes.push(node);\n }\n }\n }\n if (jsNodes.length === 0) {\n throw new SyntaxError(\"Function declaration not found\");\n }\n if (jsNodes.length > 1 || jsNodes[0].type !== \"FunctionDeclaration\") {\n throw new SyntaxError(\n `Expect a single function declaration at top level, but received: ${jsNodes\n .map((node) => `\"${node.type}\"`)\n .join(\", \")}`\n );\n }\n return jsNodes[0] as FunctionDeclaration;\n}\n\nexport interface AnalysisOptions {\n typescript?: boolean;\n tokens?: boolean;\n}\n\n/** For next-core internal or devtools usage only. */\nexport function parseForAnalysis(\n source: string,\n { typescript, tokens }: AnalysisOptions = {}\n): ParseResult<File> {\n try {\n return parse(source, {\n plugins: [\"estree\", typescript && \"typescript\"].filter(\n Boolean\n ) as ParserPlugin[],\n strictMode: true,\n attachComment: false,\n // Allow export/import declarations to make analyser handle errors.\n sourceType: \"unambiguous\",\n tokens,\n });\n } catch (e) {\n // Return no errors if parse failed.\n return null;\n }\n}\n"],"file":"parse.js"}
|