Skip to content

Commit

Permalink
Add a warning for jsnext field (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Dec 1, 2023
1 parent cba0a98 commit 2e485af
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 9 deletions.
1 change: 1 addition & 0 deletions pkg/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export type Message =
browserishCondition: string
}
>
| BaseMessage<'DEPRECATED_FIELD_JSNEXT'>

export interface Options {
/**
Expand Down
34 changes: 25 additions & 9 deletions pkg/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,28 +196,44 @@ export async function publint({ pkgDir, vfs, level, strict, _packedFiles }) {

// check file existence for other known package fields
const knownFields = [
'types',
'typings',
'jsnext:main',
'jsnext',
'unpkg',
'jsdelivr'
{ name: 'types' },
{ name: 'typings' },
{
name: 'jsnext:main',
deprecatedCode: /** @type {const} */ ('DEPRECATED_FIELD_JSNEXT')
},
{
name: 'jsnext',
deprecatedCode: /** @type {const} */ ('DEPRECATED_FIELD_JSNEXT')
},
{ name: 'unpkg' },
{ name: '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, 2)
}
for (const field of knownFields) {
const [fieldValue, fieldPkgPath] = getPublishedField(rootPkg, field)
for (const { name: fieldName, deprecatedCode } of knownFields) {
const [fieldValue, fieldPkgPath] = getPublishedField(rootPkg, fieldName)
if (
fieldValue != null &&
ensureTypeOfField(fieldValue, ['string'], fieldPkgPath)
) {
promiseQueue.push(async () => {
const fieldPath = vfs.pathJoin(pkgDir, fieldValue)
await readFile(fieldPath, fieldPkgPath, ['.js', '/index.js'])
const hasContent =
(await readFile(fieldPath, fieldPkgPath, ['.js', '/index.js'])) !==
false
if (hasContent && deprecatedCode) {
messages.push({
code: deprecatedCode,
args: {},
path: fieldPkgPath,
type: 'warning'
})
}
})
}
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/src/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ export function formatMessage(m, pkg) {
case 'EXPORTS_VALUE_CONFLICTS_WITH_BROWSER':
// prettier-ignore
return `${c.bold(fp(m.path))} is ${c.bold(pv(m.path))} which also matches ${c.bold(fp(m.args.browserPath))}: "${c.bold(pv(m.args.browserPath))}", which overrides the path when building the library with the "${c.bold(m.args.browserishCondition)}" condition. This is usually unintentional and may cause build issues. Consider using a different file name for ${c.bold(pv(m.path))}.`
case 'DEPRECATED_FIELD_JSNEXT':
// prettier-ignore
return `${c.bold(fp(m.path))} is deprecated. ${c.bold('pkg.module')} should be used instead.`
default:
return
}
Expand Down
Empty file.
8 changes: 8 additions & 0 deletions pkg/tests/fixtures/deprecated-fields/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "publint-glob-deprecated",
"version": "0.0.1",
"private": true,
"main": "./index.js",
"jsnext": "./index.js",
"jsnext:main": "./index.js"
}
5 changes: 5 additions & 0 deletions pkg/tests/playground.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ testFixture('types-versions', [])

testFixture('umd', ['FILE_INVALID_FORMAT', 'FILE_INVALID_FORMAT'])

testFixture('deprecated-fields', [
'DEPRECATED_FIELD_JSNEXT',
'DEPRECATED_FIELD_JSNEXT'
])

/**
* @typedef {{
* level?: import('../index.d.ts').Options['level']
Expand Down
4 changes: 4 additions & 0 deletions site/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,7 @@ When matching the `"worker"` condition, it will resolve to `"./lib.server.js"` w
This is usually not intended and causes the wrong file to be loaded. If it is intended, the `"worker"` condition should point to `"./lib.browser.js"` directly instead.

To fix this, you can rename `"./lib.server.js"` to `"./lib.worker.js"` for example so it has its own specific file. Or check out the [USE_EXPORTS_OR_IMPORTS_BROWSER](#use_exports_or_imports_browser) rule to refactor away the `"browser"` field.

## `DEPRECATED_FIELD_JSNEXT`

The `"jsnext:main"` and `"jsnext"` fields are deprecated. The `"module"` field should be used instead. See [this issue](https://github.com/jsforum/jsforum/issues/5) for more information.
3 changes: 3 additions & 0 deletions site/src/utils/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ function messageToString(m, pkg) {
case 'EXPORTS_VALUE_CONFLICTS_WITH_BROWSER':
// prettier-ignore
return `${bold(pv(m.path))} matches ${bold(fp(m.args.browserPath))}: "${bold(pv(m.args.browserPath))}", which overrides the path when building the library with the "${bold(m.args.browserishCondition)}" condition. This is usually unintentional and may cause build issues. Consider using a different file name for ${bold(pv(m.path))}.`
case 'DEPRECATED_FIELD_JSNEXT':
// prettier-ignore
return `${bold(fp(m.path))} is deprecated. ${bold('pkg.module')} should be used instead.`
default:
return
}
Expand Down

0 comments on commit 2e485af

Please sign in to comment.