Skip to content

Commit

Permalink
Support strict mode
Browse files Browse the repository at this point in the history
fix #38
  • Loading branch information
bluwy committed May 20, 2023
1 parent 3b24429 commit 4032625
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 20 deletions.
6 changes: 5 additions & 1 deletion pkg/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ const messages = await publint({
* - `warning`: logs only `warning` and `error` messages
* - `error`: logs only `error` messages
*/
level: 'warning'
level: 'warning',
/**
* Report warnings as errors.
*/
strict: true
})

console.log(messages)
Expand Down
4 changes: 4 additions & 0 deletions pkg/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ export interface Options {
* - `error`: logs only `error` messages
*/
level?: 'suggestion' | 'warning' | 'error'
/**
* Report warnings as errors.
*/
strict?: boolean
}

export interface Vfs {
Expand Down
3 changes: 2 additions & 1 deletion pkg/lib/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export function publint(options) {
return _publint({
pkgDir: options.pkgDir ?? '/',
vfs: options.vfs,
level: options.level ?? 'suggestion'
level: options.level ?? 'suggestion',
strict: options?.strict ?? false
})
}
10 changes: 6 additions & 4 deletions pkg/lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ const cli = sade('publint', false)
`Level of messages to log ('suggestion' | 'warning' | 'error')`,
'suggestion'
)
.option('--strict', `Report warnings as errors`, false)

cli
.command('run [dir]', 'Lint a directory (defaults to current directory)', {
default: true
})
.action(async (dir, opts) => {
const pkgDir = dir ? path.resolve(dir) : process.cwd()
const logs = await lintDir(pkgDir, opts.level)
const logs = await lintDir(pkgDir, opts.level, opts.strict)
logs.forEach((l) => console.log(l))
})

Expand Down Expand Up @@ -70,7 +71,7 @@ cli
for (let i = 0; i < deps.length; i++) {
pq.push(async () => {
const depDir = await findDepPath(deps[i], pkgDir)
const logs = depDir ? await lintDir(depDir, opts.level, true) : []
const logs = depDir ? await lintDir(depDir, opts.level, opts.strict, true) : []
// log this lint result
const log = () => {
logs.forEach((l, j) => console.log((j > 0 ? ' ' : '') + l))
Expand Down Expand Up @@ -100,9 +101,10 @@ cli.parse(process.argv)
/**
* @param {string} pkgDir
* @param {import('../index.js').Options['level']} level
* @param {import('../index.js').Options['strict']} strict
* @param {boolean} [compact]
*/
async function lintDir(pkgDir, level, compact = false) {
async function lintDir(pkgDir, level, strict, compact = false) {
/** @type {string[]} */
const logs = []

Expand All @@ -115,7 +117,7 @@ async function lintDir(pkgDir, level, compact = false) {
if (!rootPkgContent) return logs
const rootPkg = JSON.parse(rootPkgContent)
const pkgName = rootPkg.name || path.basename(pkgDir)
const messages = await publint({ pkgDir, level })
const messages = await publint({ pkgDir, level, strict })

if (messages.length) {
const suggestions = messages.filter((v) => v.type === 'suggestion')
Expand Down
1 change: 1 addition & 0 deletions pkg/lib/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export async function publint(options) {
pkgDir,
vfs: options?.vfs ?? createNodeVfs(),
level: options?.level ?? 'suggestion',
strict: options?.strict ?? false,
_packedFiles: packedFiles
})
}
10 changes: 9 additions & 1 deletion pkg/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
* @param {Options} options
* @returns {Promise<import('..').Message[]>}
*/
export async function publint({ pkgDir, vfs, level, _packedFiles }) {
export async function publint({ pkgDir, vfs, level, strict, _packedFiles }) {
/** @type {import('..').Message[]} */
const messages = []
/**
Expand Down Expand Up @@ -294,6 +294,14 @@ export async function publint({ pkgDir, vfs, level, _packedFiles }) {

await promiseQueue.wait()

if (strict) {
for (const message of messages) {
if (message.type === 'warning') {
message.type = 'error'
}
}
}

if (level === 'warning') {
return messages.filter((m) => m.type !== 'suggestion')
} else if (level === 'error') {
Expand Down
67 changes: 54 additions & 13 deletions pkg/tests/playground.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,36 +33,71 @@ testFixture('test-2', [
])

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

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

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

testFixture(
'test-2 (strict: true)',
[
{ code: 'USE_EXPORTS_BROWSER', type: 'suggestion' },
{ code: 'EXPORTS_VALUE_INVALID', type: 'error' },
{ code: 'EXPORTS_MODULE_SHOULD_BE_ESM', type: 'error' },
{ code: 'FILE_INVALID_FORMAT', type: 'error' },
{ code: 'FILE_INVALID_FORMAT', type: 'error' }
],
{ strict: true }
)

testFixture('types', ['TYPES_NOT_EXPORTED'])

/**
*
* @typedef {{
* level?: import('../index').Options['level']
* strict?: import('../index').Options['strict']
* debug?: boolean
* }} TestOptions
*/

/**
* @param {string} name
* @param {import('../index').Message['code'][]} expectCodes
* @param {import('../index').Options['level']} [level]
* @param {import('../index').Message['code'][] | Pick<import('../index').Message, 'code' | 'type'>[]} expectCodes
* @param {TestOptions} [options]
*/
function testFixture(name, expectCodes, level, debug = false) {
function testFixture(name, expectCodes, options) {
test(name, async () => {
const pkgDir = path.resolve(process.cwd(), 'tests/fixtures', name)
const messages = await publint({ pkgDir, level })
const fixtureName = name.replace(/\(.*$/, '').trim()
const pkgDir = path.resolve(process.cwd(), 'tests/fixtures', fixtureName)
const messages = await publint({
pkgDir,
level: options?.level,
strict: options?.strict
})

if (debug) {
if (options?.debug) {
const pkg = JSON.parse(
await fs.readFile(path.join(pkgDir, 'package.json'), 'utf-8')
)
Expand All @@ -72,8 +107,14 @@ function testFixture(name, expectCodes, level, debug = false) {
console.log()
}

const codes = messages.map((v) => v.code)
equal(codes, expectCodes, codes.join(', '))
// you can test an array of obe
if (typeof expectCodes[0] === 'object') {
const codes = messages.map((v) => ({ code: v.code, type: v.type }))
equal(codes, expectCodes, codes.join(', '))
} else {
const codes = messages.map((v) => v.code)
equal(codes, expectCodes, codes.join(', '))
}
})
}

Expand Down

0 comments on commit 4032625

Please sign in to comment.