|
1 |
| -"use strict"; |
2 |
| - |
3 |
| -// Require Third-party Dependencies |
4 |
| -const { walk } = require("estree-walker"); |
5 |
| -const meriyah = require("meriyah"); |
6 |
| - |
7 |
| -// Require Internal Dependencies |
8 |
| -const Analysis = require("./src/Analysis"); |
9 |
| - |
10 |
| -function runASTAnalysis(str, options = Object.create(null)) { |
11 |
| - const { module = true, isMinified = false } = options; |
12 |
| - |
13 |
| - // Note: if the file start with a shebang then we remove it because 'parseScript' may fail to parse it. |
14 |
| - // Example: #!/usr/bin/env node |
15 |
| - const strToAnalyze = str.charAt(0) === "#" ? str.slice(str.indexOf("\n")) : str; |
16 |
| - const { body } = meriyah.parseScript(strToAnalyze, { |
17 |
| - next: true, loc: true, raw: true, module: Boolean(module) |
18 |
| - }); |
19 |
| - |
20 |
| - const sastAnalysis = new Analysis(); |
21 |
| - |
22 |
| - // we walk each AST Nodes, this is a purely synchronous I/O |
23 |
| - walk(body, { |
24 |
| - enter(node) { |
25 |
| - // Skip the root of the AST. |
26 |
| - if (Array.isArray(node)) { |
27 |
| - return; |
28 |
| - } |
29 |
| - |
30 |
| - const action = sastAnalysis.walk(node); |
31 |
| - if (action === "skip") { |
32 |
| - this.skip(); |
33 |
| - } |
34 |
| - } |
35 |
| - }); |
36 |
| - |
37 |
| - const dependencies = sastAnalysis.dependencies; |
38 |
| - const { idsLengthAvg, stringScore, warnings } = sastAnalysis.getResult(isMinified); |
39 |
| - const isOneLineRequire = body.length <= 1 && dependencies.size <= 1; |
40 |
| - |
41 |
| - return { |
42 |
| - dependencies, warnings, idsLengthAvg, stringScore, isOneLineRequire |
43 |
| - }; |
| 1 | +// Import Third-party Dependencies |
| 2 | +import { walk } from "estree-walker"; |
| 3 | +import * as meriyah from "meriyah"; |
| 4 | + |
| 5 | +// Import Internal Dependencies |
| 6 | +import Analysis from "./src/Analysis.js"; |
| 7 | + |
| 8 | +export function runASTAnalysis(str, options = Object.create(null)) { |
| 9 | + const { module = true, isMinified = false } = options; |
| 10 | + |
| 11 | + // Note: if the file start with a shebang then we remove it because 'parseScript' may fail to parse it. |
| 12 | + // Example: #!/usr/bin/env node |
| 13 | + const strToAnalyze = str.charAt(0) === "#" ? str.slice(str.indexOf("\n")) : str; |
| 14 | + const { body } = meriyah.parseScript(strToAnalyze, { |
| 15 | + next: true, loc: true, raw: true, module: Boolean(module) |
| 16 | + }); |
| 17 | + |
| 18 | + const sastAnalysis = new Analysis(); |
| 19 | + |
| 20 | + // we walk each AST Nodes, this is a purely synchronous I/O |
| 21 | + walk(body, { |
| 22 | + enter(node) { |
| 23 | + // Skip the root of the AST. |
| 24 | + if (Array.isArray(node)) { |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + const action = sastAnalysis.walk(node); |
| 29 | + if (action === "skip") { |
| 30 | + this.skip(); |
| 31 | + } |
| 32 | + } |
| 33 | + }); |
| 34 | + |
| 35 | + const dependencies = sastAnalysis.dependencies; |
| 36 | + const { idsLengthAvg, stringScore, warnings } = sastAnalysis.getResult(isMinified); |
| 37 | + const isOneLineRequire = body.length <= 1 && dependencies.size <= 1; |
| 38 | + |
| 39 | + return { |
| 40 | + dependencies, warnings, idsLengthAvg, stringScore, isOneLineRequire |
| 41 | + }; |
44 | 42 | }
|
45 | 43 |
|
46 |
| -module.exports = { |
47 |
| - runASTAnalysis, |
48 |
| - CONSTANTS: { |
49 |
| - Warnings: Object.freeze({ |
50 |
| - parsingError: "ast-error", |
51 |
| - unsafeImport: "unsafe-import", |
52 |
| - unsafeRegex: "unsafe-regex", |
53 |
| - unsafeStmt: "unsafe-stmt", |
54 |
| - unsafeAssign: "unsafe-assign", |
55 |
| - encodedLiteral: "encoded-literal", |
56 |
| - shortIdentifiers: "short-identifiers", |
57 |
| - suspiciousLiteral: "suspicious-literal", |
58 |
| - obfuscatedCode: "obfuscated-code" |
59 |
| - }) |
60 |
| - } |
| 44 | +export const CONSTANTS = { |
| 45 | + Warnings: Object.freeze({ |
| 46 | + parsingError: "ast-error", |
| 47 | + unsafeImport: "unsafe-import", |
| 48 | + unsafeRegex: "unsafe-regex", |
| 49 | + unsafeStmt: "unsafe-stmt", |
| 50 | + unsafeAssign: "unsafe-assign", |
| 51 | + encodedLiteral: "encoded-literal", |
| 52 | + shortIdentifiers: "short-identifiers", |
| 53 | + suspiciousLiteral: "suspicious-literal", |
| 54 | + obfuscatedCode: "obfuscated-code" |
| 55 | + }) |
61 | 56 | };
|
0 commit comments