Skip to content

Commit

Permalink
Lint typings field file existence (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Aug 19, 2023
1 parent b264657 commit c1148cf
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
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

0 comments on commit c1148cf

Please sign in to comment.