Skip to content

Commit 23d132b

Browse files
Merge pull request #3 from allegro/bugfix/saving-strict-to-other-files
saving-strict | Fixes bug when ts strict could spill to other files
2 parents 528e073 + 5ba656f commit 23d132b

File tree

9 files changed

+118
-34
lines changed

9 files changed

+118
-34
lines changed

CHANGELOG.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project
6+
adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
## [1.0.1] - 2021-06-01
9+
10+
### Fixed
11+
12+
- Issue when using // @ts-strict in one file would affect other files without this comment resulting
13+
in strict mode errors
14+
15+
## [1.0.0] - 2021-05-11
16+
17+
### Added
18+
19+
- First stable version of the plugin

e2e/project-fixture/src/otherFileNotOnPath.ts

Whitespace-only changes.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
const assert = require('assert');
2+
const path = require('path');
3+
const it = require('../it');
4+
5+
const fileContent = `
6+
//@ts-strict
7+
interface TestType { bar: string; }
8+
const foo: TestType | undefined = undefined;
9+
const boo = foo.bar;
10+
`;
11+
12+
const otherFileContent = `
13+
interface TestType {
14+
bar: string;
15+
}
16+
17+
const foo1: TestType | undefined = undefined;
18+
19+
const boo1 = foo1.bar;
20+
`;
21+
22+
function findResponse(responses, eventName) {
23+
return responses.filter((response) => response.event === eventName);
24+
}
25+
26+
async function run(server) {
27+
const rootPath = path.resolve(__dirname, '../project-fixture/');
28+
const file = path.resolve(__dirname, '../project-fixture/' + 'src/notOnPath.ts');
29+
const otherFile = path.resolve(__dirname, '../project-fixture/' + 'src/otherFileNotOnPath.ts');
30+
31+
// open two files
32+
server.send({
33+
command: 'updateOpen',
34+
arguments: {
35+
changedFiles: [],
36+
closedFiles: [],
37+
openFiles: [
38+
{
39+
file,
40+
fileContent,
41+
projectRootPath: rootPath,
42+
scriptKindName: 'TS',
43+
},
44+
{
45+
file: otherFile,
46+
fileContent: otherFileContent,
47+
projectRootPath: rootPath,
48+
scriptKindName: 'TS',
49+
},
50+
],
51+
},
52+
});
53+
54+
await server.waitEvent('projectLoadingFinish');
55+
56+
server.send({ command: 'geterr', arguments: { files: [file, otherFile], delay: 0 } });
57+
58+
// and wait for both files diagnostics errors
59+
await server.waitEvent('semanticDiag');
60+
await server.waitEvent('semanticDiag');
61+
62+
return server.close().then(() => {
63+
const semanticDiagEvent = findResponse(server.responses, 'semanticDiag');
64+
const fileEvent = semanticDiagEvent.filter((it) => it.body.file === file)[0];
65+
const otherFileEvent = semanticDiagEvent.filter((it) => it.body.file === otherFile)[0];
66+
67+
assert(!!fileEvent);
68+
assert(!!otherFileEvent);
69+
assert.strictEqual(fileEvent.body.diagnostics.length, 1);
70+
assert.strictEqual(otherFileEvent.body.diagnostics.length, 0);
71+
});
72+
}
73+
74+
module.exports = run;

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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": "1.0.0",
3+
"version": "1.0.1",
44
"description": "Typescript plugin that allows turning on strict mode in specific files or directories.",
55
"author": "Allegro",
66
"contributors": [

sample-project/package-lock.json

Lines changed: 3 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sample-project/src/otherFile.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
interface TestType {
2+
bar: string;
3+
}
4+
5+
const foo1: TestType | undefined = undefined;
6+
7+
const boo2 = foo1.bar;

src/index.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { PluginInfo, setupProxy, turnOffStrictMode, turnOnStrictMode, log } from './utils';
21
import { StrictFileChecker } from './strictFiles';
2+
import { log, PluginInfo, setupProxy, turnOffStrictMode, turnOnStrictMode } from './utils';
33

44
const init: ts.server.PluginModuleFactory = () => {
55
function create(info: PluginInfo) {
@@ -10,7 +10,9 @@ const init: ts.server.PluginModuleFactory = () => {
1010
const strictFile = new StrictFileChecker(info).isFileStrict(fileName);
1111

1212
if (strictFile) {
13-
return getDiagnosticsWithStrictMode(info, fileName);
13+
turnOnStrictMode(info, info.project.getCompilerOptions());
14+
} else {
15+
turnOffStrictMode(info, info.project.getCompilerOptions());
1416
}
1517

1618
return info.languageService.getSemanticDiagnostics(fileName);
@@ -19,16 +21,6 @@ const init: ts.server.PluginModuleFactory = () => {
1921
return proxy;
2022
}
2123

22-
function getDiagnosticsWithStrictMode(info: PluginInfo, fileName: string) {
23-
turnOnStrictMode(info, info.project.getCompilerOptions());
24-
25-
const diagnostics = info.languageService.getSemanticDiagnostics(fileName);
26-
27-
turnOffStrictMode(info, info.project.getCompilerOptions());
28-
29-
return diagnostics;
30-
}
31-
3224
return { create };
3325
};
3426

src/utils.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as ts_module from 'typescript/lib/tsserverlibrary';
21
import { CompilerOptions } from 'typescript';
2+
import * as ts_module from 'typescript/lib/tsserverlibrary';
33

44
export interface Config {
55
paths?: string[];
@@ -8,21 +8,17 @@ export interface Config {
88
export type PluginInfo = ts_module.server.PluginCreateInfo;
99

1010
export function turnOnStrictMode(info: PluginInfo, currentOptions: CompilerOptions): void {
11-
if (!currentOptions.strict) {
12-
info.project.setCompilerOptions({
13-
...currentOptions,
14-
strict: true,
15-
});
16-
}
11+
info.project.setCompilerOptions({
12+
...currentOptions,
13+
strict: true,
14+
});
1715
}
1816

1917
export function turnOffStrictMode(info: PluginInfo, currentOptions: CompilerOptions): void {
20-
if (currentOptions.strict) {
21-
info.project.setCompilerOptions({
22-
...currentOptions,
23-
strict: false,
24-
});
25-
}
18+
info.project.setCompilerOptions({
19+
...currentOptions,
20+
strict: false,
21+
});
2622
}
2723

2824
export function setupProxy(info: PluginInfo) {

0 commit comments

Comments
 (0)