Skip to content

Commit fe2a35d

Browse files
radiosternejohnnyreilly
authored andcommitted
Working as expected on first line diagnostic.start (#190)
* fix throwing error on first line diagnostic * Removed deprecated fs.existsSync; package.json and CHANGELOG.md bump
1 parent ca92343 commit fe2a35d

9 files changed

+68
-35
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## v0.5.2
2+
3+
* [Fix erroneous error on diagnostics at 0 line; remove deprecated fs.existsSync](https://github.com/Realytics/fork-ts-checker-webpack-plugin/pull/190) (#190)
4+
15
## v0.5.1
26

37
* [Make the checker compile with TypeScript 3.2](https://github.com/Realytics/fork-ts-checker-webpack-plugin/pull/189)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fork-ts-checker-webpack-plugin",
3-
"version": "0.5.1",
3+
"version": "0.5.2",
44
"description": "Runs typescript type checker and linter on separate process.",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",

src/CancellationToken.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import * as os from 'os';
44
import * as path from 'path';
55
import * as ts from 'typescript';
66

7+
import { FsHelper } from './FsHelper';
8+
79
interface CancellationTokenData {
810
isCancelled: boolean;
911
cancellationFileName: string;
@@ -46,7 +48,7 @@ export class CancellationToken {
4648
if (duration > 10) {
4749
// check no more than once every 10ms
4850
this.lastCancellationCheckTime = time;
49-
this.isCancelled = fs.existsSync(this.getCancellationFilePath());
51+
this.isCancelled = FsHelper.existsSync(this.getCancellationFilePath());
5052
}
5153

5254
return this.isCancelled;
@@ -64,7 +66,10 @@ export class CancellationToken {
6466
}
6567

6668
public cleanupCancellation() {
67-
if (this.isCancelled && fs.existsSync(this.getCancellationFilePath())) {
69+
if (
70+
this.isCancelled &&
71+
FsHelper.existsSync(this.getCancellationFilePath())
72+
) {
6873
fs.unlinkSync(this.getCancellationFilePath());
6974
this.isCancelled = false;
7075
}

src/FsHelper.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import * as fs from 'fs';
2+
3+
export class FsHelper {
4+
public static existsSync(filePath: fs.PathLike) {
5+
try {
6+
fs.statSync(filePath);
7+
} catch (err) {
8+
if (err.code === 'ENOENT') {
9+
return false;
10+
} else {
11+
throw err;
12+
}
13+
}
14+
return true;
15+
}
16+
}

src/IncrementalChecker.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { NormalizedMessage } from './NormalizedMessage';
99
import { CancellationToken } from './CancellationToken';
1010
import * as minimatch from 'minimatch';
1111
import { VueProgram } from './VueProgram';
12+
import { FsHelper } from './FsHelper';
1213

1314
// Need some augmentation here - linterOptions.exclude is not (yet) part of the official
1415
// types for tslint.
@@ -284,7 +285,7 @@ export class IncrementalChecker {
284285
linter.lint(fileName, undefined!, this.linterConfig);
285286
} catch (e) {
286287
if (
287-
fs.existsSync(fileName) &&
288+
FsHelper.existsSync(fileName) &&
288289
// check the error type due to file system lag
289290
!(e instanceof Error) &&
290291
!(e.constructor.name === 'FatalError') &&

src/NormalizedMessage.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class NormalizedMessage {
5151
let character: number | undefined;
5252
if (diagnostic.file) {
5353
file = diagnostic.file.fileName;
54-
if (!diagnostic.start) {
54+
if (diagnostic.start === undefined) {
5555
throw new Error('Expected diagnostics to have start');
5656
}
5757
const position = diagnostic.file.getLineAndCharacterOfPosition(
@@ -92,7 +92,10 @@ export class NormalizedMessage {
9292
return new NormalizedMessage(json);
9393
}
9494

95-
public static compare(messageA: NormalizedMessage, messageB: NormalizedMessage) {
95+
public static compare(
96+
messageA: NormalizedMessage,
97+
messageB: NormalizedMessage
98+
) {
9699
if (!(messageA instanceof NormalizedMessage)) {
97100
return -1;
98101
}
@@ -102,18 +105,12 @@ export class NormalizedMessage {
102105

103106
return (
104107
NormalizedMessage.compareTypes(messageA.type, messageB.type) ||
105-
NormalizedMessage.compareOptionalStrings(
106-
messageA.file,
107-
messageB.file
108-
) ||
108+
NormalizedMessage.compareOptionalStrings(messageA.file, messageB.file) ||
109109
NormalizedMessage.compareSeverities(
110110
messageA.severity,
111111
messageB.severity
112112
) ||
113-
NormalizedMessage.compareNumbers(
114-
messageA.line,
115-
messageB.line
116-
) ||
113+
NormalizedMessage.compareNumbers(messageA.line, messageB.line) ||
117114
NormalizedMessage.compareNumbers(
118115
messageA.character,
119116
messageB.character
@@ -131,7 +128,10 @@ export class NormalizedMessage {
131128
);
132129
}
133130

134-
public static equals(messageA: NormalizedMessage, messageB: NormalizedMessage) {
131+
public static equals(
132+
messageA: NormalizedMessage,
133+
messageB: NormalizedMessage
134+
) {
135135
return this.compare(messageA, messageB) === 0;
136136
}
137137

src/formatter/codeframeFormatter.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import codeFrame = require('babel-code-frame');
33
import chalk from 'chalk';
44
import * as fs from 'fs';
55
import { NormalizedMessage } from '../NormalizedMessage';
6+
import { FsHelper } from '../FsHelper';
67

78
/**
89
* Create new code frame formatter.
@@ -23,9 +24,7 @@ export function createCodeframeFormatter(options: any) {
2324

2425
const file = message.file;
2526
const source =
26-
file &&
27-
fs.existsSync(file) &&
28-
fs.readFileSync(file, 'utf-8');
27+
file && FsHelper.existsSync(file) && fs.readFileSync(file, 'utf-8');
2928
let frame = '';
3029

3130
if (source) {
@@ -41,9 +40,7 @@ export function createCodeframeFormatter(options: any) {
4140
}
4241

4342
return (
44-
messageColor(
45-
message.severity.toUpperCase() + ' in ' + message.file
46-
) +
43+
messageColor(message.severity.toUpperCase() + ' in ' + message.file) +
4744
os.EOL +
4845
positionColor(message.line + ':' + message.character) +
4946
' ' +

src/index.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import * as path from 'path';
22
import * as process from 'process';
33
import * as childProcess from 'child_process';
44
import chalk, { Chalk } from 'chalk';
5-
import * as fs from 'fs';
65
import * as micromatch from 'micromatch';
76
import * as os from 'os';
87
import * as webpack from 'webpack';
98
import { CancellationToken } from './CancellationToken';
109
import { NormalizedMessage } from './NormalizedMessage';
1110
import { createDefaultFormatter } from './formatter/defaultFormatter';
1211
import { createCodeframeFormatter } from './formatter/codeframeFormatter';
12+
import { FsHelper } from './FsHelper';
1313
import { Message } from './Message';
1414

1515
import { AsyncSeriesHook, SyncHook } from 'tapable';
@@ -68,8 +68,14 @@ class ForkTsCheckerWebpackPlugin {
6868
public static readonly DEFAULT_MEMORY_LIMIT = 2048;
6969
public static readonly ONE_CPU = 1;
7070
public static readonly ALL_CPUS = os.cpus && os.cpus() ? os.cpus().length : 1;
71-
public static readonly ONE_CPU_FREE = Math.max(1, ForkTsCheckerWebpackPlugin.ALL_CPUS - 1);
72-
public static readonly TWO_CPUS_FREE = Math.max(1, ForkTsCheckerWebpackPlugin.ALL_CPUS - 2);
71+
public static readonly ONE_CPU_FREE = Math.max(
72+
1,
73+
ForkTsCheckerWebpackPlugin.ALL_CPUS - 1
74+
);
75+
public static readonly TWO_CPUS_FREE = Math.max(
76+
1,
77+
ForkTsCheckerWebpackPlugin.ALL_CPUS - 2
78+
);
7379

7480
public readonly options: Partial<Options>;
7581
private tsconfig: string;
@@ -129,9 +135,8 @@ class ForkTsCheckerWebpackPlugin {
129135
: options.tslint
130136
: undefined;
131137
this.tslintAutoFix = options.tslintAutoFix || false;
132-
this.watch = (typeof options.watch === 'string')
133-
? [options.watch]
134-
: options.watch || [];
138+
this.watch =
139+
typeof options.watch === 'string' ? [options.watch] : options.watch || [];
135140
this.ignoreDiagnostics = options.ignoreDiagnostics || [];
136141
this.ignoreLints = options.ignoreLints || [];
137142
this.reportFiles = options.reportFiles || [];
@@ -145,7 +150,7 @@ class ForkTsCheckerWebpackPlugin {
145150
this.useColors = options.colors !== false; // default true
146151
this.colors = new chalk.constructor({ enabled: this.useColors });
147152
this.formatter =
148-
options.formatter && (typeof options.formatter === 'function')
153+
options.formatter && typeof options.formatter === 'function'
149154
? options.formatter
150155
: ForkTsCheckerWebpackPlugin.createFormatter(
151156
(options.formatter as 'default' | 'codeframe') || 'default',
@@ -201,8 +206,8 @@ class ForkTsCheckerWebpackPlugin {
201206
this.watchPaths = this.watch.map(this.computeContextPath.bind(this));
202207

203208
// validate config
204-
const tsconfigOk = fs.existsSync(this.tsconfigPath);
205-
const tslintOk = !this.tslintPath || fs.existsSync(this.tslintPath);
209+
const tsconfigOk = FsHelper.existsSync(this.tsconfigPath);
210+
const tslintOk = !this.tslintPath || FsHelper.existsSync(this.tslintPath);
206211

207212
// validate logger
208213
if (this.logger) {
@@ -745,7 +750,10 @@ class ForkTsCheckerWebpackPlugin {
745750
}
746751
}
747752

748-
private createEmitCallback(compilation: webpack.compilation.Compilation, callback: () => void) {
753+
private createEmitCallback(
754+
compilation: webpack.compilation.Compilation,
755+
callback: () => void
756+
) {
749757
return function emitCallback(this: ForkTsCheckerWebpackPlugin) {
750758
if (!this.elapsed) {
751759
throw new Error('Execution order error');

test/unit/CancellationToken.spec.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ var describe = require('mocha').describe;
22
var it = require('mocha').it;
33
var os = require('os');
44
var ts = require('typescript');
5-
var fs = require('fs');
65
var beforeEach = require('mocha').beforeEach;
76
var afterEach = require('mocha').afterEach;
87
var expect = require('chai').expect;
98
var mockFs = require('mock-fs');
109
var CancellationToken = require('../../lib/CancellationToken')
1110
.CancellationToken;
11+
var FsHelper = require('../../lib/FsHelper').FsHelper;
1212

1313
describe('[UNIT] CancellationToken', function() {
1414
beforeEach(function() {
@@ -72,15 +72,17 @@ describe('[UNIT] CancellationToken', function() {
7272
var tokenB = new CancellationToken('rgeer#R23r$#T$3t#$t43', true);
7373
expect(function() {
7474
tokenB.throwIfCancellationRequested();
75-
}).to.throw().instanceOf(ts.OperationCanceledException);
75+
})
76+
.to.throw()
77+
.instanceOf(ts.OperationCanceledException);
7678
});
7779

7880
it('should write file in filesystem on requestCancellation', function() {
7981
var tokenA = new CancellationToken();
8082
tokenA.requestCancellation();
8183

8284
expect(tokenA.isCancellationRequested()).to.be.true;
83-
expect(fs.existsSync(tokenA.getCancellationFilePath())).to.be.true;
85+
expect(FsHelper.existsSync(tokenA.getCancellationFilePath())).to.be.true;
8486
});
8587

8688
it('should cleanup file on cleanupCancellation', function() {
@@ -89,7 +91,7 @@ describe('[UNIT] CancellationToken', function() {
8991
tokenA.cleanupCancellation();
9092

9193
expect(tokenA.isCancellationRequested()).to.be.false;
92-
expect(fs.existsSync(tokenA.getCancellationFilePath())).to.be.false;
94+
expect(FsHelper.existsSync(tokenA.getCancellationFilePath())).to.be.false;
9395

9496
// make sure we can call it as many times as we want to
9597
expect(function() {

0 commit comments

Comments
 (0)