Skip to content

Commit

Permalink
Suppress invalid glob file format if has correct adjacent file
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Aug 20, 2023
1 parent 3dde985 commit 1d24bf1
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 5 deletions.
31 changes: 28 additions & 3 deletions pkg/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
isDtsFile,
getDtsFilePathFormat,
getDtsCodeFormatExtension,
getPkgPathValue
getPkgPathValue,
replaceLast
} from './utils.js'

/**
Expand Down Expand Up @@ -295,6 +296,18 @@ export async function publint({ pkgDir, vfs, level, strict, _packedFiles }) {
if (isSafeEsm) return

const actualExtension = vfs.getExtName(filePath)
const expectExtension = getCodeFormatExtension(actualFormat)

// test if the expected extension and file path already exist. if so, skip warning as
// this invalid format file is probably intentional for other use.
// NOTE: only relax this for globbed files, as they're implicitly exported.
const expectFilePath = replaceLast(
filePath,
actualExtension,
expectExtension
)
if (await vfs.isPathExist(expectFilePath)) return

messages.push({
code: isExplicitExtension(actualExtension)
? 'FILE_INVALID_EXPLICIT_FORMAT'
Expand All @@ -303,7 +316,7 @@ export async function publint({ pkgDir, vfs, level, strict, _packedFiles }) {
actualFormat,
expectFormat,
actualExtension,
expectExtension: getCodeFormatExtension(actualFormat),
expectExtension,
actualFilePath: '/' + vfs.pathRelative(pkgDir, filePath)
},
path: ['name'],
Expand Down Expand Up @@ -474,6 +487,18 @@ export async function publint({ pkgDir, vfs, level, strict, _packedFiles }) {
if (isSafeEsm) return

const actualExtension = vfs.getExtName(filePath)
const expectExtension = getCodeFormatExtension(actualFormat)

// test if the expected extension and file path already exist. if so, skip warning as
// this invalid format file is probably intentional for other use.
// NOTE: only relax this for globbed files, as they're implicitly exported.
const expectFilePath = replaceLast(
filePath,
actualExtension,
expectExtension
)
if (await vfs.isPathExist(expectFilePath)) return

messages.push({
code: isExplicitExtension(actualExtension)
? 'FILE_INVALID_EXPLICIT_FORMAT'
Expand All @@ -482,7 +507,7 @@ export async function publint({ pkgDir, vfs, level, strict, _packedFiles }) {
actualFormat,
expectFormat,
actualExtension,
expectExtension: getCodeFormatExtension(actualFormat),
expectExtension,
actualFilePath: isGlob
? './' + vfs.pathRelative(pkgDir, filePath)
: exportsValue
Expand Down
1 change: 1 addition & 0 deletions pkg/tests/fixtures/glob/dual-extension/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'bar'
1 change: 1 addition & 0 deletions pkg/tests/fixtures/glob/dual-extension/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'bar'
1 change: 1 addition & 0 deletions pkg/tests/fixtures/test-2/types/dual-extension/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'bar'
1 change: 1 addition & 0 deletions pkg/tests/fixtures/test-2/types/dual-extension/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'bar'
6 changes: 4 additions & 2 deletions pkg/tests/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,10 @@ test('stripComments', () => {
test('exportsGlob', async () => {
const r = (s) => path.resolve(process.cwd(), 'tests/fixtures/glob', s)
const v = createNodeVfs()
equal(await exportsGlob(r('./*.js'), v), [r('alpha.js')])
equal(await exportsGlob(r('./*.mjs'), v), [r('bravo.mjs')])
// prettier-ignore
equal(await exportsGlob(r('./*.js'), v), [r('alpha.js'), r('dual-extension/index.js')])
// prettier-ignore
equal(await exportsGlob(r('./*.mjs'), v), [r('bravo.mjs'), r('dual-extension/index.mjs')])
// prettier-ignore
equal(await exportsGlob(r('./*.css'), v), [r('charlie.css'), r('quebec/romeo.css')])
// prettier-ignore
Expand Down

0 comments on commit 1d24bf1

Please sign in to comment.