Skip to content

Commit 58bb4f8

Browse files
committed
wip
1 parent 6f702d7 commit 58bb4f8

File tree

6 files changed

+83
-142
lines changed

6 files changed

+83
-142
lines changed

code-pushup.preset.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { loadTsConfig } from 'bundle-require';
2-
import { a } from 'vitest/dist/suite-UrZdHRff';
31
import type {
42
CategoryConfig,
53
CoreConfig,
@@ -19,17 +17,7 @@ import {
1917
type TypescriptPluginOptions,
2018
typescriptPlugin,
2119
} from './packages/plugin-typescript/src/index.js';
22-
import { AUDITS, GROUPS } from './packages/plugin-typescript/src/lib/constants';
23-
import {
24-
filterGroupsByAuditSlug,
25-
filterGroupsByTsOptions,
26-
getCategorieReferences,
27-
getCategoryReferences,
28-
getCategoryRefsFromGroups,
29-
getCompilerOptionsToDetermineListedAudits,
30-
getFinalAuditSlugs,
31-
loadTargetConfig,
32-
} from './packages/plugin-typescript/src/lib/utils.js';
20+
import { getCategoryRefsFromGroups } from './packages/plugin-typescript/src/lib/utils.js';
3321

3422
export const jsPackagesCategories: CategoryConfig[] = [
3523
{
Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// eslint-disable-next-line unicorn/import-style
21
import { describe, expect } from 'vitest';
32
import { getTsConfigurationFromPath } from './typescript-runner.js';
43

@@ -26,63 +25,3 @@ describe('getTsConfigurationFromPath', () => {
2625
});
2726
});
2827
});
29-
30-
/*
31-
describe('getDiagnostics', () => {
32-
it('should accept valid options', async () => {
33-
await expect(
34-
getDiagnostics({
35-
tsConfigPath:
36-
'packages/plugin-typescript/mocks/fixtures/basic-setup/tsconfig.json',
37-
}),
38-
).resolves.not.toThrow();
39-
});
40-
41-
it('should return diagnostics array', async () => {
42-
const res = await getDiagnostics({
43-
tsConfigPath:
44-
'packages/plugin-typescript/mocks/fixtures/basic-setup/tsconfig.json',
45-
});
46-
expect(res).toHaveLength(4);
47-
expect(res.at(0)?.code).toBe(2307);
48-
});
49-
50-
it('should throw if missing tsconfig path', async () => {
51-
await expect(
52-
getDiagnostics({
53-
tsConfigPath: 'missing-tsconfig.json',
54-
}),
55-
).rejects.toThrow('tsconfig not found at: missing-tsconfig.json');
56-
});
57-
58-
it('should throw if no files matched by the TypeScript configuration', async () => {
59-
await expect(
60-
getDiagnostics({
61-
tsConfigPath:
62-
'packages/plugin-typescript/mocks/fixtures/basic-setup/tsconfig-with-exclude.json',
63-
}),
64-
).rejects.toThrow(
65-
'No files matched by the TypeScript configuration. Check your "include", "exclude" or "files" settings.',
66-
);
67-
});
68-
});
69-
70-
describe('getTsConfiguration', () => {
71-
it('should accept valid TS config file', async () => {
72-
const config = await getTsConfigurationFromPath({
73-
tsConfigPath:
74-
'./packages/plugin-typescript/mocks/fixtures/basic-setup/tsconfig.json',
75-
});
76-
77-
expect({
78-
...config,
79-
// omitting path details for better snapshots
80-
fileNames: config.fileNames.map(fileName => basename(fileName)),
81-
options: {
82-
...config.options,
83-
rootDir: basename(config.options?.rootDir ?? ''),
84-
},
85-
}).toMatchSnapshot();
86-
});
87-
});
88-
*/

