Skip to content

Commit

Permalink
Merge pull request #161 from ext/refactor/strict-null-checks
Browse files Browse the repository at this point in the history
refactor: enable strictNullChecks
  • Loading branch information
ext committed Apr 14, 2023
2 parents ae885f5 + c6518d5 commit 7d4a5cd
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 22 deletions.
8 changes: 4 additions & 4 deletions src/index.ts
Expand Up @@ -25,8 +25,8 @@ interface ParsedArgs {
}

interface GetPackageJsonResults {
pkg?: PackageJson;
pkgPath?: string;
pkg: PackageJson;
pkgPath: string;
}

async function preloadStdin(): Promise<string> {
Expand All @@ -37,7 +37,7 @@ async function preloadStdin(): Promise<string> {
return;
}

const st = createWriteStream(null, { fd, autoClose: true });
const st = createWriteStream("", { fd, autoClose: true });
process.stdin.pipe(st);
st.on("finish", () => {
resolve(path);
Expand All @@ -52,7 +52,7 @@ async function preloadStdin(): Promise<string> {
async function getPackageJson(
args: ParsedArgs,
regenerateReportName: boolean
): Promise<GetPackageJsonResults> {
): Promise<GetPackageJsonResults | Record<string, never>> {
/* get from explicit path passed as argument */
if (args.pkgfile) {
return {
Expand Down
2 changes: 1 addition & 1 deletion src/package-json.spec.ts
Expand Up @@ -7,7 +7,7 @@ import { npmInfoMockDefault } from "./utils/npm-info";

jest.mock("./utils/npm-info");

let pkg: PackageJson;
let pkg: PackageJson & Required<Pick<PackageJson, "engines">>;

beforeEach(() => {
pkg = {
Expand Down
14 changes: 10 additions & 4 deletions src/rules/outdated-engines.spec.ts
Expand Up @@ -28,7 +28,9 @@ describe("should return error when unsupported version satisfies engines.node",
${">= 12.x"} | ${"Node 12"}
`("$description", ({ range, description }) => {
expect.assertions(1);
pkg.engines.node = range;
pkg.engines = {
node: range,
};
/* eslint-disable-next-line security/detect-non-literal-regexp -- not under user control */
const message = new RegExp(
String.raw`engines\.node is satisfied by ${description} \(EOL since \d{4}-.*\)`
Expand All @@ -47,7 +49,9 @@ describe("should return error when unsupported version satisfies engines.node",

it("should return error engines.node is not a valid semver range", () => {
expect.assertions(1);
pkg.engines.node = "foobar";
pkg.engines = {
node: "foobar",
};
expect(Array.from(outdatedEngines(pkg))).toMatchInlineSnapshot(`
[
{
Expand All @@ -63,7 +67,7 @@ it("should return error engines.node is not a valid semver range", () => {

it("should return error engines.node is missing", () => {
expect.assertions(1);
delete pkg.engines.node;
pkg.engines = {};
expect(Array.from(outdatedEngines(pkg))).toMatchInlineSnapshot(`
[
{
Expand Down Expand Up @@ -95,6 +99,8 @@ it("should return error engines is missing", () => {

it("should not return error when engines.node only supports active versions", () => {
expect.assertions(1);
pkg.engines.node = ">= 14";
pkg.engines = {
node: ">= 14",
};
expect(Array.from(outdatedEngines(pkg))).toMatchInlineSnapshot(`[]`);
});
2 changes: 1 addition & 1 deletion src/rules/outdated-engines.ts
Expand Up @@ -41,7 +41,7 @@ export function* outdatedEngines(pkg: PackageJson): Generator<Message> {
if (!semver.satisfies(expanded, range)) {
continue;
}
const nodeRelease = parsed.major || `0.${parsed.minor}`;
const nodeRelease = parsed?.major || `0.${parsed?.minor}`;
const message = `engines.node is satisfied by Node ${nodeRelease} (EOL since ${descriptor.eol})`;
yield {
ruleId,
Expand Down
19 changes: 13 additions & 6 deletions src/rules/types-node-matching-engine.spec.ts
@@ -1,20 +1,21 @@
import PackageJson from "../types/package-json";
import { typesNodeMatchingEngine } from "./types-node-matching-engine";

let pkg: PackageJson;
let pkg: PackageJson & Required<Pick<PackageJson, "engines">>;

beforeEach(() => {
pkg = {
name: "mock-package",
version: "1.2.3",
devDependencies: {},
engines: {},
};
});

it("should return error when engine is lower major than types", () => {
expect.assertions(1);
pkg.devDependencies["@types/node"] = "^14.1.2";
pkg.devDependencies = {
"@types/node": "^14.1.2",
};
pkg.engines.node = ">= 12";
expect(Array.from(typesNodeMatchingEngine(pkg))).toMatchInlineSnapshot(`
[
Expand All @@ -31,7 +32,9 @@ it("should return error when engine is lower major than types", () => {

it("should return error when engine is higher major than types", () => {
expect.assertions(1);
pkg.devDependencies["@types/node"] = "^12.1.2";
pkg.devDependencies = {
"@types/node": "^12.1.2",
};
pkg.engines.node = ">= 14";
expect(Array.from(typesNodeMatchingEngine(pkg))).toMatchInlineSnapshot(`
[
Expand All @@ -48,14 +51,18 @@ it("should return error when engine is higher major than types", () => {

it("should not return error when engine and types have same major", () => {
expect.assertions(1);
pkg.devDependencies["@types/node"] = "^12.1.2";
pkg.devDependencies = {
"@types/node": "^12.1.2",
};
pkg.engines.node = ">= 12";
expect(Array.from(typesNodeMatchingEngine(pkg))).toEqual([]);
});

it("should handle || in engine constraint", () => {
expect.assertions(1);
pkg.devDependencies["@types/node"] = "^14.1.2";
pkg.devDependencies = {
"@types/node": "^14.1.2",
};
pkg.engines.node = "^10.2.3 || ^12.2.3 || 14.2.3";
expect(Array.from(typesNodeMatchingEngine(pkg))).toMatchInlineSnapshot(`
[
Expand Down
4 changes: 4 additions & 0 deletions src/rules/verify-engine-constraint.ts
Expand Up @@ -29,6 +29,10 @@ export async function verifyEngineConstraint(pkg: PackageJson): Promise<Message[
}

const minDeclared = semver.minVersion(declaredConstraint);
if (!minDeclared) {
throw new Error(`Failed to parse engine constraint "${declaredConstraint}"`);
}

const messages: Message[] = [];

for await (const dependency of getDeepDependencies(pkg)) {
Expand Down
5 changes: 3 additions & 2 deletions src/utils/__mocks__/npm-info.ts
Expand Up @@ -4,8 +4,9 @@ const mock: Map<string, PackageJson> = new Map();
let defaultInfo: PackageJson | null = null;

export async function npmInfo(pkg: string): Promise<PackageJson> {
if (mock.has(pkg)) {
return mock.get(pkg);
const mocked = mock.get(pkg);
if (mocked) {
return mocked;
} else if (defaultInfo) {
return defaultInfo;
} else {
Expand Down
5 changes: 3 additions & 2 deletions src/utils/npm-info.ts
Expand Up @@ -4,8 +4,9 @@ import PackageJson from "../types/package-json";
const cache: Map<string, PackageJson> = new Map();

export async function npmInfo(pkg: string): Promise<PackageJson> {
if (cache.has(pkg)) {
return cache.get(pkg);
const cached = cache.get(pkg);
if (cached) {
return cached;
}

const result = await execa("npm", ["info", "--json", pkg]);
Expand Down
3 changes: 1 addition & 2 deletions tsconfig.json
Expand Up @@ -13,8 +13,7 @@
"resolveJsonModule": true,
"sourceMap": true,
"target": "es2017",
"strict": true,
"strictNullChecks": false
"strict": true
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules", "**/__mocks__"]
Expand Down

0 comments on commit 7d4a5cd

Please sign in to comment.