diff --git a/.yarn/versions/2743beb8.yml b/.yarn/versions/2743beb8.yml new file mode 100644 index 0000000..75deb38 --- /dev/null +++ b/.yarn/versions/2743beb8.yml @@ -0,0 +1,2 @@ +releases: + ts-overrides-plugin: minor diff --git a/packages/example/src/ignored/getDate.ts b/packages/example/src/ignored/getDate.ts new file mode 100644 index 0000000..408af1c --- /dev/null +++ b/packages/example/src/ignored/getDate.ts @@ -0,0 +1 @@ +export const ignoredVar : string | null = 42; diff --git a/packages/example/tsconfig.json b/packages/example/tsconfig.json index 6125b48..7ab9bfc 100644 --- a/packages/example/tsconfig.json +++ b/packages/example/tsconfig.json @@ -11,6 +11,7 @@ "name": "ts-overrides-plugin", "transform": "ts-overrides-plugin", "transformProgram": true, + "ignores": ["src/ignored/**/*.{ts,tsx}"], "overrides": [ { "files": ["src/modern/**/*.{ts,tsx}"], diff --git a/packages/plugin/README.md b/packages/plugin/README.md index e11bcd2..b2fc728 100644 --- a/packages/plugin/README.md +++ b/packages/plugin/README.md @@ -1,10 +1,10 @@ # ts-overrides-plugin -A plugin for `TypeScript` that allows overriding `tsconfig` for specific files +A plugin for `TypeScript` that allows overriding `tsconfig` and ignoring for specific files -[![typedoc-theme-hierarchy (latest)](https://img.shields.io/npm/v/ts-overrides-plugin)](https://www.npmjs.com/package/ts-overrides-plugin) -[![typedoc-theme-hierarchy (downloads)](https://img.shields.io/npm/dw/ts-overrides-plugin)](https://www.npmjs.com/package/ts-overrides-plugin) -[![typedoc-theme-hierarchy (stars)](https://img.shields.io/github/stars/difuks/ts-overrides-plugin?style=social)](https://github.com/DiFuks/ts-overrides-plugin) +[![ts-overrides-plugin (latest)](https://img.shields.io/npm/v/ts-overrides-plugin)](https://www.npmjs.com/package/ts-overrides-plugin) +[![ts-overrides-plugin (downloads)](https://img.shields.io/npm/dw/ts-overrides-plugin)](https://www.npmjs.com/package/ts-overrides-plugin) +[![ts-overrides-plugin (stars)](https://img.shields.io/github/stars/difuks/ts-overrides-plugin?style=social)](https://github.com/DiFuks/ts-overrides-plugin) ## Why is it needed? @@ -18,6 +18,7 @@ any other cases where you need to override the `tsconfig` settings for specific - Override diagnostics for files in the `IDE` - Override type hints when hovering over variables in the `IDE` - Override diagnostics for files in `webpack`, `tsc`, and other builders that use `ts-patch` +- Ignore files from type checking in the `IDE` and builders ## Known issues @@ -48,6 +49,7 @@ In the `tsconfig.json` file, add: "plugins": [ { "name": "ts-overrides-plugin", + "ignores": ["src/ignored/**/*.{ts,tsx}"], // Path to files (glob) that should be ignored from type checking. Should not start with './' "overrides": [ { "files": ["src/modern/**/*.{ts,tsx}"], // Path to files (glob) for which settings need to be overridden. Should not start with './' @@ -92,6 +94,7 @@ In the `tsconfig.json` file, add: "name": "ts-overrides-plugin", "transform": "ts-overrides-plugin", "transformProgram": true, + "ignores": ["src/ignored/**/*.{ts,tsx}"], // Path to files (glob) that should be ignored from type checking. Should not start with './' "overrides": [ { "files": ["src/modern/**/*.{ts,tsx}"], // Path to files (glob) for which settings need to be overridden. Should not start with './' diff --git a/packages/plugin/src/cli/index.ts b/packages/plugin/src/cli/index.ts index 2a3539d..cd99608 100644 --- a/packages/plugin/src/cli/index.ts +++ b/packages/plugin/src/cli/index.ts @@ -6,7 +6,8 @@ import type ts from 'typescript'; import { type Override } from '../types/Override'; interface CliPluginConfig extends PluginConfig { - overrides: Override[]; + overrides?: Override[]; + ignores?: string[]; } export const getOverridePrograms = ( @@ -41,8 +42,13 @@ export const getDiagnosticForFile = ( target: ts.Program, sourceFile: ts.SourceFile, method: 'getSemanticDiagnostics' | 'getBindAndCheckDiagnostics', + ignoreMatcher: ((fileName: string) => boolean) | null, cancellationToken?: ts.CancellationToken, ): readonly ts.Diagnostic[] => { + if (ignoreMatcher?.(sourceFile.fileName)) { + return []; + } + const { fileName } = sourceFile; const overrideProgramForFile = overridePrograms.find(overrideProgram => @@ -57,19 +63,28 @@ export const getDiagnosticForFile = ( export const getDiagnosticsForProject = ( program: ts.Program, overridePrograms: ts.Program[], + ignoreMatcher: ((fileName: string) => boolean) | null, cancellationToken?: ts.CancellationToken, ): ts.Diagnostic[] => program .getSourceFiles() .flatMap(sourceFile => - getDiagnosticForFile(overridePrograms, program, sourceFile, `getSemanticDiagnostics`, cancellationToken), + getDiagnosticForFile( + overridePrograms, + program, + sourceFile, + `getSemanticDiagnostics`, + ignoreMatcher, + cancellationToken, + ), ); const plugin: ProgramTransformer = (program, host, pluginConfig, extras) => { - const { overrides: overridesFromConfig } = pluginConfig as CliPluginConfig; + const { overrides: overridesFromConfig = [], ignores } = pluginConfig as CliPluginConfig; const { plugins, ...defaultCompilerOptions } = program.getCompilerOptions(); const sortedOverridesFromConfig = [...overridesFromConfig].reverse(); const rootPath = defaultCompilerOptions.project ? path.dirname(defaultCompilerOptions.project) : process.cwd(); + const ignoreMatcher = ignores ? (fileName: string) => outmatch(ignores)(path.relative(rootPath, fileName)) : null; const overridePrograms = getOverridePrograms( rootPath, @@ -90,6 +105,7 @@ const plugin: ProgramTransformer = (program, host, pluginConfig, extras) => { target, sourceFile, `getBindAndCheckDiagnostics`, + ignoreMatcher, cancellationToken, )) as ts.Program['getBindAndCheckDiagnostics']; } @@ -100,7 +116,7 @@ const plugin: ProgramTransformer = (program, host, pluginConfig, extras) => { return ((sourceFile, cancellationToken) => { // for build ForkTsCheckerWebpackPlugin and tspc if (!sourceFile) { - return getDiagnosticsForProject(target, overridePrograms, cancellationToken); + return getDiagnosticsForProject(target, overridePrograms, ignoreMatcher, cancellationToken); } // for ts-loader - watch and build @@ -109,6 +125,7 @@ const plugin: ProgramTransformer = (program, host, pluginConfig, extras) => { target, sourceFile, `getSemanticDiagnostics`, + ignoreMatcher, cancellationToken, ); }) as ts.Program['getSemanticDiagnostics']; diff --git a/packages/plugin/src/ide/index.ts b/packages/plugin/src/ide/index.ts index 8a12156..1f87241 100644 --- a/packages/plugin/src/ide/index.ts +++ b/packages/plugin/src/ide/index.ts @@ -5,7 +5,8 @@ import type ts from 'typescript/lib/tsserverlibrary'; import type { Override } from '../types/Override'; interface IdePluginConfig { - overrides: Override[]; + overrides?: Override[]; + ignores?: string[]; } const getOverrideLanguageServices = ( @@ -59,7 +60,8 @@ const getLanguageServiceForFile = ( const plugin: ts.server.PluginModuleFactory = ({ typescript }) => ({ create: info => { - const { overrides: overridesFromConfig } = info.config as IdePluginConfig; + const { overrides: overridesFromConfig = [], ignores } = info.config as IdePluginConfig; + const ignoresMatcher = ignores ? outmatch(ignores) : null; const docRegistry = typescript.createDocumentRegistry(); @@ -84,6 +86,10 @@ const plugin: ts.server.PluginModuleFactory = ({ typescript }) => ({ if (property === `getSemanticDiagnostics`) { return (fileName => { + if (ignoresMatcher?.(relative(info.project.getCurrentDirectory(), fileName))) { + return []; + } + const overrideForFile = getLanguageServiceForFile(fileName, overrideLanguageServices, target); return overrideForFile.getSemanticDiagnostics(fileName);