Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/analyze/publint.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
import {publint} from 'publint';
import {formatMessage} from 'publint/utils';
import type {Message as PublintMessage} from 'publint';
import type {ReportPluginResult, AnalysisContext} from '../types.js';

type FormatMessage = Extract<
PublintMessage,
{code: 'FILE_INVALID_FORMAT' | 'FILE_INVALID_EXPLICIT_FORMAT'}
>;

function groupFormatMessages(messages: FormatMessage[]): string {
const [first] = messages;
const count = messages.length;
const {actualFormat, expectFormat, actualExtension, expectExtension} =
first.args;

if (first.code === 'FILE_INVALID_EXPLICIT_FORMAT') {
return `${count} files end with the ${actualExtension} extension, but the code is written in ${actualFormat}. Consider using the ${expectExtension} extension.`;
}

return `${count} files are written in ${actualFormat}, but are interpreted as ${expectFormat}. Consider using the ${expectExtension} extension.`;
}

export async function runPublint(
context: AnalysisContext
): Promise<ReportPluginResult> {
Expand All @@ -11,14 +30,46 @@ export async function runPublint(

try {
const publintResult = await publint({pack: 'auto', pkgDir: context.root});

const groups = new Map<string, FormatMessage[]>();
for (const problem of publintResult.messages) {
if (
problem.code === 'FILE_INVALID_FORMAT' ||
problem.code === 'FILE_INVALID_EXPLICIT_FORMAT'
) {
const {actualFormat, expectFormat, expectExtension} = problem.args;
const key = `${problem.code}:${actualFormat}:${expectFormat}:${expectExtension}`;
const group = groups.get(key);
if (group) {
group.push(problem);
} else {
groups.set(key, [problem]);
}
continue;
}

result.messages.push({
severity: problem.type,
score: 0,
file: 'package.json',
message: formatMessage(problem, publintResult.pkg) ?? ''
});
}

for (const group of groups.values()) {
const [first] = group;
const message =
group.length === 1
? (formatMessage(first, publintResult.pkg) ?? '')
: groupFormatMessages(group);

result.messages.push({
severity: first.type,
score: 0,
file: 'package.json',
message
});
}
} catch (error) {
console.error(`Failed to run publint: ${error}`);
}
Expand Down
53 changes: 53 additions & 0 deletions src/test/analyze/publint.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {describe, it, expect} from 'vitest';
import path from 'node:path';
import {runPublint} from '../../analyze/publint.js';
import {LocalFileSystem} from '../../local-file-system.js';
import type {AnalysisContext} from '../../types.js';

function makeContext(root: string): AnalysisContext {
return {
fs: new LocalFileSystem(root),
root,
messages: [],
stats: {
name: 'test-package',
version: '1.0.0',
dependencyCount: {production: 0, development: 0},
extraStats: []
},
lockfile: {
type: 'npm',
packages: [],
root: {
name: 'test-package',
version: '1.0.0',
dependencies: [],
devDependencies: [],
optionalDependencies: [],
peerDependencies: []
}
},
packageFile: {
name: 'test-package',
version: '1.0.0'
}
};
}

describe('runPublint', () => {
it('groups per-file format warnings into a single message', async () => {
const fixture = path.join(__dirname, '../../../test/fixtures/lazy-esm-cjs');

const result = await runPublint(makeContext(fixture));

const formatMessages = result.messages.filter((m) =>
m.message.includes('are interpreted as')
);

expect(formatMessages).toHaveLength(1);
expect(formatMessages[0]?.severity).toBe('warning');
expect(formatMessages[0]?.message).toBe(
'4 files are written in ESM, but are interpreted as CJS. Consider using the .mjs extension.'
);
});
});
1 change: 1 addition & 0 deletions test/fixtures/lazy-esm-cjs/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const a = 1;
1 change: 1 addition & 0 deletions test/fixtures/lazy-esm-cjs/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const b = 2;
1 change: 1 addition & 0 deletions test/fixtures/lazy-esm-cjs/c.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const c = 3;
6 changes: 6 additions & 0 deletions test/fixtures/lazy-esm-cjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "lazy-esm-cjs",
"version": "1.0.0",
"type": "commonjs",
"main": "./a.js"
}
Loading