Skip to content
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ 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
Expand Down
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ 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
{
Expand All @@ -75,6 +75,10 @@ insert `//@ts-strict` comment.
"paths": [
"./src",
"/absolute/path/to/source/"
],
"exclude": [
"./src/tests",
"./src/fileToExclude.ts"
]
}
]
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
56 changes: 56 additions & 0 deletions src/common/__tests__/isFileStrict.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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 = {
Expand All @@ -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);
});
});
25 changes: 25 additions & 0 deletions src/common/isFileExcludedByPath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { isFileOnPath } from './isFileOnPath';

interface IsFileExcludedByPathParams {
filePath: string;
projectPath?: string;
configExclude?: string[];
}

export function isFileExcludedByPath({
Copy link
Contributor Author

@MikeKalinowski MikeKalinowski Oct 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is heavily based on check for 'paths' in isFileStrictByPath.ts, but there is a small difference in guard clause

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know if you want me to merge them into one function

filePath,
projectPath,
configExclude,
}: IsFileExcludedByPathParams): boolean {
if (configExclude === undefined) {
return false;
}

return configExclude?.some((path) =>
isFileOnPath({
filePath,
targetPath: path,
projectPath,
}),
);
}
13 changes: 13 additions & 0 deletions src/common/isFileStrict.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 });

Expand Down
1 change: 1 addition & 0 deletions src/common/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export interface Config {
paths?: string[];
exclude?: string[];
}