Skip to content

Commit 4754a71

Browse files
Add exclude to config (#48)
* feat: add exclude option to config * chore: remove console.log * chore: prettier
1 parent d81e7e3 commit 4754a71

File tree

7 files changed

+109
-4
lines changed

7 files changed

+109
-4
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project
66
adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [2.2.0] - 2022-10-17
9+
10+
### Added
11+
12+
- Add feature to exclude files from strict check based on `exclude` config property
13+
814
## [2.1.0] - 2022-10-14
915

1016
### Added

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ comment. To make these files strict too, just remove its' ignore comments.
6060

6161
## Configuration
6262

63-
Plugin takes one extra non-mandatory argument `paths` that is an array of relative or absolute paths
64-
of directories that should be included. To add strict mode to files from ignored paths you can
65-
insert `//@ts-strict` comment.
63+
Plugin takes extra, non-mandatory arguments `paths` and `exlude`. Both of them take an array of
64+
relative or absolute paths that should be included (property `paths`) or excluded (property
65+
`exclude`). To add strict mode to files from ignored paths you can insert `//@ts-strict` comment.
6666

6767
```json
6868
{
@@ -75,6 +75,10 @@ insert `//@ts-strict` comment.
7575
"paths": [
7676
"./src",
7777
"/absolute/path/to/source/"
78+
],
79+
"exclude": [
80+
"./src/tests",
81+
"./src/fileToExclude.ts"
7882
]
7983
}
8084
]

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "typescript-strict-plugin",
3-
"version": "2.1.0",
3+
"version": "2.2.0",
44
"description": "Typescript tools that help with migration to the strict mode",
55
"author": "Allegro",
66
"contributors": [

src/common/__tests__/isFileStrict.spec.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,20 @@ describe('isFileStrict', () => {
4141
expect(result).toBe(true);
4242
});
4343

44+
it('should return true when strict comment is present and file is excluded', () => {
45+
// given
46+
isCommentPresent.mockImplementation((comment) => comment === '@ts-strict');
47+
const config: Config = {
48+
exclude: [filePath],
49+
};
50+
51+
// when
52+
const result = isFileStrict({ filePath, isCommentPresent, config });
53+
54+
// then
55+
expect(result).toBe(true);
56+
});
57+
4458
it('should return false when both strict and ignore update-strict-comments are present', () => {
4559
// given
4660
isCommentPresent.mockImplementation(
@@ -110,6 +124,20 @@ describe('isFileStrict', () => {
110124
expect(result).toBe(false);
111125
});
112126

127+
it('should return false when file is on path and in exclude', () => {
128+
// given
129+
const config: Config = {
130+
paths: ['otherFilePath', filePath, 'otherFilePath'],
131+
exclude: [filePath],
132+
};
133+
134+
// when
135+
const result = isFileStrict({ filePath, isCommentPresent, config });
136+
137+
// then
138+
expect(result).toBe(false);
139+
});
140+
113141
it('should return true when path config is empty', () => {
114142
// given
115143
const config: Config = {
@@ -122,4 +150,32 @@ describe('isFileStrict', () => {
122150
// then
123151
expect(result).toBe(true);
124152
});
153+
154+
it('should return false when path config is empty and file is excluded', () => {
155+
// given
156+
const config: Config = {
157+
paths: [],
158+
exclude: [filePath],
159+
};
160+
161+
// when
162+
const result = isFileStrict({ filePath, isCommentPresent, config });
163+
164+
// then
165+
expect(result).toBe(false);
166+
});
167+
168+
it('should return true when path config is empty and different file is excluded (check for false-positive)', () => {
169+
// given
170+
const config: Config = {
171+
paths: [],
172+
exclude: ['otherFile'],
173+
};
174+
175+
// when
176+
const result = isFileStrict({ filePath, isCommentPresent, config });
177+
178+
// then
179+
expect(result).toBe(true);
180+
});
125181
});

src/common/isFileExcludedByPath.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { isFileOnPath } from './isFileOnPath';
2+
3+
interface IsFileExcludedByPathParams {
4+
filePath: string;
5+
projectPath?: string;
6+
configExclude?: string[];
7+
}
8+
9+
export function isFileExcludedByPath({
10+
filePath,
11+
projectPath,
12+
configExclude,
13+
}: IsFileExcludedByPathParams): boolean {
14+
if (configExclude === undefined) {
15+
return false;
16+
}
17+
18+
return configExclude?.some((path) =>
19+
isFileOnPath({
20+
filePath,
21+
targetPath: path,
22+
projectPath,
23+
}),
24+
);
25+
}

src/common/isFileStrict.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Config } from './types';
22
import { isFileStrictByPath } from './isFileStrictByPath';
33
import { TS_STRICT_COMMENT, TS_STRICT_IGNORE_COMMENT } from './constants';
4+
import { isFileExcludedByPath } from './isFileExcludedByPath';
45

56
type IsFileStrictConfig = {
67
filePath: string;
@@ -24,6 +25,18 @@ export function isFileStrict({
2425
return true;
2526
}
2627

28+
const configExclude = config?.exclude ?? [];
29+
30+
if (
31+
isFileExcludedByPath({
32+
filePath,
33+
configExclude,
34+
projectPath,
35+
})
36+
) {
37+
return false;
38+
}
39+
2740
const configPaths = config?.paths ?? [];
2841
const fileStrictByPath = isFileStrictByPath({ filePath, configPaths, projectPath });
2942

src/common/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export interface Config {
22
paths?: string[];
3+
exclude?: string[];
34
}

0 commit comments

Comments
 (0)