Skip to content

Commit 1f4f421

Browse files
committed
refacto probesOptions ASTAnalyzer
1 parent 8501003 commit 1f4f421

File tree

6 files changed

+38
-50
lines changed

6 files changed

+38
-50
lines changed

index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ function runASTAnalysis(
99
) {
1010
const {
1111
customParser = new JsSourceParser(),
12-
customProbe = [],
13-
isReplacing = false,
12+
customProbes = [],
13+
skipDefaultProbes = false,
1414
...opts
1515
} = options;
1616

@@ -25,8 +25,8 @@ async function runASTAnalysisOnFile(
2525
) {
2626
const {
2727
customParser = new JsSourceParser(),
28-
customProbe = [],
29-
isReplacing = false,
28+
customProbes = [],
29+
skipDefaultProbes = false,
3030
...opts
3131
} = options;
3232

src/AstAnalyser.js

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,16 @@ import { JsSourceParser } from "./JsSourceParser.js";
1515
export class AstAnalyser {
1616
/**
1717
* @constructor
18-
* @param options
18+
* @param {object} [options={}]
19+
* @param {JsSourceParser} [options.customParser]
20+
* @param {Array<object>} [options.customProbes]
21+
* @param {boolean} [options.skipDefaultProbes=false]
1922
*/
2023
constructor(options = {}) {
21-
if (options.customParser !== undefined) {
22-
assert(options.customParser instanceof JsSourceParser || typeof options.customParser === "object",
23-
`customParser must be an instance of JsSourceParser or an object`);
24-
}
25-
if (options.customProbe !== undefined) {
26-
assert(Array.isArray(options.customProbe), `customProbe must be an array`);
27-
}
28-
if (options.isReplacing !== undefined) {
29-
assert(typeof options.isReplacing === "boolean", `isReplacing must be a boolean`);
30-
}
31-
32-
this.parser = options.customParser || new JsSourceParser();
33-
this.options = {
34-
isReplacing: options.isReplacing || false,
35-
customProbe: options.customProbe || []
24+
this.parser = options.customParser ?? new JsSourceParser();
25+
this.probesOptions = {
26+
customProbes: options.customProbes ?? [],
27+
skipDefaultProbes: options.skipDefaultProbes ?? false
3628
};
3729
}
3830

@@ -47,7 +39,7 @@ export class AstAnalyser {
4739
isEcmaScriptModule: Boolean(module)
4840
});
4941

50-
const source = new SourceFile(str, this.options);
42+
const source = new SourceFile(str, this.probesOptions);
5143

5244
// we walk each AST Nodes, this is a purely synchronous I/O
5345
walk(body, {

src/SourceFile.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ export class SourceFile {
2020
encodedLiterals = new Map();
2121
warnings = [];
2222

23-
constructor(sourceCodeString, options = {}) {
23+
constructor(sourceCodeString, probesOptions = {}) {
2424
this.tracer = new VariableTracer()
2525
.enableDefaultTracing()
2626
.trace("crypto.createHash", {
2727
followConsecutiveAssignment: true, moduleName: "crypto"
2828
});
2929

3030
let probes = ProbeRunner.Defaults;
31-
if (Array.isArray(options.customProbe) && options.customProbe.length > 0) {
32-
probes = options.isReplacing === true ? options.customProbe : [...probes, ...options.customProbe];
31+
if (Array.isArray(probesOptions.customProbes) && probesOptions.customProbes.length > 0) {
32+
probes = probesOptions.skipDefaultProbes === true ? probesOptions.customProbes : [...probes, ...probesOptions.customProbes];
3333
}
3434
this.probesRunner = new ProbeRunner(this, probes);
3535

test/AstAnalyser.spec.js

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -240,26 +240,8 @@ describe("AstAnalyser", (t) => {
240240
it("should instantiate with correct default ASTOptions", () => {
241241
const analyser = new AstAnalyser();
242242
assert(analyser.parser instanceof JsSourceParser || typeof analyser.parser.customParser === "object");
243-
assert.deepStrictEqual(analyser.options.customProbe, []);
244-
assert.strictEqual(analyser.options.isReplacing, false);
245-
});
246-
247-
it("should throw an error if customParser is not an instance of JsSourceParser", () => {
248-
assert.throws(() => {
249-
new AstAnalyser({ customParser: "new JsSourceParser()" });
250-
}, AssertionError);
251-
});
252-
253-
it("should throw an error if customProbe is not an array", () => {
254-
assert.throws(() => {
255-
new AstAnalyser({ customProbe: "notArray" });
256-
}, AssertionError);
257-
});
258-
259-
it("should throw an error if isReplacing is not a boolean", () => {
260-
assert.throws(() => {
261-
new AstAnalyser({ isReplacing: "false" });
262-
}, AssertionError);
243+
assert.deepStrictEqual(analyser.probesOptions.customProbes, []);
244+
assert.strictEqual(analyser.probesOptions.skipDefaultProbes, false);
263245
});
264246
});
265247
});

test/issues/221-inject-custom-probes.spec.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const customProbes = [
3636
];
3737

3838
test("should append to list of probes (default)", () => {
39-
const analyser = new AstAnalyser({ parser: new JsSourceParser(), customProbe: customProbes });
39+
const analyser = new AstAnalyser({ customParser: new JsSourceParser(), customProbes });
4040
const result = analyser.analyse(kIncriminedCodeSample);
4141

4242
assert.equal(result.warnings[0].kind, kWarningUnsafeDanger);
@@ -46,17 +46,27 @@ test("should append to list of probes (default)", () => {
4646
});
4747

4848
test("should replace list of probes", () => {
49-
const analyser = new AstAnalyser({ parser: new JsSourceParser(), customProbe: customProbes, isReplacing: true });
49+
const analyser = new AstAnalyser({
50+
parser: new JsSourceParser(),
51+
customProbes,
52+
skipDefaultProbes: true
53+
});
5054
const result = analyser.analyse(kIncriminedCodeSample);
5155

56+
console.log(analyser);
57+
5258
assert.equal(result.warnings[0].kind, kWarningUnsafeDanger);
5359
assert.equal(result.warnings.length, 1);
5460
});
5561

5662
test("should append list of probes using runASTAnalysis", () => {
5763
const result = runASTAnalysis(
5864
kIncriminedCodeSample,
59-
{ parser: new JsSourceParser(), customProbe: customProbes, isReplacing: false }
65+
{
66+
parser: new JsSourceParser(),
67+
customProbes,
68+
skipDefaultProbes: false
69+
}
6070
);
6171

6272
assert.equal(result.warnings[0].kind, kWarningUnsafeDanger);
@@ -68,7 +78,11 @@ test("should append list of probes using runASTAnalysis", () => {
6878
test("should replace list of probes using runASTAnalysis", () => {
6979
const result = runASTAnalysis(
7080
kIncriminedCodeSample,
71-
{ parser: new JsSourceParser(), customProbe: customProbes, isReplacing: true }
81+
{
82+
parser: new JsSourceParser(),
83+
customProbes,
84+
skipDefaultProbes: true
85+
}
7286
);
7387

7488
assert.equal(result.warnings[0].kind, kWarningUnsafeDanger);

types/api.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ interface RuntimeAnalyzerOptions {
6464
/**
6565
* @default []
6666
*/
67-
customProbe?: Probe[] | null;
67+
customProbes?: Probe[] | null;
6868
/**
6969
* @default false
7070
*/
71-
isReplacing?: boolean;
71+
skipDefaultProbes?: boolean;
7272
}
7373

7474
type RuntimeOptions = RuntimeAnalyzerOptions & (RuntimeDefaultOptions | RuntimeFileOptions);

0 commit comments

Comments
 (0)