Skip to content

Commit

Permalink
refactor!: remove legacy runASTAnalysis and runASTAnalysisOnFile APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
fraxken committed Aug 16, 2024
1 parent 81e44c5 commit 9990244
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 239 deletions.
80 changes: 4 additions & 76 deletions index.js
Original file line number Diff line number Diff line change
@@ -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";
103 changes: 103 additions & 0 deletions test/AstAnalyser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
});
});
});

Expand Down
74 changes: 0 additions & 74 deletions test/runASTAnalysis.spec.js

This file was deleted.

89 changes: 0 additions & 89 deletions test/runASTAnalysisOnFile.spec.js

This file was deleted.

0 comments on commit 9990244

Please sign in to comment.