Skip to content
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

ignoring files feature #20

Merged
merged 2 commits into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .yarn/versions/2743beb8.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
releases:
ts-overrides-plugin: minor
1 change: 1 addition & 0 deletions packages/example/src/ignored/getDate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const ignoredVar : string | null = 42;
1 change: 1 addition & 0 deletions packages/example/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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}"],
Expand Down
11 changes: 7 additions & 4 deletions packages/plugin/README.md
Original file line number Diff line number Diff line change
@@ -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?

Expand All @@ -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
Expand Down Expand Up @@ -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 './'
Expand Down Expand Up @@ -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 './'
Expand Down
25 changes: 21 additions & 4 deletions packages/plugin/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down Expand Up @@ -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 =>
Expand All @@ -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,
Expand All @@ -90,6 +105,7 @@ const plugin: ProgramTransformer = (program, host, pluginConfig, extras) => {
target,
sourceFile,
`getBindAndCheckDiagnostics`,
ignoreMatcher,
cancellationToken,
)) as ts.Program['getBindAndCheckDiagnostics'];
}
Expand All @@ -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
Expand All @@ -109,6 +125,7 @@ const plugin: ProgramTransformer = (program, host, pluginConfig, extras) => {
target,
sourceFile,
`getSemanticDiagnostics`,
ignoreMatcher,
cancellationToken,
);
}) as ts.Program['getSemanticDiagnostics'];
Expand Down
10 changes: 8 additions & 2 deletions packages/plugin/src/ide/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down Expand Up @@ -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();

Expand All @@ -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);
Expand Down