Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #171 from ext/feature/exports-types-order
feat: ensure typescript `types` comes first in `exports`
- Loading branch information
Showing
4 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import PackageJson from "../types/package-json"; | ||
import { exportsTypesOrder } from "./exports-types-order"; | ||
|
||
let pkg: PackageJson; | ||
|
||
beforeEach(() => { | ||
pkg = { | ||
name: "mock-package", | ||
version: "1.2.3", | ||
}; | ||
}); | ||
|
||
it("should report error when types isn't first", () => { | ||
expect.assertions(1); | ||
pkg.exports = { | ||
require: "./index.cjs", | ||
import: "./index.mjs", | ||
types: "./index.d.ts", | ||
}; | ||
expect(Array.from(exportsTypesOrder(pkg))).toMatchInlineSnapshot(` | ||
[ | ||
{ | ||
"column": 1, | ||
"line": 1, | ||
"message": ""types" must be the first condition in "exports"", | ||
"ruleId": "exports-types-order", | ||
"severity": 2, | ||
}, | ||
] | ||
`); | ||
}); | ||
|
||
it("should not report error when types is first", () => { | ||
expect.assertions(1); | ||
pkg.exports = { | ||
types: "./index.d.ts", | ||
require: "./index.cjs", | ||
import: "./index.mjs", | ||
}; | ||
expect(Array.from(exportsTypesOrder(pkg))).toMatchInlineSnapshot(`[]`); | ||
}); | ||
|
||
it("should handle nested objects", () => { | ||
expect.assertions(1); | ||
pkg.exports = { | ||
"./foo": { | ||
require: "./foo.cjs", | ||
import: "./foo.mjs", | ||
types: "./foo.d.ts", | ||
}, | ||
"./bar": { | ||
deeply: { | ||
nested: { | ||
require: "./bar.cjs", | ||
import: "./bar.mjs", | ||
types: "./bar.d.ts", | ||
}, | ||
}, | ||
}, | ||
}; | ||
expect(Array.from(exportsTypesOrder(pkg))).toMatchInlineSnapshot(` | ||
[ | ||
{ | ||
"column": 1, | ||
"line": 1, | ||
"message": ""types" must be the first condition in "exports["./foo"]"", | ||
"ruleId": "exports-types-order", | ||
"severity": 2, | ||
}, | ||
{ | ||
"column": 1, | ||
"line": 1, | ||
"message": ""types" must be the first condition in "exports["./bar"]["deeply"]["nested"]"", | ||
"ruleId": "exports-types-order", | ||
"severity": 2, | ||
}, | ||
] | ||
`); | ||
}); | ||
|
||
it("should handle when exports are missing", () => { | ||
expect.assertions(1); | ||
expect(Array.from(exportsTypesOrder(pkg))).toMatchInlineSnapshot(`[]`); | ||
}); | ||
|
||
it("should handle when exports is string", () => { | ||
expect.assertions(1); | ||
pkg.exports = "./index.js"; | ||
expect(Array.from(exportsTypesOrder(pkg))).toMatchInlineSnapshot(`[]`); | ||
}); | ||
|
||
it("should handle when exports is empty", () => { | ||
expect.assertions(1); | ||
pkg.exports = {}; | ||
expect(Array.from(exportsTypesOrder(pkg))).toMatchInlineSnapshot(`[]`); | ||
}); | ||
|
||
it("should handle when exports does not include types", () => { | ||
expect.assertions(1); | ||
pkg.exports = { | ||
require: "./index.cjs", | ||
import: "./index.mjs", | ||
}; | ||
expect(Array.from(exportsTypesOrder(pkg))).toMatchInlineSnapshot(`[]`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { Severity } from "@html-validate/stylish"; | ||
import { Message } from "../message"; | ||
import PackageJson, { PackageJsonExports } from "../types/package-json"; | ||
|
||
const ruleId = "exports-types-order"; | ||
const severity = Severity.ERROR; | ||
|
||
function* validateOrder( | ||
value: string | PackageJsonExports | null, | ||
path: string[] | ||
): Generator<Message> { | ||
if (!value || typeof value === "string") { | ||
return; | ||
} | ||
|
||
const keys = Object.keys(value); | ||
if (keys.length === 0) { | ||
return; | ||
} | ||
|
||
if (keys.includes("types") && keys[0] !== "types") { | ||
const property = path.map((it) => `["${it}"]`).join(""); | ||
yield { | ||
ruleId, | ||
severity, | ||
message: `"types" must be the first condition in "exports${property}"`, | ||
line: 1, | ||
column: 1, | ||
}; | ||
} | ||
|
||
for (const key of keys) { | ||
yield* validateOrder(value[key], [...path, key]); | ||
} | ||
} | ||
|
||
export function* exportsTypesOrder(pkg: PackageJson): Generator<Message> { | ||
if (pkg.exports) { | ||
yield* validateOrder(pkg.exports, []); | ||
} | ||
} |