Skip to content

Commit a853d19

Browse files
sxzzsxzzbot
andauthored
fix(attw): apply profiles to file-based problems (#1108)
Co-authored-by: Kevin Bot <gh-bot@sxzz.moe>
1 parent 53638e8 commit a853d19

5 files changed

Lines changed: 181 additions & 29 deletions

File tree

‎docs/options/lint.md‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ import { defineConfig } from 'tsdown'
7878
export default defineConfig({
7979
attw: {
8080
// Resolution profile:
81-
// 'strict' - requires all resolutions (default)
81+
// 'strict' - requires all resolutions
8282
// 'node16' - ignores node10 resolution failures
83-
// 'esm-only' - ignores CJS resolution failures
83+
// 'esm-only' - ignores node10 and CJS resolution failures (default)
8484
profile: 'node16',
8585

8686
// Level: 'warn' (default) or 'error' (fails the build)
@@ -94,11 +94,11 @@ export default defineConfig({
9494

9595
### Profiles
9696

97-
| Profile | Description |
98-
| ---------- | ----------------------------------------------------- |
99-
| `strict` | Requires all resolutions to pass (default) |
100-
| `node16` | Ignores `node10` resolution failures |
101-
| `esm-only` | Ignores `node10` and `node16-cjs` resolution failures |
97+
| Profile | Description |
98+
| ---------- | --------------------------------------------------------------- |
99+
| `strict` | Requires all resolutions to pass |
100+
| `node16` | Ignores `node10` resolution failures |
101+
| `esm-only` | Ignores `node10` and `node16-cjs` resolution failures (default) |
102102

103103
### Ignore Rules
104104

‎docs/zh-CN/options/lint.md‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ import { defineConfig } from 'tsdown'
7878
export default defineConfig({
7979
attw: {
8080
// 解析配置:
81-
// 'strict' - 要求所有解析方式通过(默认)
81+
// 'strict' - 要求所有解析方式通过
8282
// 'node16' - 忽略 node10 解析失败
83-
// 'esm-only' - 忽略 CJS 解析失败
83+
// 'esm-only' - 忽略 node10 和 CJS 解析失败(默认)
8484
profile: 'node16',
8585

8686
// 级别:'warn'(默认)或 'error'(导致构建失败)
@@ -94,11 +94,11 @@ export default defineConfig({
9494

9595
### 配置(Profiles)
9696

97-
| 配置 | 说明 |
98-
| ---------- | -------------------------------------- |
99-
| `strict` | 要求所有解析方式通过(默认) |
100-
| `node16` | 忽略 `node10` 解析失败 |
101-
| `esm-only` | 忽略 `node10` 和 `node16-cjs` 解析失败 |
97+
| 配置 | 说明 |
98+
| ---------- | ---------------------------------------------- |
99+
| `strict` | 要求所有解析方式通过 |
100+
| `node16` | 忽略 `node10` 解析失败 |
101+
| `esm-only` | 忽略 `node10` 和 `node16-cjs` 解析失败(默认) |
102102

103103
### 忽略规则
104104

‎schema.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5122,8 +5122,8 @@
51225122
{ "const": "node16" },
51235123
{ "const": "esm-only" }
51245124
],
5125-
"description": "Profiles select a set of resolution modes to require/ignore. All are evaluated but failures outside\nof those required are ignored.\n\nThe available profiles are:\n- `strict`: requires all resolutions\n- `node16`: ignores node10 resolution failures\n- `esm-only`: ignores CJS resolution failures",
5126-
"default": "strict"
5125+
"description": "Profiles select a set of resolution modes to require/ignore. All are evaluated but failures outside\nof those required are ignored.\n\nThe available profiles are:\n- `strict`: requires all resolutions\n- `node16`: ignores node10 resolution failures\n- `esm-only`: ignores node10 and CJS resolution failures",
5126+
"default": "esm-only"
51275127
},
51285128
"level": {
51295129
"anyOf": [{ "const": "warn" }, { "const": "error" }],

‎src/features/pkg/attw.test.ts‎

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
import { Buffer } from 'node:buffer'
2+
import { describe, expect, test, vi } from 'vitest'
3+
import { attw, type AttwOptions } from './attw.ts'
4+
import type { ResolvedConfig } from '../../config/types.ts'
5+
import type {
6+
CheckResult,
7+
Problem,
8+
ResolutionKind,
9+
} from '@arethetypeswrong/core'
10+
11+
const resolutionKinds: ResolutionKind[] = [
12+
'node10',
13+
'node16-cjs',
14+
'node16-esm',
15+
'bundler',
16+
]
17+
18+
function createCheckResult(
19+
problems: [Problem, ResolutionKind[]][],
20+
): CheckResult {
21+
return {
22+
packageName: 'pkg',
23+
packageVersion: '1.0.0',
24+
buildTools: {},
25+
types: { kind: 'included' },
26+
programInfo: {} as any,
27+
problems: problems.map(([problem]) => problem),
28+
entrypoints: {
29+
'.': {
30+
subpath: '.',
31+
hasTypes: true,
32+
isWildcard: false,
33+
resolutions: Object.fromEntries(
34+
resolutionKinds.map((resolutionKind) => [
35+
resolutionKind,
36+
{
37+
name: '.',
38+
resolutionKind,
39+
visibleProblems: problems.flatMap(([, kinds], index) =>
40+
kinds.includes(resolutionKind) ? [index] : [],
41+
),
42+
},
43+
]),
44+
) as any,
45+
},
46+
},
47+
}
48+
}
49+
50+
async function runAttw(
51+
checkResult: CheckResult,
52+
attwOptions: Omit<AttwOptions, 'module'>,
53+
) {
54+
const logger = { warn: vi.fn(), error: vi.fn(), success: vi.fn() }
55+
const options = {
56+
pkg: {},
57+
nameLabel: undefined,
58+
logger,
59+
attw: {
60+
level: 'error',
61+
...attwOptions,
62+
module: {
63+
createPackageFromTarballData: () => ({}),
64+
checkPackage: () => Promise.resolve(checkResult),
65+
},
66+
},
67+
} as unknown as ResolvedConfig
68+
await attw(options, Buffer.from('') as Buffer<ArrayBuffer>)
69+
return logger
70+
}
71+
72+
const internalResolutionError: Problem = {
73+
kind: 'InternalResolutionError',
74+
resolutionOption: 'node10',
75+
fileName: '/node_modules/pkg/client.d.ts',
76+
pos: 0,
77+
end: 0,
78+
moduleSpecifier: 'pkg',
79+
resolutionMode: undefined,
80+
trace: [],
81+
}
82+
83+
const cjsResolvesToEsm: Problem = {
84+
kind: 'CJSResolvesToESM',
85+
entrypoint: '.',
86+
resolutionKind: 'node16-cjs',
87+
}
88+
89+
describe('attw profile', () => {
90+
test('ignores file-based problems only visible from ignored resolutions', async () => {
91+
const checkResult = createCheckResult([
92+
[internalResolutionError, ['node10']],
93+
[cjsResolvesToEsm, ['node16-cjs']],
94+
])
95+
96+
const logger = await runAttw(checkResult, { profile: 'esm-only' })
97+
expect(logger.error).not.toHaveBeenCalled()
98+
expect(logger.success).toHaveBeenCalled()
99+
})
100+
101+
test('keeps file-based problems visible from required resolutions', async () => {
102+
const checkResult = createCheckResult([
103+
[internalResolutionError, ['node10', 'bundler']],
104+
])
105+
106+
const logger = await runAttw(checkResult, { profile: 'esm-only' })
107+
expect(logger.error).toHaveBeenCalledOnce()
108+
expect(logger.error.mock.calls[0].join(' ')).toContain(
109+
'Internal resolution error',
110+
)
111+
})
112+
113+
test('strict reports every problem', async () => {
114+
const checkResult = createCheckResult([
115+
[internalResolutionError, ['node10']],
116+
[cjsResolvesToEsm, ['node16-cjs']],
117+
])
118+
119+
const logger = await runAttw(checkResult, { profile: 'strict' })
120+
const message = logger.error.mock.calls[0].join(' ')
121+
expect(message).toContain('Internal resolution error')
122+
expect(message).toContain('CJS resolves to ESM')
123+
})
124+
125+
test('node16 only ignores node10', async () => {
126+
const checkResult = createCheckResult([
127+
[internalResolutionError, ['node10']],
128+
[cjsResolvesToEsm, ['node16-cjs']],
129+
])
130+
131+
const logger = await runAttw(checkResult, { profile: 'node16' })
132+
const message = logger.error.mock.calls[0].join(' ')
133+
expect(message).not.toContain('Internal resolution error')
134+
expect(message).toContain('CJS resolves to ESM')
135+
})
136+
137+
test('ignoreRules still applies', async () => {
138+
const checkResult = createCheckResult([[cjsResolvesToEsm, ['node16-cjs']]])
139+
140+
const logger = await runAttw(checkResult, {
141+
profile: 'strict',
142+
ignoreRules: ['cjs-resolves-to-esm'],
143+
})
144+
expect(logger.error).not.toHaveBeenCalled()
145+
})
146+
})

‎src/features/pkg/attw.ts‎

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ export interface AttwOptions extends CheckPackageOptions {
3737
* The available profiles are:
3838
* - `strict`: requires all resolutions
3939
* - `node16`: ignores node10 resolution failures
40-
* - `esm-only`: ignores CJS resolution failures
40+
* - `esm-only`: ignores node10 and CJS resolution failures
4141
*
42-
* @default 'strict'
42+
* @default 'esm-only'
4343
*/
4444
profile?: 'strict' | 'node16' | 'esm-only'
4545
/**
@@ -147,19 +147,25 @@ export async function attw(
147147

148148
let errorMessage: string | undefined
149149
if (checkResult.types) {
150-
const problems = checkResult.problems.filter((problem) => {
151-
// Exclude ignored problem kinds
152-
if (ignoreRules.includes(problemFlags[problem.kind])) {
153-
return false
150+
// File-based problems have no `resolutionKind`; only `visibleProblems` ties them to a resolution.
151+
const ignoredResolutions = profiles[profile] ?? []
152+
const visibleProblems = new Set<number>()
153+
for (const entrypoint of Object.values(checkResult.entrypoints)) {
154+
for (const [resolutionKind, resolution] of Object.entries(
155+
entrypoint.resolutions,
156+
)) {
157+
if (ignoredResolutions.includes(resolutionKind)) continue
158+
resolution.visibleProblems?.forEach((index) =>
159+
visibleProblems.add(index),
160+
)
154161
}
162+
}
155163

156-
// Only apply profile filter to problems that have resolutionKind
157-
if ('resolutionKind' in problem) {
158-
return !profiles[profile]?.includes(problem.resolutionKind)
159-
}
160-
// Include all other problem types
161-
return true
162-
})
164+
const problems = checkResult.problems.filter(
165+
(problem, index) =>
166+
!ignoreRules.includes(problemFlags[problem.kind]) &&
167+
visibleProblems.has(index),
168+
)
163169

164170
if (problems.length) {
165171
const problemList = problems

0 commit comments

Comments
 (0)