From 0a8b964221c76ec3da8564a435c0c768b7366be1 Mon Sep 17 00:00:00 2001 From: Eric Shirley Date: Thu, 24 Jul 2025 09:32:37 -0400 Subject: [PATCH] Allow passing an env var for the strict mode ignore comment --- README.md | 38 +++++++++++++++++++++++++++++++++----- src/common/constants.ts | 2 +- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 25140fa..e6e3174 100644 --- a/README.md +++ b/README.md @@ -60,11 +60,11 @@ comment. To make these files strict too, just remove its' ignore comments. ## Configuration -Plugin takes extra, non-mandatory arguments `paths`, `exclude` and `excludePattern`. Args `paths` and -`exclude` accept an array of relative or absolute paths that should be included (property `paths`) -or excluded (property `exclude`). Arg `excludePattern` accepts an array of strings that will be -matched with [minimatch](https://github.com/isaacs/minimatch). To add strict mode to files from -ignored paths you can insert `//@ts-strict` comment. +Plugin takes extra, non-mandatory arguments `paths`, `exclude` and `excludePattern`. Args `paths` +and `exclude` accept an array of relative or absolute paths that should be included (property +`paths`) or excluded (property `exclude`). Arg `excludePattern` accepts an array of strings that +will be matched with [minimatch](https://github.com/isaacs/minimatch). To add strict mode to files +from ignored paths you can insert `//@ts-strict` comment. ```json { @@ -121,6 +121,34 @@ yarn tsc-strict --strictNullChecks false would not check for the strict null check in your files. The `tsc-strict` accepts all the arguments that regular `tsc` command accepts. +### Customizing the ignore comment + +You can customize the ignore comment used by the plugin through the `TS_STRICT_IGNORE_COMMENT` +environment variable. This is particularly useful for teams migrating to strict mode who want +different behavior between their development environment and CI pipeline. + +Example: + +```ts +// @ts-strict-cli-ignore +const str: string | null = null; +const len = str.length; // error in IDE, passes cli with proper env var +``` + +in package.json scripts: + +```json +{ + "scripts": { + "typecheck": "tsc && TS_STRICT_IGNORE_COMMENT=@ts-strict-ci-ignore tsc-strict" + } +} +``` + +- Files marked with e.g `@ts-strict-cli-ignore` will be ignored only by the CLI tool in CI +- This allows gradual migration where developers see and fix issues locally without breaking the + build + ## Migrating to v2 Because of difficulties with migrating large projects to strict mode with original `//@ts-strict` diff --git a/src/common/constants.ts b/src/common/constants.ts index 6e0d0fb..fe9d0ac 100644 --- a/src/common/constants.ts +++ b/src/common/constants.ts @@ -1,3 +1,3 @@ export const PLUGIN_NAME = 'typescript-strict-plugin'; export const TS_STRICT_COMMENT = '@ts-strict'; -export const TS_STRICT_IGNORE_COMMENT = '@ts-strict-ignore'; +export const TS_STRICT_IGNORE_COMMENT = process.env.TS_STRICT_IGNORE_COMMENT || '@ts-strict-ignore';