Skip to content

Commit

Permalink
Working as expected on first line diagnostic.start (#190)
Browse files Browse the repository at this point in the history
* fix throwing error on first line diagnostic

* Removed deprecated fs.existsSync;

package.json and CHANGELOG.md bump
  • Loading branch information
radiosterne authored and johnnyreilly committed Dec 13, 2018
1 parent ca92343 commit fe2a35d
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 35 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v0.5.2

* [Fix erroneous error on diagnostics at 0 line; remove deprecated fs.existsSync](https://github.com/Realytics/fork-ts-checker-webpack-plugin/pull/190) (#190)

## v0.5.1

* [Make the checker compile with TypeScript 3.2](https://github.com/Realytics/fork-ts-checker-webpack-plugin/pull/189)
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": "fork-ts-checker-webpack-plugin",
"version": "0.5.1",
"version": "0.5.2",
"description": "Runs typescript type checker and linter on separate process.",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
9 changes: 7 additions & 2 deletions src/CancellationToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import * as os from 'os';
import * as path from 'path';
import * as ts from 'typescript';

import { FsHelper } from './FsHelper';

interface CancellationTokenData {
isCancelled: boolean;
cancellationFileName: string;
Expand Down Expand Up @@ -46,7 +48,7 @@ export class CancellationToken {
if (duration > 10) {
// check no more than once every 10ms
this.lastCancellationCheckTime = time;
this.isCancelled = fs.existsSync(this.getCancellationFilePath());
this.isCancelled = FsHelper.existsSync(this.getCancellationFilePath());
}

return this.isCancelled;
Expand All @@ -64,7 +66,10 @@ export class CancellationToken {
}

public cleanupCancellation() {
if (this.isCancelled && fs.existsSync(this.getCancellationFilePath())) {
if (
this.isCancelled &&
FsHelper.existsSync(this.getCancellationFilePath())
) {
fs.unlinkSync(this.getCancellationFilePath());
this.isCancelled = false;
}
Expand Down
16 changes: 16 additions & 0 deletions src/FsHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as fs from 'fs';

export class FsHelper {
public static existsSync(filePath: fs.PathLike) {
try {
fs.statSync(filePath);
} catch (err) {
if (err.code === 'ENOENT') {
return false;
} else {
throw err;
}
}
return true;
}
}
3 changes: 2 additions & 1 deletion src/IncrementalChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { NormalizedMessage } from './NormalizedMessage';
import { CancellationToken } from './CancellationToken';
import * as minimatch from 'minimatch';
import { VueProgram } from './VueProgram';
import { FsHelper } from './FsHelper';

// Need some augmentation here - linterOptions.exclude is not (yet) part of the official
// types for tslint.
Expand Down Expand Up @@ -284,7 +285,7 @@ export class IncrementalChecker {
linter.lint(fileName, undefined!, this.linterConfig);
} catch (e) {
if (
fs.existsSync(fileName) &&
FsHelper.existsSync(fileName) &&
// check the error type due to file system lag
!(e instanceof Error) &&
!(e.constructor.name === 'FatalError') &&
Expand Down
22 changes: 11 additions & 11 deletions src/NormalizedMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class NormalizedMessage {
let character: number | undefined;
if (diagnostic.file) {
file = diagnostic.file.fileName;
if (!diagnostic.start) {
if (diagnostic.start === undefined) {
throw new Error('Expected diagnostics to have start');
}
const position = diagnostic.file.getLineAndCharacterOfPosition(
Expand Down Expand Up @@ -92,7 +92,10 @@ export class NormalizedMessage {
return new NormalizedMessage(json);
}

public static compare(messageA: NormalizedMessage, messageB: NormalizedMessage) {
public static compare(
messageA: NormalizedMessage,
messageB: NormalizedMessage
) {
if (!(messageA instanceof NormalizedMessage)) {
return -1;
}
Expand All @@ -102,18 +105,12 @@ export class NormalizedMessage {

return (
NormalizedMessage.compareTypes(messageA.type, messageB.type) ||
NormalizedMessage.compareOptionalStrings(
messageA.file,
messageB.file
) ||
NormalizedMessage.compareOptionalStrings(messageA.file, messageB.file) ||
NormalizedMessage.compareSeverities(
messageA.severity,
messageB.severity
) ||
NormalizedMessage.compareNumbers(
messageA.line,
messageB.line
) ||
NormalizedMessage.compareNumbers(messageA.line, messageB.line) ||
NormalizedMessage.compareNumbers(
messageA.character,
messageB.character
Expand All @@ -131,7 +128,10 @@ export class NormalizedMessage {
);
}

public static equals(messageA: NormalizedMessage, messageB: NormalizedMessage) {
public static equals(
messageA: NormalizedMessage,
messageB: NormalizedMessage
) {
return this.compare(messageA, messageB) === 0;
}

Expand Down
9 changes: 3 additions & 6 deletions src/formatter/codeframeFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import codeFrame = require('babel-code-frame');
import chalk from 'chalk';
import * as fs from 'fs';
import { NormalizedMessage } from '../NormalizedMessage';
import { FsHelper } from '../FsHelper';

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

const file = message.file;
const source =
file &&
fs.existsSync(file) &&
fs.readFileSync(file, 'utf-8');
file && FsHelper.existsSync(file) && fs.readFileSync(file, 'utf-8');
let frame = '';

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

return (
messageColor(
message.severity.toUpperCase() + ' in ' + message.file
) +
messageColor(message.severity.toUpperCase() + ' in ' + message.file) +
os.EOL +
positionColor(message.line + ':' + message.character) +
' ' +
Expand Down
28 changes: 18 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import * as path from 'path';
import * as process from 'process';
import * as childProcess from 'child_process';
import chalk, { Chalk } from 'chalk';
import * as fs from 'fs';
import * as micromatch from 'micromatch';
import * as os from 'os';
import * as webpack from 'webpack';
import { CancellationToken } from './CancellationToken';
import { NormalizedMessage } from './NormalizedMessage';
import { createDefaultFormatter } from './formatter/defaultFormatter';
import { createCodeframeFormatter } from './formatter/codeframeFormatter';
import { FsHelper } from './FsHelper';
import { Message } from './Message';

import { AsyncSeriesHook, SyncHook } from 'tapable';
Expand Down Expand Up @@ -68,8 +68,14 @@ class ForkTsCheckerWebpackPlugin {
public static readonly DEFAULT_MEMORY_LIMIT = 2048;
public static readonly ONE_CPU = 1;
public static readonly ALL_CPUS = os.cpus && os.cpus() ? os.cpus().length : 1;
public static readonly ONE_CPU_FREE = Math.max(1, ForkTsCheckerWebpackPlugin.ALL_CPUS - 1);
public static readonly TWO_CPUS_FREE = Math.max(1, ForkTsCheckerWebpackPlugin.ALL_CPUS - 2);
public static readonly ONE_CPU_FREE = Math.max(
1,
ForkTsCheckerWebpackPlugin.ALL_CPUS - 1
);
public static readonly TWO_CPUS_FREE = Math.max(
1,
ForkTsCheckerWebpackPlugin.ALL_CPUS - 2
);

public readonly options: Partial<Options>;
private tsconfig: string;
Expand Down Expand Up @@ -129,9 +135,8 @@ class ForkTsCheckerWebpackPlugin {
: options.tslint
: undefined;
this.tslintAutoFix = options.tslintAutoFix || false;
this.watch = (typeof options.watch === 'string')
? [options.watch]
: options.watch || [];
this.watch =
typeof options.watch === 'string' ? [options.watch] : options.watch || [];
this.ignoreDiagnostics = options.ignoreDiagnostics || [];
this.ignoreLints = options.ignoreLints || [];
this.reportFiles = options.reportFiles || [];
Expand All @@ -145,7 +150,7 @@ class ForkTsCheckerWebpackPlugin {
this.useColors = options.colors !== false; // default true
this.colors = new chalk.constructor({ enabled: this.useColors });
this.formatter =
options.formatter && (typeof options.formatter === 'function')
options.formatter && typeof options.formatter === 'function'
? options.formatter
: ForkTsCheckerWebpackPlugin.createFormatter(
(options.formatter as 'default' | 'codeframe') || 'default',
Expand Down Expand Up @@ -201,8 +206,8 @@ class ForkTsCheckerWebpackPlugin {
this.watchPaths = this.watch.map(this.computeContextPath.bind(this));

// validate config
const tsconfigOk = fs.existsSync(this.tsconfigPath);
const tslintOk = !this.tslintPath || fs.existsSync(this.tslintPath);
const tsconfigOk = FsHelper.existsSync(this.tsconfigPath);
const tslintOk = !this.tslintPath || FsHelper.existsSync(this.tslintPath);

// validate logger
if (this.logger) {
Expand Down Expand Up @@ -745,7 +750,10 @@ class ForkTsCheckerWebpackPlugin {
}
}

private createEmitCallback(compilation: webpack.compilation.Compilation, callback: () => void) {
private createEmitCallback(
compilation: webpack.compilation.Compilation,
callback: () => void
) {
return function emitCallback(this: ForkTsCheckerWebpackPlugin) {
if (!this.elapsed) {
throw new Error('Execution order error');
Expand Down
10 changes: 6 additions & 4 deletions test/unit/CancellationToken.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ var describe = require('mocha').describe;
var it = require('mocha').it;
var os = require('os');
var ts = require('typescript');
var fs = require('fs');
var beforeEach = require('mocha').beforeEach;
var afterEach = require('mocha').afterEach;
var expect = require('chai').expect;
var mockFs = require('mock-fs');
var CancellationToken = require('../../lib/CancellationToken')
.CancellationToken;
var FsHelper = require('../../lib/FsHelper').FsHelper;

describe('[UNIT] CancellationToken', function() {
beforeEach(function() {
Expand Down Expand Up @@ -72,15 +72,17 @@ describe('[UNIT] CancellationToken', function() {
var tokenB = new CancellationToken('rgeer#R23r$#T$3t#$t43', true);
expect(function() {
tokenB.throwIfCancellationRequested();
}).to.throw().instanceOf(ts.OperationCanceledException);
})
.to.throw()
.instanceOf(ts.OperationCanceledException);
});

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

expect(tokenA.isCancellationRequested()).to.be.true;
expect(fs.existsSync(tokenA.getCancellationFilePath())).to.be.true;
expect(FsHelper.existsSync(tokenA.getCancellationFilePath())).to.be.true;
});

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

expect(tokenA.isCancellationRequested()).to.be.false;
expect(fs.existsSync(tokenA.getCancellationFilePath())).to.be.false;
expect(FsHelper.existsSync(tokenA.getCancellationFilePath())).to.be.false;

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

0 comments on commit fe2a35d

Please sign in to comment.