Skip to content

Commit

Permalink
Fix file existence check for non-js files
Browse files Browse the repository at this point in the history
fix #96
  • Loading branch information
bluwy committed May 18, 2024
1 parent 7ba9aa3 commit 0b0f517
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 16 deletions.
31 changes: 16 additions & 15 deletions pkg/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import {
getDtsFilePathFormat,
getDtsCodeFormatExtension,
getPkgPathValue,
replaceLast
replaceLast,
isRelativePath,
isAbsolutePath
} from './utils.js'

/**
Expand Down Expand Up @@ -511,7 +513,9 @@ export async function publint({ pkgDir, vfs, level, strict, _packedFiles }) {
* @param {string} exportsValue
*/
async function getExportsFiles(exportsValue) {
const exportsPath = vfs.pathJoin(pkgDir, exportsValue)
const exportsPath = isRelativePath(exportsValue)
? vfs.pathJoin(pkgDir, exportsValue)
: exportsValue
const isGlob = exportsValue.includes('*')
return isGlob
? await exportsGlob(exportsPath, vfs, _packedFiles)
Expand Down Expand Up @@ -577,16 +581,6 @@ export async function publint({ pkgDir, vfs, level, strict, _packedFiles }) {
return
}

// types. check file existence only
if (currentPath.includes('types')) {
const pq = createPromiseQueue()
for (const filePath of exportsFiles) {
pq.push(async () => await readFile(filePath, currentPath))
}
await pq.wait()
return
}

// if the exports value matches a key in `pkg.browser` (meaning it'll be remapped
// if in a browser-ish environment), check if this is a browser-ish environment/condition.
// if so, warn about this conflict as it's often unexpected behaviour.
Expand Down Expand Up @@ -618,9 +612,16 @@ export async function publint({ pkgDir, vfs, level, strict, _packedFiles }) {
isGlob ? './' + vfs.pathRelative(pkgDir, filePath) : undefined
)
)
return
// TODO: maybe check .ts in the future
if (!isFilePathLintable(filePath)) continue
continue
// TODO: maybe improve .ts checks in the future
if (!isFilePathLintable(filePath)) {
// if not lintable, simply check file existence. only if it's an absolute path,
// so we avoid linting strings like `std:lib`
if (isAbsolutePath(filePath)) {
pq.push(async () => await readFile(filePath, currentPath))
}
continue
}
pq.push(async () => {
// could fail if in !isGlob
const fileContent = await readFile(filePath, currentPath)
Expand Down
23 changes: 23 additions & 0 deletions pkg/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,29 @@ export function isFileContentLintable(fileContent) {
return !FLOW_COMMENT_RE.test(fileContent)
}

/**
* Whether the `filePath` looks like a relative path, e.g.
* - ./foo
* - ../foo
* - foo/bar
* @param {string} filePath
*/
export function isRelativePath(filePath) {
if (filePath[0] === '.') return true
return filePath[0] !== '/' && !filePath.includes(':')
}

/**
* Whether the `filePath` looks like an absoluate path
* @param {string} filePath
*/
export function isAbsolutePath(filePath) {
return (
filePath[0] === '/' ||
(filePath[1] === ':' && filePath[0].match(/[a-zA-Z]/))
)
}

/**
* @param {string} filePath
* @param {import('../index.d.ts').Vfs} vfs
Expand Down
1 change: 1 addition & 0 deletions pkg/tests/fixtures/missing-files/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"exports": {
".": {
"types": "./missing.d.ts",
"bun": "./missing.ts",
"import": "./missing.js"
},
"./not-published": "./not-published.js"
Expand Down
2 changes: 1 addition & 1 deletion pkg/tests/playground.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ testFixture('invalid-jsx-extensions', [
])

testFixture('missing-files', [
...Array(7).fill('FILE_DOES_NOT_EXIST'),
...Array(8).fill('FILE_DOES_NOT_EXIST'),
'FILE_NOT_PUBLISHED',
'USE_EXPORTS_OR_IMPORTS_BROWSER'
])
Expand Down

0 comments on commit 0b0f517

Please sign in to comment.