Skip to content

Commit 30716e9

Browse files
authored
Fixes incorrect absolute path resolving in the plugin (#37)
* Fixes incorrect absolute path resolving in the plugin * Reverts changes in the sample project * Removes unnecessary logs * Bumps up the version and updates the changelog
1 parent cb07327 commit 30716e9

File tree

12 files changed

+84
-18
lines changed

12 files changed

+84
-18
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.0.1] - 2022-07-28
9+
10+
### Fixed
11+
12+
- Incorrect relative path resolving
13+
814
## [2.0.0] - 2021-29-11
915

1016
### Changed

e2e/fixtures/path-config/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
{
1313
"name": "../../dist/plugin",
1414
"paths": [
15-
"./lib"
15+
"./included"
1616
]
1717
}
1818
]

e2e/plugin/singleFile.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,17 @@ describe('single file diagnostics', () => {
4545
// then
4646
expect(diagnostics).toHaveLength(1);
4747
});
48+
49+
it('should enable strict mode with a relative path config', async () => {
50+
// given
51+
const { projectPath, filePaths } = fixtureWithPathConfig;
52+
53+
// when
54+
const diagnosticsIncluded = await getDiagnostics(projectPath, filePaths.included);
55+
const diagnosticsExcluded = await getDiagnostics(projectPath, filePaths.excluded);
56+
57+
// then
58+
expect(diagnosticsIncluded).toHaveLength(1);
59+
expect(diagnosticsExcluded).toHaveLength(0);
60+
});
4861
});

e2e/tsc-strict/tsc-strict.spec.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import execa from 'execa';
22
import { join } from 'path';
3-
import { fixtureWithDefaultConfig } from '../fixtures/paths';
3+
import { fixtureWithDefaultConfig, fixtureWithPathConfig } from '../fixtures/paths';
44

55
const runInPath = async (path: string, args: string[] = []): Promise<string> => {
66
const cwd = process.cwd();
@@ -35,3 +35,16 @@ it('should detect strict file errors', async () => {
3535
expect(stdout).toMatch(/Found 1 strict file/i);
3636
expect(stdout).toMatch(/Found 1 error/i);
3737
});
38+
39+
it('should enable strict mode with a relative path config', async () => {
40+
//given
41+
const { projectPath, filePaths } = fixtureWithPathConfig;
42+
43+
// when
44+
const stdout = await runInPath(projectPath);
45+
46+
// then
47+
expect(stdout).not.toEqual(expect.stringContaining(filePaths.excluded));
48+
expect(stdout).toEqual(expect.stringContaining(filePaths.included));
49+
expect(stdout).toMatch(/error TS2322: Type 'null' is not assignable to type 'string'\./i);
50+
});

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.0.0",
3+
"version": "2.0.1",
44
"description": "Typescript tools that help with migration to the strict mode",
55
"author": "Allegro",
66
"contributors": [

src/cli/update-strict-comments/getFilePaths.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ export const getFilePathsOnPathWithoutErrors = (
1818
) =>
1919
allFilePaths.filter(
2020
(filePath) =>
21-
isFileStrictByPath(filePath, configPaths) && !filePathsWithErrors.includes(filePath),
21+
isFileStrictByPath({ filePath, configPaths }) && !filePathsWithErrors.includes(filePath),
2222
);

src/cli/update-strict-comments/updateStrictComments.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ export async function updateStrictComments(
4747
return { updatedFileCount };
4848
}
4949

50-
function shouldInsertIgnoreComment(filePath: string, strictPaths?: string[]): boolean {
51-
return isFileStrictByPath(filePath, strictPaths) && !isIgnoreCommentPresent(filePath);
50+
function shouldInsertIgnoreComment(filePath: string, configPaths?: string[]): boolean {
51+
return isFileStrictByPath({ filePath, configPaths }) && !isIgnoreCommentPresent(filePath);
5252
}
5353

54-
function shouldRemoveStrictComment(filePath: string, strictPaths?: string[]): boolean {
55-
return isFileStrictByPath(filePath, strictPaths) && isStrictCommentPresent(filePath);
54+
function shouldRemoveStrictComment(filePath: string, configPaths?: string[]): boolean {
55+
return isFileStrictByPath({ filePath, configPaths }) && isStrictCommentPresent(filePath);
5656
}

src/common/__tests__/isFileStrict.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe('isFileStrict', () => {
1515
beforeEach(() => {
1616
jest.clearAllMocks();
1717

18-
isFileOnPathMock.mockImplementation((_, path) => path === filePath);
18+
isFileOnPathMock.mockImplementation(({ targetPath }) => targetPath === filePath);
1919
isCommentPresent.mockReturnValue(false);
2020
});
2121

src/common/isFileOnPath.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
import { getPosixFilePath } from './utils';
22
import { getAbsolutePath } from './getAbsolutePath';
33

4-
export function isFileOnPath(currentFilePath: string, pathToStrictFiles: string): boolean {
5-
const absolutePathToStrictFiles = getAbsolutePath(process.cwd(), pathToStrictFiles);
4+
interface IsFileOnPathParams {
5+
filePath: string;
6+
targetPath: string;
7+
projectPath?: string;
8+
}
9+
10+
export function isFileOnPath({
11+
filePath,
12+
targetPath,
13+
projectPath = process.cwd(),
14+
}: IsFileOnPathParams): boolean {
15+
const absolutePathToStrictFiles = getAbsolutePath(projectPath, targetPath);
616

7-
return getPosixFilePath(currentFilePath).startsWith(getPosixFilePath(absolutePathToStrictFiles));
17+
return getPosixFilePath(filePath).startsWith(getPosixFilePath(absolutePathToStrictFiles));
818
}

src/common/isFileStrict.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@ import { TS_STRICT_COMMENT, TS_STRICT_IGNORE_COMMENT } from './constants';
44

55
type IsFileStrictConfig = {
66
filePath: string;
7+
projectPath?: string;
78
config?: Config;
89
isCommentPresent: (comment: string, filePath: string) => boolean;
910
};
1011

1112
// Common logic determining whether file is strict or not
12-
export function isFileStrict({ filePath, config, isCommentPresent }: IsFileStrictConfig): boolean {
13+
export function isFileStrict({
14+
filePath,
15+
config,
16+
projectPath,
17+
isCommentPresent,
18+
}: IsFileStrictConfig): boolean {
1319
if (isCommentPresent(TS_STRICT_IGNORE_COMMENT, filePath)) {
1420
return false;
1521
}
@@ -18,9 +24,10 @@ export function isFileStrict({ filePath, config, isCommentPresent }: IsFileStric
1824
return true;
1925
}
2026

21-
const strictPaths = config?.paths ?? [];
27+
const configPaths = config?.paths ?? [];
28+
const fileStrictByPath = isFileStrictByPath({ filePath, configPaths, projectPath });
2229

23-
if (strictPaths.length > 0 && !isFileStrictByPath(filePath, strictPaths)) {
30+
if (configPaths.length > 0 && !fileStrictByPath) {
2431
return false;
2532
}
2633

0 commit comments

Comments
 (0)