From 88ec2664d7093f51273a7a5e3ef68c7d6fbcee61 Mon Sep 17 00:00:00 2001 From: Michal Kalinowski Date: Mon, 17 Oct 2022 17:06:30 +0200 Subject: [PATCH 1/3] feat: add exclude option to config --- CHANGELOG.md | 5 ++ README.md | 8 ++-- package.json | 2 +- src/common/__tests__/isFileStrict.spec.ts | 56 +++++++++++++++++++++++ src/common/isFileExcludedByPath.ts | 26 +++++++++++ src/common/isFileStrict.ts | 13 ++++++ src/common/types.ts | 1 + 7 files changed, 107 insertions(+), 4 deletions(-) create mode 100644 src/common/isFileExcludedByPath.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index f6d5003..537e0a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [2.2.0] - 2022-10-17 + +### Added +- Add feature to exclude files from strict check based on `exclude` config property + ## [2.1.0] - 2022-10-14 ### Added diff --git a/README.md b/README.md index 71034ec..df13763 100644 --- a/README.md +++ b/README.md @@ -60,9 +60,7 @@ comment. To make these files strict too, just remove its' ignore comments. ## Configuration -Plugin takes one extra non-mandatory argument `paths` that is an array of relative or absolute paths -of directories that should be included. To add strict mode to files from ignored paths you can -insert `//@ts-strict` comment. +Plugin takes extra, non-mandatory arguments `paths` and `exlude`. Both of them take an array of relative or absolute paths that should be included (property `paths`) or excluded (property `exclude`). To add strict mode to files from ignored paths you can insert `//@ts-strict` comment. ```json { @@ -75,6 +73,10 @@ insert `//@ts-strict` comment. "paths": [ "./src", "/absolute/path/to/source/" + ], + "exclude": [ + "./src/tests", + "./src/fileToExclude.ts" ] } ] diff --git a/package.json b/package.json index 8cf1c42..b07949a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "typescript-strict-plugin", - "version": "2.1.0", + "version": "2.2.0", "description": "Typescript tools that help with migration to the strict mode", "author": "Allegro", "contributors": [ diff --git a/src/common/__tests__/isFileStrict.spec.ts b/src/common/__tests__/isFileStrict.spec.ts index ac6c4c8..a3b8024 100644 --- a/src/common/__tests__/isFileStrict.spec.ts +++ b/src/common/__tests__/isFileStrict.spec.ts @@ -41,6 +41,20 @@ describe('isFileStrict', () => { expect(result).toBe(true); }); + it('should return true when strict comment is present and file is excluded', () => { + // given + isCommentPresent.mockImplementation((comment) => comment === '@ts-strict'); + const config: Config = { + exclude: [filePath], + }; + + // when + const result = isFileStrict({ filePath, isCommentPresent, config }); + + // then + expect(result).toBe(true); + }); + it('should return false when both strict and ignore update-strict-comments are present', () => { // given isCommentPresent.mockImplementation( @@ -110,6 +124,20 @@ describe('isFileStrict', () => { expect(result).toBe(false); }); + it('should return false when file is on path and in exclude', () => { + // given + const config: Config = { + paths: ['otherFilePath', filePath, 'otherFilePath'], + exclude: [filePath], + }; + + // when + const result = isFileStrict({ filePath, isCommentPresent, config }); + + // then + expect(result).toBe(false); + }); + it('should return true when path config is empty', () => { // given const config: Config = { @@ -122,4 +150,32 @@ describe('isFileStrict', () => { // then expect(result).toBe(true); }); + + it('should return false when path config is empty and file is excluded', () => { + // given + const config: Config = { + paths: [], + exclude: [filePath], + }; + + // when + const result = isFileStrict({ filePath, isCommentPresent, config }); + + // then + expect(result).toBe(false); + }); + + it('should return true when path config is empty and different file is excluded (check for false-positive)', () => { + // given + const config: Config = { + paths: [], + exclude: ['otherFile'], + }; + + // when + const result = isFileStrict({ filePath, isCommentPresent, config }); + + // then + expect(result).toBe(true); + }); }); diff --git a/src/common/isFileExcludedByPath.ts b/src/common/isFileExcludedByPath.ts new file mode 100644 index 0000000..d720d1d --- /dev/null +++ b/src/common/isFileExcludedByPath.ts @@ -0,0 +1,26 @@ +import { isFileOnPath } from './isFileOnPath'; + +interface IsFileExcludedByPathParams { + filePath: string; + projectPath?: string; + configExclude?: string[]; +} + +export function isFileExcludedByPath({ + filePath, + projectPath, + configExclude, +}: IsFileExcludedByPathParams): boolean { + if (configExclude === undefined) { + console.log('No Exludes detected'); + return false; + } + + return configExclude?.some((path) => + isFileOnPath({ + filePath, + targetPath: path, + projectPath, + }), + ); +} diff --git a/src/common/isFileStrict.ts b/src/common/isFileStrict.ts index 9ed08a6..f336cfa 100644 --- a/src/common/isFileStrict.ts +++ b/src/common/isFileStrict.ts @@ -1,6 +1,7 @@ import { Config } from './types'; import { isFileStrictByPath } from './isFileStrictByPath'; import { TS_STRICT_COMMENT, TS_STRICT_IGNORE_COMMENT } from './constants'; +import { isFileExcludedByPath } from './isFileExcludedByPath'; type IsFileStrictConfig = { filePath: string; @@ -24,6 +25,18 @@ export function isFileStrict({ return true; } + const configExclude = config?.exclude ?? []; + + if ( + isFileExcludedByPath({ + filePath, + configExclude, + projectPath, + }) + ) { + return false; + } + const configPaths = config?.paths ?? []; const fileStrictByPath = isFileStrictByPath({ filePath, configPaths, projectPath }); diff --git a/src/common/types.ts b/src/common/types.ts index 08fae91..1b4e16b 100644 --- a/src/common/types.ts +++ b/src/common/types.ts @@ -1,3 +1,4 @@ export interface Config { paths?: string[]; + exclude?: string[]; } From 3a6d5fd092b8c025077d5b1c56e69d6c721496f3 Mon Sep 17 00:00:00 2001 From: Michal Kalinowski Date: Mon, 17 Oct 2022 17:09:36 +0200 Subject: [PATCH 2/3] chore: remove console.log --- src/common/isFileExcludedByPath.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/common/isFileExcludedByPath.ts b/src/common/isFileExcludedByPath.ts index d720d1d..4153288 100644 --- a/src/common/isFileExcludedByPath.ts +++ b/src/common/isFileExcludedByPath.ts @@ -12,7 +12,6 @@ export function isFileExcludedByPath({ configExclude, }: IsFileExcludedByPathParams): boolean { if (configExclude === undefined) { - console.log('No Exludes detected'); return false; } From 9e8547b6c5b1d941a0c9bc3ce1f8c3061c37e272 Mon Sep 17 00:00:00 2001 From: Michal Kalinowski Date: Tue, 18 Oct 2022 15:12:49 +0200 Subject: [PATCH 3/3] chore: prettier --- CHANGELOG.md | 1 + README.md | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 537e0a4..5257dd1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [2.2.0] - 2022-10-17 ### Added + - Add feature to exclude files from strict check based on `exclude` config property ## [2.1.0] - 2022-10-14 diff --git a/README.md b/README.md index df13763..b8c8300 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,9 @@ comment. To make these files strict too, just remove its' ignore comments. ## Configuration -Plugin takes extra, non-mandatory arguments `paths` and `exlude`. Both of them take an array of relative or absolute paths that should be included (property `paths`) or excluded (property `exclude`). To add strict mode to files from ignored paths you can insert `//@ts-strict` comment. +Plugin takes extra, non-mandatory arguments `paths` and `exlude`. Both of them take an array of +relative or absolute paths that should be included (property `paths`) or excluded (property +`exclude`). To add strict mode to files from ignored paths you can insert `//@ts-strict` comment. ```json {