Skip to content

Commit 62c1625

Browse files
authoredApr 1, 2023
refactor: rewrites remaining cjs tests to esm (#768)
1 parent e4eb94d commit 62c1625

20 files changed

+171
-134
lines changed
 

‎.eslintrc.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
module.exports = {
2+
root: true,
3+
extends: ["moving-meadow", "plugin:@typescript-eslint/recommended"],
4+
plugins: ["@typescript-eslint"],
5+
parser: "@typescript-eslint/parser",
6+
parserOptions: {
7+
ecmaVersion: 2021,
8+
},
9+
rules: {
10+
"@typescript-eslint/no-explicit-any": "off", // I'd rather have this explicitly so I can see where they are
11+
"@typescript-eslint/no-var-requires": "off", // we kind-of live off of those in here
12+
"@typescript-eslint/no-unused-vars": "off", // duplicate of the same in several other sets
13+
"budapestian/global-constant-pattern": "off", // currently does not work with the AST as emitted @typescript-eslint parser (FIXME)
14+
"no-param-reassign": "error",
15+
"security/detect-non-literal-fs-filename": "off",
16+
"unicorn/prefer-top-level-await": "off", // only works as of node 16
17+
"unicorn/no-useless-fallback-in-spread": "off", // useful, probably. We'll try it later, though
18+
"unicorn/prefer-node-protocol": "off", // yarn 1 pnp doesn't understand node: protocol, and as we still support yarn 1 pnp we're not doing this
19+
},
20+
overrides: [
21+
{
22+
files: ["**/*.d.ts"],
23+
rules: {
24+
"init-declarations": "off", // in transient contexts it's not even _possible_ to init declarations
25+
},
26+
},
27+
{
28+
files: ["test/**/*.{js,mjs,cjs}"],
29+
env: {
30+
mocha: true,
31+
},
32+
rules: {
33+
"max-lines": "off",
34+
"max-lines-per-function": "off",
35+
"mocha/valid-suite-description": [
36+
"error",
37+
{
38+
pattern: "^\\[[EIU]\\]",
39+
suiteNames: ["describe"],
40+
message:
41+
"start suite titles with [E], [I] or [U] to mark them as E2E, Integration or Unit test suite",
42+
},
43+
],
44+
},
45+
},
46+
{
47+
files: ["**/*.mjs"],
48+
rules: {
49+
"node/no-unsupported-features/es-syntax": "off",
50+
},
51+
},
52+
],
53+
ignorePatterns: [
54+
".pnp.cjs",
55+
".yarn",
56+
"node_modules",
57+
"coverage",
58+
"tmp",
59+
"src/**/*.schema.js",
60+
"src/**/*.template.js",
61+
"src/cli/tools/svg-in-html-snippets/script.snippet.js",
62+
"test/integration/**",
63+
"test/*/__fixtures__/**",
64+
"test/*/*/__fixtures__/**",
65+
"test/*/*/*/__fixtures__/**",
66+
"test/*/__mocks__/**",
67+
"test/*/*/__mocks__/**",
68+
"types/**",
69+
],
70+
};

‎.eslintrc.json

-57
This file was deleted.

‎.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
strategy:
2626
fail-fast: false
2727
matrix:
28-
node-version: [14.x, 16.x, 19.x]
28+
node-version: [16.x, 19.x]
2929
platform: [ubuntu-latest]
3030
runs-on: ${{matrix.platform}}
3131
steps:

‎src/cli/init-config/config.js.template.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/extract/ast-extractors/swc-dependency-visitor.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,9 @@ if (VisitorModule) {
148148
visitExportAllDeclration(pNode) {
149149
this.pushImportExportSource(pNode);
150150
/* c8 ignore start */
151-
// @ts-ignore
151+
// @ts-expect-error see above
152152
if (super.visitExportAllDeclration) {
153-
// @ts-ignore
153+
// @ts-expect-error see above
154154
return super.visitExportAllDeclration(pNode);
155155
} else {
156156
/* c8 ignore stop */
@@ -168,9 +168,9 @@ if (VisitorModule) {
168168
visitExportNamedDeclration(pNode) {
169169
this.pushImportExportSource(pNode);
170170
/* c8 ignore start */
171-
// @ts-ignore
171+
// @ts-expect-error see above
172172
if (super.visitExportNamedDeclration) {
173-
// @ts-ignore
173+
// @ts-expect-error see above
174174
return super.visitExportNamedDeclration(pNode);
175175
} else {
176176
/* c8 ignore stop */

‎src/extract/parse/to-javascript-ast.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function getASTFromSource(pFileRecord, pTranspileOptions) {
3333
if (needsJSXTreatment(pFileRecord, pTranspileOptions)) {
3434
return acornJsxParser.parse(lJavaScriptSource, {
3535
...ACORN_OPTIONS,
36-
// @ts-ignore
36+
// @ts-expect-error because ...
3737
// acornJsxParser.parse takes an acorn.Options which doesn't include
3838
// allowNamespacedObjects. acornJsx.Options doesn't include sourceType
3939
// though, so a bit rock < this code > hard place hence ignore it

‎src/extract/parse/to-swc-ast.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ module.exports = {
3939
* @return {boolean} - true if the swc compiler is available,
4040
* false in all other cases
4141
*/
42-
// @ts-ignore
42+
// @ts-expect-error dfdfd
4343
isAvailable: () => swc !== false,
4444

4545
/**

‎src/extract/parse/to-typescript-ast.js

-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ module.exports = {
6363
* @return {boolean} - true if the typescript compiler is available,
6464
* false in all other cases
6565
*/
66-
// @ts-ignore
6766
isAvailable: () => typescript !== false,
6867

6968
/**

‎src/main/index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
// workaround to nodenext shenanigans with the TypeScript compiler
22
// see https://github.com/sverweij/dependency-cruiser/issues/754
3+
// eslint-disable-next-line import/no-unresolved, node/no-missing-import, node/no-unpublished-import
34
export * from "../../types/dependency-cruiser";

‎src/main/options/normalize.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ function normalizeCruiseOptions(pOptions, pFileAndDirectoryArray = []) {
184184
args: pFileAndDirectoryArray.join(" "),
185185
};
186186

187-
// @ts-ignore the idea of normalizing maxDepth to number is that after
188-
// that we're sure it's a number. Should maybe best be solved by
187+
// @ts-expect-error the idea of normalizing maxDepth to number is that after
188+
// that we're sure it's a number. Should maybe best be solved by
189189
// having two types/ interfaces
190190
lReturnValue.maxDepth = Number.parseInt(lReturnValue.maxDepth, 10);
191191
lReturnValue.moduleSystems = uniq(lReturnValue.moduleSystems);

‎src/main/resolve-options/normalize.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ function compileResolveOptions(
9191
const TsConfigPathsPlugin = require("tsconfig-paths-webpack-plugin");
9292
lResolveOptions.plugins = pushPlugin(
9393
lResolveOptions.plugins,
94-
// @ts-ignore TS2351 "TsConfPathsPlugin is not constructable" - is unjustified
94+
// @ts-expect-error TS2351 "TsConfPathsPlugin is not constructable" - is unjustified
9595
new TsConfigPathsPlugin({
9696
configFile: pResolveOptions.tsConfig,
9797
// TsConfigPathsPlugin doesn't (can't) read enhanced-resolve's

‎src/utl/find-all-files.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function readIgnoreFile(pFileName) {
7272
/**
7373
* @type FilterFunctionType
7474
*/
75-
// eslint-disable-next-line no-unused-vars
75+
7676
function identityFilter(_pString, _pIndex, _pArray) {
7777
return true;
7878
}

‎src/utl/regex-util.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function extractGroups(pFromRestriction, pActualPath) {
1515
let lReturnValue = [];
1616

1717
if (pFromRestriction.path) {
18-
// @ts-ignore pFromRestriction.path can be a string | string[] | null
18+
// @ts-expect-error pFromRestriction.path can be a string | string[] | null
1919
// except before it enters here it has already been 'normalized' to a string
2020
// so it can be safely passed to match. The right solution here (TODO)
2121
// is to create a separate type for NormalizedFromRestriction

‎src/validate/index.d.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
/* eslint-disable node/no-unpublished-import */
2+
/* eslint-disable node/no-missing-import */
3+
/* eslint-disable import/no-unresolved */
14
import {
25
IDependency,
36
IFolder,
47
IFolderDependency,
58
IModule,
9+
IFlattenedRuleSet,
610
} from "../../types/cruise-result";
7-
import { IFlattenedRuleSet } from "../../types/rule-set";
811
import { SeverityType } from "../../types/shared-types";
912

1013
export interface IValidationResult {

‎src/validate/match-module-rule.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const matchers = require("./matchers");
1414
function matchesOrphanRule(pRule, pModule) {
1515
return (
1616
has(pRule, "from.orphan") &&
17-
// @ts-ignore the 'has' above guarantees there's a 'from.orphan' attributre
17+
// @ts-expect-error the 'has' above guarantees there's a 'from.orphan' attribute
1818
pModule.orphan === pRule.from.orphan &&
1919
matchers.fromPath(pRule, pModule) &&
2020
matchers.fromPathNot(pRule, pModule)
@@ -32,11 +32,11 @@ function matchesOrphanRule(pRule, pModule) {
3232
*/
3333
function matchesReachableRule(pRule, pModule) {
3434
if (has(pRule, "to.reachable") && has(pModule, "reachable")) {
35-
// @ts-ignore the 'has' above ensures the 'reachable' exists
35+
// @ts-expect-error the 'has' above ensures the 'reachable' exists
3636
const lReachableRecord = pModule.reachable.find(
3737
(pReachable) =>
3838
pReachable.asDefinedInRule === pRule.name &&
39-
// @ts-ignore the 'has' above ensures the 'to.reachable' exists
39+
// @ts-expect-error the 'has' above ensures the 'to.reachable' exists
4040
pReachable.value === pRule.to.reachable
4141
);
4242
if (lReachableRecord) {
@@ -64,7 +64,7 @@ function matchesReachesRule(pRule, pModule) {
6464
return (
6565
has(pRule, "to.reachable") &&
6666
has(pModule, "reaches") &&
67-
// @ts-ignore the 'has' above guarantees the .reaches exists
67+
// @ts-expect-error the 'has' above guarantees the .reaches exists
6868
pModule.reaches.some(
6969
(pReaches) =>
7070
pReaches.asDefinedInRule === pRule.name &&

‎test/config-utl/extract-depcruise-config/index.spec.cjs ‎test/config-utl/extract-depcruise-config/index.spec.mjs

+34-15
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
1-
const path = require("path");
2-
const { expect } = require("chai");
3-
const loadConfig = require("../../../src/config-utl/extract-depcruise-config");
4-
const fixture = require("./__mocks__/rules.sub-not-allowed-error.json");
5-
const mergedFixture = require("./__mocks__/extends/merged.json");
6-
const mergedArrayOneFixture = require("./__mocks__/extends/merged-array-1.json");
7-
const mergedArrayTwoFixture = require("./__mocks__/extends/merged-array-2.json");
1+
import { fileURLToPath } from "node:url";
2+
import path from "node:path";
3+
import { expect } from "chai";
4+
import loadConfig from "../../../src/config-utl/extract-depcruise-config/index.js";
85

6+
const __dirname = fileURLToPath(new URL(".", import.meta.url));
97
const mockDirectory = path.join(__dirname, "__mocks__");
108

119
describe("[I] config-utl/compile-config", () => {
12-
it("a rule set without an extends returns just that rule set", () => {
10+
it("a rule set without an extends returns just that rule set", async () => {
11+
const fixture = await import(
12+
"./__mocks__/rules.sub-not-allowed-error.json",
13+
{
14+
assert: { type: "json" },
15+
}
16+
);
1317
expect(
1418
loadConfig(path.join(mockDirectory, "rules.sub-not-allowed-error.json"))
15-
).to.deep.equal(fixture);
19+
).to.deep.equal(fixture.default);
1620
});
1721

18-
it("a rule set with an extends returns that rule set, extending the mentioned base", () => {
22+
it("a rule set with an extends returns that rule set, extending the mentioned base", async () => {
23+
const mergedFixture = await import("./__mocks__/extends/merged.json", {
24+
assert: { type: "json" },
25+
});
1926
expect(
2027
loadConfig(path.join(mockDirectory, "extends/extending.json"))
21-
).to.deep.equal(mergedFixture);
28+
).to.deep.equal(mergedFixture.default);
2229
});
2330

2431
it("a rule set with an extends array (0 members) returns that rule set", () => {
@@ -40,23 +47,35 @@ describe("[I] config-utl/compile-config", () => {
4047
});
4148
});
4249

43-
it("a rule set with an extends array (1 member) returns that rule set, extending the mentioned base", () => {
50+
it("a rule set with an extends array (1 member) returns that rule set, extending the mentioned base", async () => {
51+
const mergedArrayOneFixture = await import(
52+
"./__mocks__/extends/merged-array-1.json",
53+
{
54+
assert: { type: "json" },
55+
}
56+
);
4457
expect(
4558
loadConfig(
4659
path.join(mockDirectory, "extends/extending-array-with-one-member.json")
4760
)
48-
).to.deep.equal(mergedArrayOneFixture);
61+
).to.deep.equal(mergedArrayOneFixture.default);
4962
});
5063

51-
it("a rule set with an extends array (>1 member) returns that rule set, extending the mentioned bases", () => {
64+
it("a rule set with an extends array (>1 member) returns that rule set, extending the mentioned bases", async () => {
65+
const mergedArrayTwoFixture = await import(
66+
"./__mocks__/extends/merged-array-2.json",
67+
{
68+
assert: { type: "json" },
69+
}
70+
);
5271
expect(
5372
loadConfig(
5473
path.join(
5574
mockDirectory,
5675
"extends/extending-array-with-two-members.json"
5776
)
5877
)
59-
).to.deep.equal(mergedArrayTwoFixture);
78+
).to.deep.equal(mergedArrayTwoFixture.default);
6079
});
6180

6281
it("a rule set with an extends from node_modules gets merged properly as well", () => {

‎test/report/plugins/index.get-external-plugin-reporter.spec.cjs ‎test/report/plugins/index.get-external-plugin-reporter.spec.mjs

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
const path = require("path");
2-
const { expect } = require("chai");
3-
const { getExternalPluginReporter } = require("../../../src/report/plugins");
1+
import { join } from "node:path";
2+
import { fileURLToPath } from "node:url";
3+
import { expect } from "chai";
4+
import { getExternalPluginReporter } from "../../../src/report/plugins.js";
45

5-
const FIXTURE_DIRECTORY = path.join(__dirname, "__fixtures__");
6+
const __dirname = fileURLToPath(new URL(".", import.meta.url));
7+
const FIXTURE_DIRECTORY = join(__dirname, "__fixtures__");
68

79
describe("[I] report/plugins - getExternalPluginReporter", () => {
810
it("throws when the plugin:reporter is not a valid plugin (missing exit code)", () => {
9-
const lNoExitCodePlugin = path.join(
11+
const lNoExitCodePlugin = join(
1012
FIXTURE_DIRECTORY,
1113
"invalid-no-exit-code-plugin.cjs"
1214
);
@@ -16,7 +18,7 @@ describe("[I] report/plugins - getExternalPluginReporter", () => {
1618
});
1719

1820
it("throws when the plugin:reporter is not a valid plugin (missing output)", () => {
19-
const lNoExitCodePlugin = path.join(
21+
const lNoExitCodePlugin = join(
2022
FIXTURE_DIRECTORY,
2123
"invalid-no-output-plugin.cjs"
2224
);

‎test/report/plugins/index.is-valid-plugin.spec.cjs

-37
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { expect } from "chai";
2+
import { isValidPlugin } from "../../../src/report/plugins.js";
3+
4+
describe("[U] report/plugins - isValidPlugin", () => {
5+
it("returns false when the plugin's function doesn't return an exit code attribute", async () => {
6+
const lCandidatePlugin = await import(
7+
"./__fixtures__/invalid-no-exit-code-plugin.cjs"
8+
);
9+
expect(isValidPlugin(lCandidatePlugin.default)).to.equal(false);
10+
});
11+
12+
it("returns false when the plugin's function doesn't return an output attribute", async () => {
13+
const lCandidatePlugin = await import(
14+
"./__fixtures__/invalid-no-output-plugin.cjs"
15+
);
16+
expect(isValidPlugin(lCandidatePlugin.default)).to.equal(false);
17+
});
18+
19+
it("returns false when the plugin's function doesn't return an exit code that is not a number", async () => {
20+
const lCandidatePlugin = await import(
21+
"./__fixtures__/invalid-non-number-exit-code-plugin.cjs"
22+
);
23+
expect(isValidPlugin(lCandidatePlugin.default)).to.equal(false);
24+
});
25+
26+
it("returns false when the plugin doesn't return a function", async () => {
27+
const lCandidatePlugin = await import(
28+
"./__fixtures__/invalid-not-a-function-plugin.cjs"
29+
);
30+
expect(isValidPlugin(lCandidatePlugin.default)).to.equal(false);
31+
});
32+
33+
it("returns true when the plugin doesn't returns a function that takes a minimal cruise result and returns an output attribute + an numerical exitCode attribute", async () => {
34+
const lCandidatePlugin = await import("./__fixtures__/valid-plugin.cjs");
35+
expect(isValidPlugin(lCandidatePlugin.default)).to.equal(true);
36+
});
37+
});

0 commit comments

Comments
 (0)
Please sign in to comment.