-
Notifications
You must be signed in to change notification settings - Fork 15
feat(plugin-typescript): add plugin logic #936
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a81c8c3
feat(plugin-typescript): add plugin logic
BioPhoton 0888cc5
feat(plugin-typescript): adjust types
BioPhoton fa8fc9d
test(plugin-typescript): refine tests
BioPhoton dd8e85e
test(plugin-typescript): refine types
BioPhoton 1aea4c0
test(plugin-typescript): remove old code
BioPhoton 100fa0d
fix(plugin-typescript): add missing deps
BioPhoton e3dd722
test(plugin-typescript): add test case
BioPhoton f548eea
fix(plugin-typescript): fix lint, format code
BioPhoton b1c7466
Update packages/plugin-typescript/src/lib/schema.ts
BioPhoton 73d4b10
Update packages/plugin-typescript/src/lib/typescript-plugin.ts
BioPhoton fb1bbad
fix(plugin-typescript): refine types
BioPhoton 169df80
fix(plugin-typescript): refine testing types; use ui logger
BioPhoton 1d88b67
fix(plugin-typescript): format
BioPhoton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import { z } from 'zod'; | ||
| import { AUDITS, DEFAULT_TS_CONFIG } from './constants.js'; | ||
|
|
||
| const auditSlugs = AUDITS.map(({ slug }) => slug) as [string, ...string[]]; | ||
| export const typescriptPluginConfigSchema = z.object({ | ||
| tsconfig: z | ||
| .string({ | ||
| description: 'Path to the TsConfig', | ||
| }) | ||
| .default(DEFAULT_TS_CONFIG), | ||
| onlyAudits: z | ||
| .array(z.enum(auditSlugs), { | ||
| description: 'Array with specific TsCodes to measure', | ||
| }) | ||
| .optional(), | ||
| }); | ||
|
|
||
| export type TypescriptPluginOptions = z.infer< | ||
| typeof typescriptPluginConfigSchema | ||
| >; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { | ||
| type TypescriptPluginOptions, | ||
| typescriptPluginConfigSchema, | ||
| } from './schema.js'; | ||
|
|
||
| describe('typescriptPluginConfigSchema', () => { | ||
| const tsConfigPath = 'tsconfig.json'; | ||
|
|
||
| it('accepts a empty configuration', () => { | ||
| expect(() => typescriptPluginConfigSchema.parse({})).not.toThrow(); | ||
| }); | ||
|
|
||
| it('accepts a configuration with tsConfigPath set', () => { | ||
| expect(() => | ||
| typescriptPluginConfigSchema.parse({ | ||
| tsConfigPath, | ||
| } satisfies TypescriptPluginOptions), | ||
| ).not.toThrow(); | ||
| }); | ||
|
|
||
| it('accepts a configuration with tsConfigPath and empty onlyAudits', () => { | ||
| expect(() => | ||
| typescriptPluginConfigSchema.parse({ | ||
| tsConfigPath, | ||
| onlyAudits: [], | ||
| } satisfies TypescriptPluginOptions), | ||
| ).not.toThrow(); | ||
| }); | ||
|
BioPhoton marked this conversation as resolved.
|
||
|
|
||
| it('accepts a configuration with tsConfigPath and full onlyAudits', () => { | ||
| expect(() => | ||
| typescriptPluginConfigSchema.parse({ | ||
| tsConfigPath, | ||
| onlyAudits: [ | ||
| 'syntax-errors', | ||
| 'semantic-errors', | ||
| 'configuration-errors', | ||
| ], | ||
| } satisfies TypescriptPluginOptions), | ||
| ).not.toThrow(); | ||
| }); | ||
|
|
||
| it('throws for invalid onlyAudits', () => { | ||
| expect(() => | ||
| typescriptPluginConfigSchema.parse({ | ||
| onlyAudits: 123, | ||
| }), | ||
| ).toThrow('invalid_type'); | ||
| }); | ||
|
|
||
| it('throws for invalid onlyAudits items', () => { | ||
| expect(() => | ||
| typescriptPluginConfigSchema.parse({ | ||
| tsConfigPath, | ||
| onlyAudits: [123, true], | ||
| }), | ||
| ).toThrow('invalid_type'); | ||
| }); | ||
|
|
||
| it('throws for unknown audit slug', () => { | ||
| expect(() => | ||
| typescriptPluginConfigSchema.parse({ | ||
| tsConfigPath, | ||
| onlyAudits: ['unknown-audit'], | ||
| }), | ||
| ).toThrow(/unknown-audit/); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import { createRequire } from 'node:module'; | ||
| import type { PluginConfig } from '@code-pushup/models'; | ||
| import { stringifyError } from '@code-pushup/utils'; | ||
| import { DEFAULT_TS_CONFIG, TYPESCRIPT_PLUGIN_SLUG } from './constants.js'; | ||
| import { createRunnerFunction } from './runner/runner.js'; | ||
| import type { DiagnosticsOptions } from './runner/ts-runner.js'; | ||
| import { typescriptPluginConfigSchema } from './schema.js'; | ||
| import type { AuditSlug } from './types.js'; | ||
| import { getAudits, getGroups, logSkippedAudits } from './utils.js'; | ||
|
|
||
| const packageJson = createRequire(import.meta.url)( | ||
| '../../package.json', | ||
| ) as typeof import('../../package.json'); | ||
|
|
||
| export type FilterOptions = { onlyAudits?: AuditSlug[] }; | ||
| export type TypescriptPluginOptions = Partial<DiagnosticsOptions> & | ||
| FilterOptions; | ||
|
BioPhoton marked this conversation as resolved.
Outdated
|
||
|
|
||
| export async function typescriptPlugin( | ||
| options?: TypescriptPluginOptions, | ||
| ): Promise<PluginConfig> { | ||
| const { tsconfig = DEFAULT_TS_CONFIG, onlyAudits } = parseOptions( | ||
| options ?? {}, | ||
| ); | ||
|
|
||
| const filteredAudits = getAudits({ onlyAudits }); | ||
| const filteredGroups = getGroups({ onlyAudits }); | ||
|
|
||
| logSkippedAudits(filteredAudits); | ||
|
|
||
| return { | ||
| slug: TYPESCRIPT_PLUGIN_SLUG, | ||
| packageName: packageJson.name, | ||
| version: packageJson.version, | ||
| title: 'Typescript', | ||
| description: 'Official Code PushUp Typescript plugin.', | ||
| docsUrl: 'https://www.npmjs.com/package/@code-pushup/typescript-plugin/', | ||
| icon: 'typescript', | ||
| audits: filteredAudits, | ||
| groups: filteredGroups, | ||
| runner: createRunnerFunction({ | ||
| tsconfig, | ||
| expectedAudits: filteredAudits, | ||
| }), | ||
| }; | ||
| } | ||
|
|
||
| function parseOptions( | ||
| tsPluginOptions: TypescriptPluginOptions, | ||
| ): TypescriptPluginOptions { | ||
| try { | ||
| return typescriptPluginConfigSchema.parse(tsPluginOptions) as FilterOptions; | ||
| } catch (error) { | ||
| throw new Error( | ||
| `Error parsing TypeScript Plugin options: ${stringifyError(error)}`, | ||
| ); | ||
| } | ||
| } | ||
|
BioPhoton marked this conversation as resolved.
|
||
40 changes: 40 additions & 0 deletions
40
packages/plugin-typescript/src/lib/typescript-plugin.unit.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import { expect } from 'vitest'; | ||
| import { pluginConfigSchema } from '@code-pushup/models'; | ||
| import { AUDITS, GROUPS } from './constants.js'; | ||
| import type { TypescriptPluginOptions } from './types.js'; | ||
| import { typescriptPlugin } from './typescript-plugin.js'; | ||
|
|
||
| describe('typescriptPlugin-config-object', () => { | ||
| it('should create valid plugin config without options', async () => { | ||
| const pluginConfig = await typescriptPlugin(); | ||
|
|
||
| expect(() => pluginConfigSchema.parse(pluginConfig)).not.toThrow(); | ||
|
|
||
| const { audits, groups } = pluginConfig; | ||
| expect(audits).toHaveLength(AUDITS.length); | ||
| expect(groups).toBeDefined(); | ||
| expect(groups!).toHaveLength(GROUPS.length); | ||
| }); | ||
|
|
||
| it('should create valid plugin config', async () => { | ||
| const pluginConfig = await typescriptPlugin({ | ||
| tsconfig: 'mocked-away/tsconfig.json', | ||
| onlyAudits: ['syntax-errors', 'semantic-errors', 'configuration-errors'], | ||
| }); | ||
|
|
||
| expect(() => pluginConfigSchema.parse(pluginConfig)).not.toThrow(); | ||
|
|
||
| const { audits, groups } = pluginConfig; | ||
| expect(audits).toHaveLength(3); | ||
| expect(groups).toBeDefined(); | ||
| expect(groups!).toHaveLength(2); | ||
| }); | ||
|
|
||
| it('should throw for invalid valid params', async () => { | ||
| await expect(() => | ||
| typescriptPlugin({ | ||
| tsconfig: 42, | ||
| } as unknown as TypescriptPluginOptions), | ||
| ).rejects.toThrow(/invalid_type/); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.