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' ;
34import {
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
1730export 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
5775export 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
94116export 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
142166let _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
164194export 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
169201const _TS_CONFIG_MAP = new Map < string , ParsedCommandLine > ( ) ;
170202
171203export 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 ;
0 commit comments