Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lint typings field file existence #60

Merged
merged 2 commits into from
Aug 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions pkg/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,19 @@ export async function publint({ pkgDir, vfs, level, strict, _packedFiles }) {
}

// check file existence for other known package fields
const knownFields = ['types', 'jsnext:main', 'jsnext', 'unpkg', 'jsdelivr']
// if has typesVersions field, it complicates `types` field resolution a lot.
const knownFields = [
'types',
'typings',
'jsnext:main',
'jsnext',
'unpkg',
'jsdelivr'
]
// if has typesVersions field, it complicates `types`/`typings` field resolution a lot.
// for now skip it, but further improvements are tracked at
// https://github.com/bluwy/publint/issues/42
if (getPublishedField(rootPkg, 'typesVersions')[0]) {
knownFields.splice(0, 1)
knownFields.splice(0, 2)
}
for (const field of knownFields) {
const [fieldValue, fieldPkgPath] = getPublishedField(rootPkg, field)
Expand Down Expand Up @@ -723,8 +730,11 @@ export async function publint({ pkgDir, vfs, level, strict, _packedFiles }) {
let typesFilePath
if (exportsKey == null || exportsKey === '.') {
const [types] = getPublishedField(rootPkg, 'types')
const [typings] = getPublishedField(rootPkg, 'typings')
if (types) {
typesFilePath = types
} else if (typings) {
typesFilePath = typings
} else if (await readFile(vfs.pathJoin(pkgDir, './index.d.ts'))) {
typesFilePath = './index.d.ts'
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/tests/fixtures/test-2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"private": true,
"main": "./main.mjs",
"browser": "./lib/bar.js",
"typings": "./doesn't-exist.d.ts",
"exports": {
"./*.js": "./lib/*.js",
"./browser": {
Expand All @@ -25,4 +26,4 @@
}
}
}
}
}
10 changes: 9 additions & 1 deletion pkg/tests/playground.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ testFixture('test-1', ['FILE_INVALID_FORMAT', 'TYPES_NOT_EXPORTED'])
testFixture('test-2', [
'EXPORTS_MODULE_SHOULD_BE_ESM',
'EXPORTS_VALUE_INVALID',
'FILE_DOES_NOT_EXIST',
'FILE_INVALID_FORMAT',
'FILE_INVALID_FORMAT',
'USE_EXPORTS_BROWSER'
Expand All @@ -47,6 +48,7 @@ testFixture(
[
'EXPORTS_MODULE_SHOULD_BE_ESM',
'EXPORTS_VALUE_INVALID',
'FILE_DOES_NOT_EXIST',
'FILE_INVALID_FORMAT',
'FILE_INVALID_FORMAT'
],
Expand All @@ -55,7 +57,11 @@ testFixture(

testFixture(
'test-2 (level: error)',
['EXPORTS_MODULE_SHOULD_BE_ESM', 'EXPORTS_VALUE_INVALID'],
[
'EXPORTS_MODULE_SHOULD_BE_ESM',
'EXPORTS_VALUE_INVALID',
'FILE_DOES_NOT_EXIST'
],
{ level: 'error' }
)

Expand All @@ -64,6 +70,7 @@ testFixture(
[
'EXPORTS_MODULE_SHOULD_BE_ESM',
'EXPORTS_VALUE_INVALID',
'FILE_DOES_NOT_EXIST',
'FILE_INVALID_FORMAT',
'FILE_INVALID_FORMAT'
],
Expand All @@ -75,6 +82,7 @@ testFixture(
[
{ code: 'EXPORTS_MODULE_SHOULD_BE_ESM', type: 'error' },
{ code: 'EXPORTS_VALUE_INVALID', type: 'error' },
{ code: 'FILE_DOES_NOT_EXIST', type: 'error' },
{ code: 'FILE_INVALID_FORMAT', type: 'error' },
{ code: 'FILE_INVALID_FORMAT', type: 'error' },
{ code: 'USE_EXPORTS_BROWSER', type: 'suggestion' }
Expand Down