@nodesecure/js-x-ray 4.4.0 → 5.0.1
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/README.md +67 -39
- package/index.d.ts +33 -123
- package/index.js +2 -40
- package/package.json +7 -6
- package/src/ASTDeps.js +63 -55
- package/src/Analysis.js +140 -152
- package/src/constants.js +0 -12
- package/src/probes/index.js +3 -1
- package/src/probes/isArrayExpression.js +27 -21
- package/src/probes/isAssignmentExpression.js +29 -22
- package/src/probes/isBinaryExpression.js +53 -39
- package/src/probes/isFunctionDeclaration.js +27 -20
- package/src/probes/isImportDeclaration.js +30 -26
- package/src/probes/isLiteral.js +52 -47
- package/src/probes/isLiteralRegex.js +31 -27
- package/src/probes/isObjectExpression.js +29 -23
- package/src/probes/isRegexObject.js +42 -38
- package/src/probes/isRequire.js +159 -160
- package/src/probes/isUnaryExpression.js +26 -18
- package/src/probes/isUnsafeCallee.js +23 -19
- package/src/probes/isVariableDeclaration.js +104 -104
- package/src/probes/isWeakCrypto.js +31 -0
- package/src/utils.js +166 -182
- package/src/warnings.js +70 -0
- package/types/api.d.ts +44 -0
- package/types/astdeps.d.ts +30 -0
- package/types/warnings.d.ts +33 -0
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@ JavaScript AST analysis. This package has been created to export the [Node-Secur
|
|
|
10
10
|
|
|
11
11
|
The goal is to quickly identify dangerous code and patterns for developers and Security researchers. Interpreting the results of this tool will still require you to have a set of security notions.
|
|
12
12
|
|
|
13
|
-
>
|
|
13
|
+
> **Note** I have no particular background in security. I'm simply becoming more and more interested and passionate about static code analysis. But I would be more than happy to learn that my work can help prevent potential future attacks (or leaks).
|
|
14
14
|
|
|
15
15
|
## Goals
|
|
16
16
|
The objective of the project is to successfully detect all potentially suspicious JavaScript codes.. The target is obviously codes that are added or injected for malicious purposes..
|
|
@@ -71,59 +71,58 @@ console.log(warnings);
|
|
|
71
71
|
|
|
72
72
|
The analysis will return: `http` (in try), `crypto`, `util` and `fs`.
|
|
73
73
|
|
|
74
|
-
>
|
|
74
|
+
> **Warning** There is also a lot of suspicious code example in the `./examples` cases directory. Feel free to try the tool on these files.
|
|
75
75
|
|
|
76
76
|
## Warnings
|
|
77
77
|
|
|
78
78
|
This section describes how use `warnings` export.
|
|
79
79
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
80
|
+
```ts
|
|
81
|
+
type WarningName = "parsing-error"
|
|
82
|
+
| "encoded-literal"
|
|
83
|
+
| "unsafe-regex"
|
|
84
|
+
| "unsafe-stmt"
|
|
85
|
+
| "unsafe-assign"
|
|
86
|
+
| "short-identifiers"
|
|
87
|
+
| "suspicious-literal"
|
|
88
|
+
| "obfuscated-code"
|
|
89
|
+
| "weak-crypto"
|
|
90
|
+
| "unsafe-import";
|
|
91
|
+
|
|
92
|
+
declare const warnings: Record<WarningName, {
|
|
93
|
+
i18n: string;
|
|
94
|
+
severity: "Information" | "Warning" | "Critical";
|
|
95
|
+
experimental?: boolean;
|
|
96
|
+
}>;
|
|
96
97
|
```
|
|
97
98
|
|
|
98
99
|
We make a call to `i18n` through the package `NodeSecure/i18n` to get the translation.
|
|
99
100
|
|
|
100
|
-
```
|
|
101
|
+
```js
|
|
101
102
|
import * as jsxray from "@nodesecure/js-x-ray";
|
|
102
103
|
import * as i18n from "@nodesecure/i18n";
|
|
103
104
|
|
|
104
|
-
console.log(i18n.getToken(jsxray.warnings.
|
|
105
|
-
|
|
105
|
+
console.log(i18n.getToken(jsxray.warnings["parsing-error"].i18n));
|
|
106
106
|
```
|
|
107
107
|
|
|
108
|
-
## Warnings Legends
|
|
109
|
-
|
|
110
|
-
> Node-secure versions equal or lower than 0.7.0 are no longer compatible with the warnings table below.
|
|
108
|
+
## Warnings Legends
|
|
111
109
|
|
|
112
|
-
|
|
110
|
+
> **Warning** versions of NodeSecure greather than v0.7.0 are no longer compatible with the warnings table below.
|
|
113
111
|
|
|
114
|
-
|
|
115
|
-
| --- | --- |
|
|
116
|
-
| parsing-error | An error occured when parsing the JavaScript code with meriyah. It mean that the conversion from string to AST as failed. If you encounter such an error, **please open an issue here**. |
|
|
117
|
-
| unsafe-import | Unable to follow an import (require, require.resolve) statement/expr. |
|
|
118
|
-
| unsafe-regex | A RegEx as been detected as unsafe and may be used for a ReDoS Attack. |
|
|
119
|
-
| unsafe-stmt | Usage of dangerous statement like `eval()` or `Function("")`. |
|
|
120
|
-
| unsafe-assign | Assignment of a protected global like `process` or `require`. |
|
|
121
|
-
| encoded-literal | An encoded literal has been detected (it can be an hexa value, unicode sequence, base64 string etc) |
|
|
122
|
-
| short-identifiers | This mean that all identifiers has an average length below 1.5. Only possible if the file contains more than 5 identifiers. |
|
|
123
|
-
| suspicious-literal | This mean that the sum of suspicious score of all Literals is bigger than 3. |
|
|
124
|
-
| obfuscated-code (**experimental**) | There's a very high probability that the code is obfuscated... |
|
|
112
|
+
This section describe all the possible warnings returned by JSXRay. Click on the warning **name** for additional information and examples.
|
|
125
113
|
|
|
126
|
-
|
|
114
|
+
| name | experimental | description |
|
|
115
|
+
| --- | :-: | --- |
|
|
116
|
+
| [parsing-error](./docs/parsing-error.md) | ❌ | The AST parser throw an error |
|
|
117
|
+
| [unsafe-import](./docs/unsafe-import.md) | ❌ | Unable to follow an import (require, require.resolve) statement/expr. |
|
|
118
|
+
| [unsafe-regex](./docs/unsafe-regex.md) | ❌ | A RegEx as been detected as unsafe and may be used for a ReDoS Attack. |
|
|
119
|
+
| [unsafe-stmt](./docs//unsafe-stmt.md) | ❌ | Usage of dangerous statement like `eval()` or `Function("")`. |
|
|
120
|
+
| [unsafe-assign](./docs/unsafe-assign.md) | ❌ | Assignment of a protected global like `process` or `require`. |
|
|
121
|
+
| [encoded-literal](./docs/encoded-literal.md) | ❌ | An encoded literal has been detected (it can be an hexa value, unicode sequence or a base64 string) |
|
|
122
|
+
| [short-identifiers](./docs/short-identifiers.md) | ❌ | This mean that all identifiers has an average length below 1.5. |
|
|
123
|
+
| [suspicious-literal](./docs/suspicious-literal.md) | ❌ | A suspicious literal has been found in the source code. |
|
|
124
|
+
| [obfuscated-code](./docs/obfuscated-code.md) | ✔️ | There's a very high probability that the code is obfuscated. |
|
|
125
|
+
| [weak-crypto](./docs/weak-crypto.md) | ✔️ | The code probably contains a weak crypto algorithm (md5, sha1...) |
|
|
127
126
|
|
|
128
127
|
## API
|
|
129
128
|
|
|
@@ -142,7 +141,7 @@ The method take a first argument which is the code you want to analyse. It will
|
|
|
142
141
|
```ts
|
|
143
142
|
interface Report {
|
|
144
143
|
dependencies: ASTDeps;
|
|
145
|
-
warnings: Warning
|
|
144
|
+
warnings: Warning[];
|
|
146
145
|
idsLengthAvg: number;
|
|
147
146
|
stringScore: number;
|
|
148
147
|
isOneLineRequire: boolean;
|
|
@@ -151,11 +150,37 @@ interface Report {
|
|
|
151
150
|
|
|
152
151
|
</details>
|
|
153
152
|
|
|
153
|
+
<details>
|
|
154
|
+
<summary>runASTAnalysisOnFile(pathToFile: string, options?: RuntimeFileOptions): Promise< ReportOnFile ></summary>
|
|
155
|
+
|
|
156
|
+
```ts
|
|
157
|
+
interface RuntimeOptions {
|
|
158
|
+
module?: boolean;
|
|
159
|
+
isMinified?: boolean;
|
|
160
|
+
}
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
Run the SAST scanner on a given JavaScript file.
|
|
164
|
+
|
|
165
|
+
```ts
|
|
166
|
+
export type ReportOnFile = {
|
|
167
|
+
ok: true,
|
|
168
|
+
warnings: Warning[];
|
|
169
|
+
dependencies: ASTDeps;
|
|
170
|
+
isMinified: boolean;
|
|
171
|
+
} | {
|
|
172
|
+
ok: false,
|
|
173
|
+
warnings: Warning[];
|
|
174
|
+
}
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
</details>
|
|
178
|
+
|
|
154
179
|
|
|
155
180
|
## Contributors ✨
|
|
156
181
|
|
|
157
182
|
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
|
|
158
|
-
[](#contributors-)
|
|
159
184
|
<!-- ALL-CONTRIBUTORS-BADGE:END -->
|
|
160
185
|
|
|
161
186
|
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
|
|
@@ -168,6 +193,9 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
|
|
|
168
193
|
<td align="center"><a href="https://www.linkedin.com/in/thomas-gentilhomme/"><img src="https://avatars.githubusercontent.com/u/4438263?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Gentilhomme</b></sub></a><br /><a href="https://github.com/NodeSecure/js-x-ray/commits?author=fraxken" title="Code">💻</a> <a href="https://github.com/NodeSecure/js-x-ray/commits?author=fraxken" title="Documentation">📖</a> <a href="https://github.com/NodeSecure/js-x-ray/pulls?q=is%3Apr+reviewed-by%3Afraxken" title="Reviewed Pull Requests">👀</a> <a href="#security-fraxken" title="Security">🛡️</a> <a href="https://github.com/NodeSecure/js-x-ray/issues?q=author%3Afraxken" title="Bug reports">🐛</a></td>
|
|
169
194
|
<td align="center"><a href="https://github.com/Rossb0b"><img src="https://avatars.githubusercontent.com/u/39910164?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Nicolas Hallaert</b></sub></a><br /><a href="https://github.com/NodeSecure/js-x-ray/commits?author=Rossb0b" title="Documentation">📖</a></td>
|
|
170
195
|
<td align="center"><a href="https://github.com/antoine-coulon"><img src="https://avatars.githubusercontent.com/u/43391199?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Antoine</b></sub></a><br /><a href="https://github.com/NodeSecure/js-x-ray/commits?author=antoine-coulon" title="Code">💻</a></td>
|
|
196
|
+
<td align="center"><a href="https://github.com/Mathieuka"><img src="https://avatars.githubusercontent.com/u/34446722?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Mathieu</b></sub></a><br /><a href="https://github.com/NodeSecure/js-x-ray/commits?author=Mathieuka" title="Code">💻</a></td>
|
|
197
|
+
<td align="center"><a href="https://github.com/Kawacrepe"><img src="https://avatars.githubusercontent.com/u/40260517?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Vincent Dhennin</b></sub></a><br /><a href="https://github.com/NodeSecure/js-x-ray/commits?author=Kawacrepe" title="Code">💻</a> <a href="https://github.com/NodeSecure/js-x-ray/commits?author=Kawacrepe" title="Tests">⚠️</a></td>
|
|
198
|
+
<td align="center"><a href="http://tonygo.dev"><img src="https://avatars.githubusercontent.com/u/22824417?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Tony Gorez</b></sub></a><br /><a href="https://github.com/NodeSecure/js-x-ray/commits?author=tony-go" title="Code">💻</a> <a href="https://github.com/NodeSecure/js-x-ray/commits?author=tony-go" title="Documentation">📖</a> <a href="https://github.com/NodeSecure/js-x-ray/commits?author=tony-go" title="Tests">⚠️</a></td>
|
|
171
199
|
</tr>
|
|
172
200
|
</table>
|
|
173
201
|
|
package/index.d.ts
CHANGED
|
@@ -1,124 +1,34 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
warnings: Warning<BaseWarning>[];
|
|
35
|
-
idsLengthAvg: number;
|
|
36
|
-
stringScore: number;
|
|
37
|
-
isOneLineRequire: boolean;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
interface SourceLocation {
|
|
41
|
-
start: {
|
|
42
|
-
line: number;
|
|
43
|
-
column: number;
|
|
44
|
-
};
|
|
45
|
-
end: {
|
|
46
|
-
line: number;
|
|
47
|
-
column: number;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
interface Dependency {
|
|
52
|
-
unsafe: boolean;
|
|
53
|
-
inTry: boolean;
|
|
54
|
-
location?: SourceLocation;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
interface WarningsNames {
|
|
58
|
-
parsingError: {
|
|
59
|
-
code: "ast-error",
|
|
60
|
-
i18n: "sast_warnings.ast_error"
|
|
61
|
-
},
|
|
62
|
-
unsafeImport: {
|
|
63
|
-
code: "unsafe-import",
|
|
64
|
-
i18n: "sast_warnings.unsafe_import"
|
|
65
|
-
},
|
|
66
|
-
unsafeRegex: {
|
|
67
|
-
code: "unsafe-regex",
|
|
68
|
-
i18n: "sast_warnings.unsafe_regex"
|
|
69
|
-
},
|
|
70
|
-
unsafeStmt: {
|
|
71
|
-
code: "unsafe-stmt",
|
|
72
|
-
i18n: "sast_warnings.unsafe_stmt"
|
|
73
|
-
},
|
|
74
|
-
unsafeAssign: {
|
|
75
|
-
code: "unsafe-assign",
|
|
76
|
-
i18n: "sast_warnings.unsafe_assign"
|
|
77
|
-
},
|
|
78
|
-
encodedLiteral: {
|
|
79
|
-
code: "encoded-literal",
|
|
80
|
-
i18n: "sast_warnings.encoded_literal"
|
|
81
|
-
},
|
|
82
|
-
shortIdentifiers: {
|
|
83
|
-
code: "short-identifiers",
|
|
84
|
-
i18n: "sast_warnings.short_identifiers"
|
|
85
|
-
},
|
|
86
|
-
suspiciousLiteral: {
|
|
87
|
-
code: "suspicious-literal",
|
|
88
|
-
i18n: "sast_warnings.suspicious_literal"
|
|
89
|
-
},
|
|
90
|
-
obfuscatedCode: {
|
|
91
|
-
code: "obfuscated-code",
|
|
92
|
-
i18n: "sast_warnings.obfuscated_code"
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
interface RuntimeOptions {
|
|
97
|
-
module?: boolean;
|
|
98
|
-
isMinified?: boolean;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
export function runASTAnalysis(str: string, options?: RuntimeOptions): Report;
|
|
102
|
-
|
|
103
|
-
export type ReportOnFile = {
|
|
104
|
-
ok: true,
|
|
105
|
-
warnings: Warning<BaseWarning>[];
|
|
106
|
-
dependencies: ASTDeps;
|
|
107
|
-
isMinified: boolean;
|
|
108
|
-
} | {
|
|
109
|
-
ok: false,
|
|
110
|
-
warnings: Warning<BaseWarning>[];
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
export interface RuntimeFileOptions {
|
|
114
|
-
packageName?: string;
|
|
115
|
-
module?: boolean;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
export function runASTAnalysisOnFile(pathToFile: string, options?: RuntimeFileOptions): Promise<ReportOnFile>;
|
|
119
|
-
|
|
120
|
-
export const warnings: WarningsNames;
|
|
1
|
+
import {
|
|
2
|
+
runASTAnalysis,
|
|
3
|
+
runASTAnalysisOnFile,
|
|
4
|
+
Report,
|
|
5
|
+
ReportOnFile,
|
|
6
|
+
RuntimeFileOptions,
|
|
7
|
+
RuntimeOptions
|
|
8
|
+
} from "./types/api";
|
|
9
|
+
import {
|
|
10
|
+
Warning,
|
|
11
|
+
WarningDefault,
|
|
12
|
+
WarningLocation,
|
|
13
|
+
WarningName,
|
|
14
|
+
WarningNameWithValue
|
|
15
|
+
} from "./types/warnings";
|
|
16
|
+
import { ASTDeps } from "./types/astdeps";
|
|
17
|
+
|
|
18
|
+
declare const warnings: Record<WarningName, Pick<WarningDefault, "experimental" | "i18n" | "severity">>;
|
|
19
|
+
|
|
20
|
+
export {
|
|
21
|
+
warnings,
|
|
22
|
+
runASTAnalysis,
|
|
23
|
+
runASTAnalysisOnFile,
|
|
24
|
+
Report,
|
|
25
|
+
ReportOnFile,
|
|
26
|
+
RuntimeFileOptions,
|
|
27
|
+
RuntimeOptions,
|
|
28
|
+
ASTDeps,
|
|
29
|
+
Warning,
|
|
30
|
+
WarningDefault,
|
|
31
|
+
WarningLocation,
|
|
32
|
+
WarningName,
|
|
33
|
+
WarningNameWithValue
|
|
121
34
|
}
|
|
122
|
-
|
|
123
|
-
export = JSXRay;
|
|
124
|
-
export as namespace JSXRay;
|
package/index.js
CHANGED
|
@@ -9,6 +9,7 @@ import isMinified from "is-minified-code";
|
|
|
9
9
|
|
|
10
10
|
// Import Internal Dependencies
|
|
11
11
|
import Analysis from "./src/Analysis.js";
|
|
12
|
+
import { warnings } from "./src/warnings.js";
|
|
12
13
|
|
|
13
14
|
export function runASTAnalysis(str, options = Object.create(null)) {
|
|
14
15
|
const { module = true, isMinified = false } = options;
|
|
@@ -83,43 +84,4 @@ export async function runASTAnalysisOnFile(pathToFile, options = {}) {
|
|
|
83
84
|
}
|
|
84
85
|
}
|
|
85
86
|
|
|
86
|
-
export
|
|
87
|
-
parsingError: {
|
|
88
|
-
code: "ast-error",
|
|
89
|
-
i18n: "sast_warnings.ast_error"
|
|
90
|
-
},
|
|
91
|
-
unsafeImport: {
|
|
92
|
-
code: "unsafe-import",
|
|
93
|
-
i18n: "sast_warnings.unsafe_import"
|
|
94
|
-
},
|
|
95
|
-
unsafeRegex: {
|
|
96
|
-
code: "unsafe-regex",
|
|
97
|
-
i18n: "sast_warnings.unsafe_regex"
|
|
98
|
-
},
|
|
99
|
-
unsafeStmt: {
|
|
100
|
-
code: "unsafe-stmt",
|
|
101
|
-
i18n: "sast_warnings.unsafe_stmt"
|
|
102
|
-
},
|
|
103
|
-
unsafeAssign: {
|
|
104
|
-
code: "unsafe-assign",
|
|
105
|
-
i18n: "sast_warnings.unsafe_assign"
|
|
106
|
-
},
|
|
107
|
-
encodedLiteral: {
|
|
108
|
-
code: "encoded-literal",
|
|
109
|
-
i18n: "sast_warnings.encoded_literal"
|
|
110
|
-
},
|
|
111
|
-
shortIdentifiers: {
|
|
112
|
-
code: "short-identifiers",
|
|
113
|
-
i18n: "sast_warnings.short_identifiers"
|
|
114
|
-
},
|
|
115
|
-
suspiciousLiteral: {
|
|
116
|
-
code: "suspicious-literal",
|
|
117
|
-
i18n: "sast_warnings.suspicious_literal"
|
|
118
|
-
},
|
|
119
|
-
obfuscatedCode: {
|
|
120
|
-
code: "obfuscated-code",
|
|
121
|
-
i18n: "sast_warnings.obfuscated_code"
|
|
122
|
-
}
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
|
|
87
|
+
export { warnings };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nodesecure/js-x-ray",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.1",
|
|
4
4
|
"description": "JavaScript AST XRay analysis",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": "./index.js",
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"scripts": {
|
|
11
11
|
"lint": "eslint src test",
|
|
12
12
|
"prepublishOnly": "pkg-ok",
|
|
13
|
-
"test": "cross-env esm-tape-runner 'test
|
|
13
|
+
"test": "cross-env esm-tape-runner 'test/**/*.spec.js' | tap-monkey",
|
|
14
14
|
"check": "cross-env npm run lint && npm run test"
|
|
15
15
|
},
|
|
16
16
|
"repository": {
|
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
],
|
|
28
28
|
"files": [
|
|
29
29
|
"src",
|
|
30
|
+
"types",
|
|
30
31
|
"index.js",
|
|
31
32
|
"index.d.ts"
|
|
32
33
|
],
|
|
@@ -44,13 +45,13 @@
|
|
|
44
45
|
"safe-regex": "^2.1.1"
|
|
45
46
|
},
|
|
46
47
|
"devDependencies": {
|
|
47
|
-
"@nodesecure/eslint-config": "^1.
|
|
48
|
+
"@nodesecure/eslint-config": "^1.4.1",
|
|
48
49
|
"@slimio/is": "^1.5.1",
|
|
49
50
|
"@small-tech/esm-tape-runner": "^2.0.0",
|
|
50
|
-
"@small-tech/tap-monkey": "^1.
|
|
51
|
-
"@types/node": "^17.0.
|
|
51
|
+
"@small-tech/tap-monkey": "^1.4.0",
|
|
52
|
+
"@types/node": "^17.0.42",
|
|
52
53
|
"cross-env": "^7.0.3",
|
|
53
|
-
"eslint": "^8.
|
|
54
|
+
"eslint": "^8.17.0",
|
|
54
55
|
"pkg-ok": "^3.0.0",
|
|
55
56
|
"tape": "^5.5.3"
|
|
56
57
|
}
|
package/src/ASTDeps.js
CHANGED
|
@@ -1,55 +1,63 @@
|
|
|
1
|
-
|
|
2
|
-
export default class ASTDeps {
|
|
3
|
-
#inTry = false;
|
|
4
|
-
dependencies = Object.create(null);
|
|
5
|
-
|
|
6
|
-
get isInTryStmt() {
|
|
7
|
-
return this.#inTry;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
set isInTryStmt(value) {
|
|
11
|
-
if (typeof value !== "boolean") {
|
|
12
|
-
throw new TypeError("value must be a boolean!");
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
this.#inTry = value;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
removeByName(name) {
|
|
19
|
-
if (Reflect.has(this.dependencies, name)) {
|
|
20
|
-
delete this.dependencies[name];
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
add(depName, location = null, unsafe = false) {
|
|
25
|
-
if (depName.trim() === "") {
|
|
26
|
-
return;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
const cleanDepName = depName.charAt(depName.length - 1) === "/" ? depName.slice(0, -1) : depName;
|
|
30
|
-
const dep = {
|
|
31
|
-
unsafe,
|
|
32
|
-
inTry: this.isInTryStmt
|
|
33
|
-
};
|
|
34
|
-
if (location !== null) {
|
|
35
|
-
dep.location = location;
|
|
36
|
-
}
|
|
37
|
-
this.dependencies[cleanDepName] = dep;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
*
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
1
|
+
|
|
2
|
+
export default class ASTDeps {
|
|
3
|
+
#inTry = false;
|
|
4
|
+
dependencies = Object.create(null);
|
|
5
|
+
|
|
6
|
+
get isInTryStmt() {
|
|
7
|
+
return this.#inTry;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
set isInTryStmt(value) {
|
|
11
|
+
if (typeof value !== "boolean") {
|
|
12
|
+
throw new TypeError("value must be a boolean!");
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
this.#inTry = value;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
removeByName(name) {
|
|
19
|
+
if (Reflect.has(this.dependencies, name)) {
|
|
20
|
+
delete this.dependencies[name];
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
add(depName, location = null, unsafe = false) {
|
|
25
|
+
if (depName.trim() === "") {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const cleanDepName = depName.charAt(depName.length - 1) === "/" ? depName.slice(0, -1) : depName;
|
|
30
|
+
const dep = {
|
|
31
|
+
unsafe,
|
|
32
|
+
inTry: this.isInTryStmt
|
|
33
|
+
};
|
|
34
|
+
if (location !== null) {
|
|
35
|
+
dep.location = location;
|
|
36
|
+
}
|
|
37
|
+
this.dependencies[cleanDepName] = dep;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
has(depName) {
|
|
41
|
+
if (depName.trim() === "") {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return Reflect.has(this.dependencies, depName);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
get size() {
|
|
49
|
+
return Object.keys(this.dependencies).length;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
* getDependenciesInTryStatement() {
|
|
53
|
+
for (const [depName, props] of Object.entries(this.dependencies)) {
|
|
54
|
+
if (props.inTry === true && props.unsafe === false) {
|
|
55
|
+
yield depName;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
* [Symbol.iterator]() {
|
|
61
|
+
yield* Object.keys(this.dependencies);
|
|
62
|
+
}
|
|
63
|
+
}
|