diff --git a/index.js b/index.js index e636dbb..6db3d8a 100644 --- a/index.js +++ b/index.js @@ -1,76 +1,4 @@ -// Import Internal Dependencies -import { warnings } from "./src/warnings.js"; -import { JsSourceParser } from "./src/JsSourceParser.js"; -import { AstAnalyser } from "./src/AstAnalyser.js"; -import { EntryFilesAnalyser } from "./src/EntryFilesAnalyser.js"; - -/** - * @deprecated - */ -function runASTAnalysis( - str, - options = Object.create(null) -) { - process.emitWarning( - "The runASTAnalysis API is deprecated and will be removed in v8. Please use the AstAnalyser class instead.", - { - code: "DeprecationWarning", - detail: "The runASTAnalysis API is deprecated and will be removed in v8. Please use the AstAnalyser class instead." - } - ); - - const { - customParser = new JsSourceParser(), - customProbes = [], - skipDefaultProbes = false, - ...opts - } = options; - - const analyser = new AstAnalyser({ - customParser, - customProbes, - skipDefaultProbes - }); - - return analyser.analyse(str, opts); -} - -/** - * @deprecated - */ -async function runASTAnalysisOnFile( - pathToFile, - options = {} -) { - process.emitWarning( - "The runASTAnalysisOnFile API is deprecated and will be removed in v8. Please use the AstAnalyser class instead.", - { - code: "DeprecationWarning", - detail: "The runASTAnalysisOnFile API is deprecated and will be removed in v8. Please use the AstAnalyser class instead." - } - ); - - const { - customProbes = [], - customParser = new JsSourceParser(), - skipDefaultProbes = false, - ...opts - } = options; - - const analyser = new AstAnalyser({ - customParser, - customProbes, - skipDefaultProbes - }); - - return analyser.analyseFile(pathToFile, opts); -} - -export { - warnings, - AstAnalyser, - EntryFilesAnalyser, - JsSourceParser, - runASTAnalysis, - runASTAnalysisOnFile -}; +export { warnings } from "./src/warnings.js"; +export { JsSourceParser } from "./src/JsSourceParser.js"; +export { AstAnalyser } from "./src/AstAnalyser.js"; +export { EntryFilesAnalyser } from "./src/EntryFilesAnalyser.js"; diff --git a/test/AstAnalyser.spec.js b/test/AstAnalyser.spec.js index fcae71a..695f004 100644 --- a/test/AstAnalyser.spec.js +++ b/test/AstAnalyser.spec.js @@ -6,6 +6,7 @@ import { readFileSync } from "node:fs"; // Import Internal Dependencies import { AstAnalyser, JsSourceParser } from "../index.js"; +import { FakeSourceParser } from "./fixtures/FakeSourceParser.js"; import { SourceFile } from "../src/SourceFile.js"; import { customProbes, @@ -176,6 +177,22 @@ describe("AstAnalyser", (t) => { assert.equal(result.warnings.length, 1); }); + it("should call with the expected arguments", (t) => { + t.mock.method(AstAnalyser.prototype, "analyse"); + + const source = "const http = require(\"http\");"; + new AstAnalyser().analyse(source, { module: true, removeHTMLComments: true }); + + const source2 = "const fs = require(\"fs\");"; + new AstAnalyser().analyse(source2, { module: false, removeHTMLComments: false }); + + const calls = AstAnalyser.prototype.analyse.mock.calls; + assert.strictEqual(calls.length, 2); + + assert.deepEqual(calls[0].arguments, [source, { module: true, removeHTMLComments: true }]); + assert.deepEqual(calls[1].arguments, [source2, { module: false, removeHTMLComments: false }]); + }); + describe("hooks", () => { describe("initialize", () => { const analyser = new AstAnalyser(); @@ -281,6 +298,56 @@ describe("AstAnalyser", (t) => { assert.strictEqual(parsingError.kind, "parsing-error"); }); + it("should call the method with the expected arguments", async(t) => { + t.mock.method(AstAnalyser.prototype, "analyseFile"); + + const url = new URL("depName.js", FIXTURE_URL); + await new AstAnalyser().analyseFile( + url, + { module: false, packageName: "foobar" } + ); + + const url2 = new URL("parsingError.js", FIXTURE_URL); + await new AstAnalyser().analyseFile( + url, + { module: true, packageName: "foobar2" } + ); + + const calls = AstAnalyser.prototype.analyseFile.mock.calls; + assert.strictEqual(calls.length, 2); + + assert.deepEqual(calls[0].arguments, [url, { module: false, packageName: "foobar" }]); + assert.deepEqual(calls[1].arguments, [url2, { module: true, packageName: "foobar2" }]); + }); + + it("should implement new customProbes while keeping default probes", async() => { + const result = await new AstAnalyser( + { + parser: new JsSourceParser(), + customProbes, + skipDefaultProbes: false + } + ).analyseFile(new URL("customProbe.js", FIXTURE_URL)); + + assert.equal(result.warnings[0].kind, kWarningUnsafeDanger); + assert.equal(result.warnings[1].kind, kWarningUnsafeImport); + assert.equal(result.warnings[2].kind, kWarningUnsafeStmt); + assert.equal(result.warnings.length, 3); + }); + + it("should implement new customProbes while skipping/removing default probes", async() => { + const result = await new AstAnalyser( + { + parser: new JsSourceParser(), + customProbes, + skipDefaultProbes: true + } + ).analyseFile(new URL("customProbe.js", FIXTURE_URL)); + + assert.equal(result.warnings[0].kind, kWarningUnsafeDanger); + assert.equal(result.warnings.length, 1); + }); + describe("hooks", () => { const analyser = new AstAnalyser(); const url = new URL("depName.js", FIXTURE_URL); @@ -543,6 +610,42 @@ describe("AstAnalyser", (t) => { assert.deepStrictEqual(analyser.probesOptions.customProbes, []); assert.strictEqual(analyser.probesOptions.skipDefaultProbes, false); }); + + it("should properly instanciate default or custom parser (using analyseFile)", async(t) => { + t.mock.method(JsSourceParser.prototype, "parse"); + t.mock.method(FakeSourceParser.prototype, "parse"); + + await new AstAnalyser().analyseFile( + new URL("depName.js", FIXTURE_URL), + { module: false, packageName: "foobar" } + ); + + await new AstAnalyser( + { customParser: new FakeSourceParser() } + ).analyseFile( + new URL("parsingError.js", FIXTURE_URL), + { module: true, packageName: "foobar2" } + ); + + assert.strictEqual(JsSourceParser.prototype.parse.mock.calls.length, 1); + assert.strictEqual(FakeSourceParser.prototype.parse.mock.calls.length, 1); + }); + + it("should properly instanciate default or custom parser (using analyse)", (t) => { + t.mock.method(JsSourceParser.prototype, "parse"); + t.mock.method(FakeSourceParser.prototype, "parse"); + + new AstAnalyser().analyse("const http = require(\"http\");", { module: true, removeHTMLComments: true }); + + new AstAnalyser({ + customParser: new FakeSourceParser() + }).analyse("const fs = require(\"fs\");", + { module: false, removeHTMLComments: false } + ); + + assert.strictEqual(JsSourceParser.prototype.parse.mock.calls.length, 1); + assert.strictEqual(FakeSourceParser.prototype.parse.mock.calls.length, 1); + }); }); }); diff --git a/test/runASTAnalysis.spec.js b/test/runASTAnalysis.spec.js deleted file mode 100644 index bea68ca..0000000 --- a/test/runASTAnalysis.spec.js +++ /dev/null @@ -1,74 +0,0 @@ -// Import Node.js Dependencies -import { it } from "node:test"; -import assert from "node:assert"; - -// Import Internal Dependencies -import { runASTAnalysis, AstAnalyser, JsSourceParser } from "../index.js"; -import { FakeSourceParser } from "./fixtures/FakeSourceParser.js"; -import { - customProbes, - kIncriminedCodeSampleCustomProbe, - kWarningUnsafeDanger, - kWarningUnsafeImport, - kWarningUnsafeStmt -} from "./utils/index.js"; - -it("should call AstAnalyser.analyse with the expected arguments", (t) => { - t.mock.method(AstAnalyser.prototype, "analyse"); - - const source = "const http = require(\"http\");"; - new AstAnalyser().analyse(source, { module: true, removeHTMLComments: true }); - - const source2 = "const fs = require(\"fs\");"; - new AstAnalyser().analyse(source2, { module: false, removeHTMLComments: false }); - - const calls = AstAnalyser.prototype.analyse.mock.calls; - assert.strictEqual(calls.length, 2); - - assert.deepEqual(calls[0].arguments, [source, { module: true, removeHTMLComments: true }]); - assert.deepEqual(calls[1].arguments, [source2, { module: false, removeHTMLComments: false }]); -}); - -it("should instantiate AstAnalyser with the expected parser", (t) => { - t.mock.method(JsSourceParser.prototype, "parse"); - t.mock.method(FakeSourceParser.prototype, "parse"); - - new AstAnalyser().analyse("const http = require(\"http\");", { module: true, removeHTMLComments: true }); - - new AstAnalyser({ - customParser: new FakeSourceParser() - }).analyse("const fs = require(\"fs\");", - { module: false, removeHTMLComments: false } - ); - - assert.strictEqual(JsSourceParser.prototype.parse.mock.calls.length, 1); - assert.strictEqual(FakeSourceParser.prototype.parse.mock.calls.length, 1); -}); - -it("should append list of probes using runASTAnalysis", () => { - const result = new AstAnalyser( - { - parser: new JsSourceParser(), - customProbes, - skipDefaultProbes: false - } - ).analyse(kIncriminedCodeSampleCustomProbe); - - assert.equal(result.warnings[0].kind, kWarningUnsafeDanger); - assert.equal(result.warnings[1].kind, kWarningUnsafeImport); - assert.equal(result.warnings[2].kind, kWarningUnsafeStmt); - assert.equal(result.warnings.length, 3); -}); - -it("should replace list of probes using runASTAnalysis", () => { - const result = new AstAnalyser( - { - parser: new JsSourceParser(), - customProbes, - skipDefaultProbes: true - } - ).analyse(kIncriminedCodeSampleCustomProbe); - - assert.equal(result.warnings[0].kind, kWarningUnsafeDanger); - assert.equal(result.warnings.length, 1); -}); diff --git a/test/runASTAnalysisOnFile.spec.js b/test/runASTAnalysisOnFile.spec.js deleted file mode 100644 index d44f0cc..0000000 --- a/test/runASTAnalysisOnFile.spec.js +++ /dev/null @@ -1,89 +0,0 @@ -// Import Node.js Dependencies -import { it } from "node:test"; -import assert from "node:assert"; - -// Import Internal Dependencies -import { - AstAnalyser, - JsSourceParser -} from "../index.js"; -import { FakeSourceParser } from "./fixtures/FakeSourceParser.js"; -import { - customProbes, - kWarningUnsafeDanger, - kWarningUnsafeImport, - kWarningUnsafeStmt -} from "./utils/index.js"; - -// CONSTANTS -const FIXTURE_URL = new URL("fixtures/searchRuntimeDependencies/", import.meta.url); - -it("should call AstAnalyser.analyseFile with the expected arguments", async(t) => { - t.mock.method(AstAnalyser.prototype, "analyseFile"); - - const url = new URL("depName.js", FIXTURE_URL); - await new AstAnalyser().analyseFile( - url, - { module: false, packageName: "foobar" } - ); - - const url2 = new URL("parsingError.js", FIXTURE_URL); - await new AstAnalyser().analyseFile( - url, - { module: true, packageName: "foobar2" } - ); - - const calls = AstAnalyser.prototype.analyseFile.mock.calls; - assert.strictEqual(calls.length, 2); - - assert.deepEqual(calls[0].arguments, [url, { module: false, packageName: "foobar" }]); - assert.deepEqual(calls[1].arguments, [url2, { module: true, packageName: "foobar2" }]); -}); - -it("should instantiate AstAnalyser with the expected parser", async(t) => { - t.mock.method(JsSourceParser.prototype, "parse"); - t.mock.method(FakeSourceParser.prototype, "parse"); - - await new AstAnalyser().analyseFile( - new URL("depName.js", FIXTURE_URL), - { module: false, packageName: "foobar" } - ); - - await new AstAnalyser( - { customParser: new FakeSourceParser() } - ).analyseFile( - new URL("parsingError.js", FIXTURE_URL), - { module: true, packageName: "foobar2" } - ); - - assert.strictEqual(JsSourceParser.prototype.parse.mock.calls.length, 1); - assert.strictEqual(FakeSourceParser.prototype.parse.mock.calls.length, 1); -}); - -it("should append list of probes using runASTAnalysisOnFile", async() => { - const result = await new AstAnalyser( - { - parser: new JsSourceParser(), - customProbes, - skipDefaultProbes: false - } - ).analyseFile(new URL("customProbe.js", FIXTURE_URL)); - - assert.equal(result.warnings[0].kind, kWarningUnsafeDanger); - assert.equal(result.warnings[1].kind, kWarningUnsafeImport); - assert.equal(result.warnings[2].kind, kWarningUnsafeStmt); - assert.equal(result.warnings.length, 3); -}); - -it("should replace list of probes using runASTAnalysisOnFile", async() => { - const result = await new AstAnalyser( - { - parser: new JsSourceParser(), - customProbes, - skipDefaultProbes: true - } - ).analyseFile(new URL("customProbe.js", FIXTURE_URL)); - - assert.equal(result.warnings[0].kind, kWarningUnsafeDanger); - assert.equal(result.warnings.length, 1); -});