packages/plugin-typescript/src/lib/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { z } from 'zod';
22
import { AUDITS, DEFAULT_TS_CONFIG } from './constants.js';
3-
import type { AuditSlug, CompilerOptionName } from './types.js';
3+
import type { AuditSlug } from './types.js';
44

55
const auditSlugs = AUDITS.map(({ slug }) => slug) as [string, ...string[]];
66
export const typescriptPluginConfigSchema = z.object({

packages/plugin-typescript/src/lib/utils.ts

Lines changed: 77 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,34 @@
1-
import {access} from 'node:fs/promises';
2-
import {dirname} from 'node:path';
1+
import { access } from 'node:fs/promises';
2+
// eslint-disable-next-line unicorn/import-style
3+
import { dirname } from 'node:path';
34
import {
45
type CompilerOptions,
5-
parseConfigFileTextToJson,
66
type ParsedCommandLine,
7+
parseConfigFileTextToJson,
78
parseJsonConfigFileContent,
89
sys,
910
} from 'typescript';
10-
import type {CategoryRef, CategoryRef, CategoryRef, Group} from '@code-pushup/models';
11-
import {camelCaseToKebabCase, executeProcess, kebabCaseToCamelCase, readTextFile} from '@code-pushup/utils';
12-
import type {AuditSlug, SemVerString, TypescriptPluginOptions} from './types.js';
13-
import {TS_ERROR_CODES} from "./runner/ts-error-codes.js";
14-
import {DEFAULT_TS_CONFIG, GROUPS, TYPESCRIPT_PLUGIN_SLUG} from "./constants.js";
15-
11+
import type { CategoryRef, Group } from '@code-pushup/models';
12+
import {
13+
camelCaseToKebabCase,
14+
executeProcess,
15+
kebabCaseToCamelCase,
16+
readTextFile,
17+
} from '@code-pushup/utils';
18+
import {
19+
DEFAULT_TS_CONFIG,
20+
GROUPS,
21+
TYPESCRIPT_PLUGIN_SLUG,
22+
} from './constants.js';
23+
import { TS_ERROR_CODES } from './runner/ts-error-codes.js';
24+
import type {
25+
AuditSlug,
26+
SemVerString,
27+
TypescriptPluginOptions,
28+
} from './types.js';
1629

1730
export function filterAuditsBySlug(slugs?: string[]) {
18-
return ({slug}: { slug: string }) => {
31+
return ({ slug }: { slug: string }) => {
1932
if (slugs && slugs.length > 0) {
2033
return slugs.includes(slug);
2134
}
@@ -47,25 +60,32 @@ export function auditSlugToCompilerOption(slug: string): string {
4760
* @param onlyAudits OnlyAudits
4861
* @returns Filtered Audits
4962
*/
50-
export function filterAuditsByTsOptions(compilerOptions: CompilerOptions, onlyAudits?: string[]) {
51-
return ({slug}: { slug: string }) => {
63+
export function filterAuditsByTsOptions(
64+
compilerOptions: CompilerOptions,
65+
onlyAudits?: string[],
66+
) {
67+
return ({ slug }: { slug: string }) => {
5268
const option = compilerOptions[auditSlugToCompilerOption(slug)];
53-
return (option !== false && option !== undefined) && filterAuditsBySlug(onlyAudits);
69+
return (
70+
option !== false && option !== undefined && filterAuditsBySlug(onlyAudits)
71+
);
5472
};
5573
}
5674

5775
export function filterGroupsByAuditSlug(slugs?: string[]) {
58-
return ({refs}: Group) => refs.some(filterAuditsBySlug(slugs));
76+
return ({ refs }: Group) => refs.some(filterAuditsBySlug(slugs));
5977
}
6078

61-
62-
export function getGroups(compilerOptions: CompilerOptions, {onlyAudits}: TypescriptPluginOptions) {
63-
return GROUPS
64-
.map(group => ({
65-
...group,
66-
refs: group.refs.filter(filterAuditsByTsOptions(compilerOptions, onlyAudits))
67-
}))
68-
.filter(group => group.refs.length > 0);
79+
export function getGroups(
80+
compilerOptions: CompilerOptions,
81+
onlyAudits?: string[],
82+
) {
83+
return GROUPS.map(group => ({
84+
...group,
85+
refs: group.refs.filter(
86+
filterAuditsByTsOptions(compilerOptions, onlyAudits),
87+
),
88+
})).filter(group => group.refs.length > 0);
6989
}
7090

7191
/**
@@ -74,16 +94,18 @@ export function getGroups(compilerOptions: CompilerOptions, {onlyAudits}: Typesc
7494
* @param opt TSPluginOptions
7595
* @returns The array of category references
7696
*/
77-
export async function getCategoryRefsFromGroups(opt?: TypescriptPluginOptions): Promise<CategoryRef[]> {
78-
97+
export async function getCategoryRefsFromGroups(
98+
opt?: TypescriptPluginOptions,
99+
): Promise<CategoryRef[]> {
79100
const definitive = await getCompilerOptionsToDetermineListedAudits(opt);
80-
return GROUPS
81-
.map(group => ({
82-
...group,
83-
refs: group.refs.filter(filterAuditsByTsOptions(definitive, opt?.onlyAudits))
84-
}))
101+
return GROUPS.map(group => ({
102+
...group,
103+
refs: group.refs.filter(
104+
filterAuditsByTsOptions(definitive, opt?.onlyAudits),
105+
),
106+
}))
85107
.filter(group => group.refs.length > 0)
86-
.map(({slug}) => ({
108+
.map(({ slug }) => ({
87109
plugin: TYPESCRIPT_PLUGIN_SLUG,
88110
slug,
89111
weight: 1,
@@ -92,7 +114,7 @@ export async function getCategoryRefsFromGroups(opt?: TypescriptPluginOptions):
92114
}
93115

94116
export async function getCurrentTsVersion(): Promise<SemVerString> {
95-
const {stdout} = await executeProcess({
117+
const { stdout } = await executeProcess({
96118
command: 'npx',
97119
args: ['tsc', '--version'],
98120
});
@@ -130,15 +152,17 @@ export function handleCompilerOptionStrict(options: CompilerOptions) {
130152
return options;
131153
}
132154

133-
const strictOptions = Object.fromEntries(Object.keys(TS_ERROR_CODES.strict).map((key) => [key, true])) as CompilerOptions;
155+
const strictOptions = Object.fromEntries(
156+
Object.keys(TS_ERROR_CODES.strict).map(key => [key, true]),
157+
) as CompilerOptions;
134158

135159
return {
136160
...options,
137-
...strictOptions
161+
...strictOptions,
138162
};
139163
}
140164

141-
165+
// eslint-disable-next-line functional/no-let
142166
let _COMPILER_OPTIONS: CompilerOptions;
143167

144168
/**
@@ -147,35 +171,46 @@ let _COMPILER_OPTIONS: CompilerOptions;
147171
* later if existing
148172
* @param options Plugin options
149173
*/
150-
export async function getCompilerOptionsToDetermineListedAudits(options?: TypescriptPluginOptions) {
174+
export async function getCompilerOptionsToDetermineListedAudits(
175+
options?: TypescriptPluginOptions,
176+
) {
151177
if (_COMPILER_OPTIONS) {
152178
return _COMPILER_OPTIONS;
153179
}
154-
const {tsConfigPath = DEFAULT_TS_CONFIG} = options ?? {};
155-
const {compilerOptions: defaultCompilerOptions} = await loadDefaultTsConfig(await getCurrentTsVersion());
180+
const { tsConfigPath = DEFAULT_TS_CONFIG } = options ?? {};
181+
const { compilerOptions: defaultCompilerOptions } = await loadDefaultTsConfig(
182+
await getCurrentTsVersion(),
183+
);
156184
const config = await loadTargetConfig(tsConfigPath);
157-
const definitiveCompilerOptions = handleCompilerOptionStrict({...defaultCompilerOptions, ...config.options});
185+
const definitiveCompilerOptions = handleCompilerOptionStrict({
186+
...defaultCompilerOptions,
187+
...config.options,
188+
});
158189
_COMPILER_OPTIONS = definitiveCompilerOptions;
159190
return _COMPILER_OPTIONS;
160-
161191
}
162192

163193
// used in presets
164194
export async function getFinalAuditSlugs(options: TypescriptPluginOptions) {
165195
const definitive = await getCompilerOptionsToDetermineListedAudits(options);
166-
return Object.keys(definitive).map((key) => camelCaseToKebabCase(key) as AuditSlug);
196+
return Object.keys(definitive).map(
197+
key => camelCaseToKebabCase(key) as AuditSlug,
198+
);
167199
}
168200

169201
const _TS_CONFIG_MAP = new Map<string, ParsedCommandLine>();
170202

171203
export async function loadTargetConfig(tsConfigPath: string) {
172204
if (_TS_CONFIG_MAP.get(tsConfigPath) === undefined) {
173-
const {config} = parseConfigFileTextToJson(tsConfigPath, await readTextFile(tsConfigPath));
205+
const { config } = parseConfigFileTextToJson(
206+
tsConfigPath,
207+
await readTextFile(tsConfigPath),
208+
);
174209

175210
const parsedConfig = parseJsonConfigFileContent(
176211
config,
177212
sys,
178-
dirname(tsConfigPath)
213+
dirname(tsConfigPath),
179214
);
180215

181216
if (parsedConfig.fileNames.length === 0) {
@@ -184,7 +219,6 @@ export async function loadTargetConfig(tsConfigPath: string) {
184219
);
185220
}
186221

187-
188222
_TS_CONFIG_MAP.set(tsConfigPath, parsedConfig);
189223
}
190224
return _TS_CONFIG_MAP.get(tsConfigPath) as ParsedCommandLine;

packages/plugin-typescript/src/lib/utils.unit.test.ts

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import type { CompilerOptions } from 'typescript';
22
import { describe, expect, it } from 'vitest';
33
import type { Audit, Group } from '@code-pushup/models';
4-
import { GROUPS } from './constants';
54
import {
65
filterAuditsBySlug,
76
filterGroupsByAuditSlug,
8-
filterGroupsByTsOptions,
97
handleCompilerOptionStrict,
108
} from './utils.js';
119

@@ -127,20 +125,3 @@ describe('handleCompilerOptionStrict', () => {
127125
expect(result.noImplicitAny).toBe(true);
128126
});
129127
});
130-
131-
describe('filterGroupsByTsOptions', () => {
132-
it('should filter the groups correctly', () => {
133-
expect(
134-
GROUPS.filter(filterGroupsByTsOptions({ strict: false }, [])),
135-
).toStrictEqual(
136-
expect.arrayContaining([
137-
expect.objectContaining({
138-
strict: expect.objectContaining({
139-
slug: 'strict-null-checks',
140-
weight: 1,
141-
}),
142-
}),
143-
]),
144-
);
145-
});
146-
});

packages/plugin-typescript/tools/generate-ts-config.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,13 @@ export async function getRelevantVersions() {
165165
args: ['view', 'typescript', 'versions', '--json'],
166166
});
167167
const allVersions: SemVerString[] = JSON.parse(stdout);
168-
return allVersions.filter(version => {
169-
return (
168+
return allVersions.filter(
169+
version =>
170170
// not ends with a prerelease version like -dev.20190404, -0, -rc
171171
!version.match('-[a-z0-9]') &&
172172
// start from 1.6.2 as before that there was no init
173-
version >= '1.6.2'
174-
);
175-
});
173+
version >= '1.6.2',
174+
);
176175
}
177176

178177
/**

0 commit comments

Comments
 (0)