fork-ts-checker-webpack-plugin 0.2.6 → 0.2.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,190 +1,174 @@
1
- var ts = require('typescript');
2
- var fs = require('fs');
3
- var path = require('path');
4
- var endsWith = require('lodash.endswith');
5
- var FilesRegister = require('./FilesRegister');
6
- var FilesWatcher = require('./FilesWatcher');
7
- var WorkSet = require('./WorkSet');
8
- var NormalizedMessage = require('./NormalizedMessage');
9
-
10
- function IncrementalChecker (programConfigFile, linterConfigFile, watchPaths, workNumber, workDivision) {
11
- this.programConfigFile = programConfigFile;
12
- this.linterConfigFile = linterConfigFile;
13
- this.watchPaths = watchPaths;
14
- this.workNumber = workNumber || 0;
15
- this.workDivision = workDivision || 1;
16
-
17
- // it's shared between compilations
18
- this.files = new FilesRegister(function() {
19
- // data shape
20
- return {
21
- source: undefined,
22
- linted: false,
23
- lints: []
1
+ "use strict";
2
+ var fs = require("fs");
3
+ var endsWith = require("lodash.endswith");
4
+ var path = require("path");
5
+ var ts = require("typescript");
6
+ var FilesRegister = require("./FilesRegister");
7
+ var FilesWatcher = require("./FilesWatcher");
8
+ var WorkSet = require("./WorkSet");
9
+ var NormalizedMessage = require("./NormalizedMessage");
10
+ var minimatch = require("minimatch");
11
+ var IncrementalChecker = /** @class */ (function () {
12
+ function IncrementalChecker(programConfigFile, linterConfigFile, watchPaths, workNumber, workDivision, checkSyntacticErrors) {
13
+ this.programConfigFile = programConfigFile;
14
+ this.linterConfigFile = linterConfigFile;
15
+ this.watchPaths = watchPaths;
16
+ this.workNumber = workNumber || 0;
17
+ this.workDivision = workDivision || 1;
18
+ this.checkSyntacticErrors = checkSyntacticErrors || false;
19
+ // Use empty array of exclusions in general to avoid having
20
+ // to check of its existence later on.
21
+ this.linterExclusions = [];
22
+ // it's shared between compilations
23
+ this.files = new FilesRegister(function () { return ({
24
+ // data shape
25
+ source: undefined,
26
+ linted: false,
27
+ lints: []
28
+ }); });
29
+ }
30
+ IncrementalChecker.loadProgramConfig = function (configFile) {
31
+ return ts.parseJsonConfigFileContent(
32
+ // Regardless of the setting in the tsconfig.json we want isolatedModules to be false
33
+ Object.assign(ts.readConfigFile(configFile, ts.sys.readFile).config, { isolatedModules: false }), ts.sys, path.dirname(configFile));
34
+ };
35
+ IncrementalChecker.loadLinterConfig = function (configFile) {
36
+ var tslint = require('tslint');
37
+ return tslint.Configuration.loadConfigurationFromPath(configFile);
38
+ };
39
+ IncrementalChecker.createProgram = function (programConfig, files, watcher, oldProgram) {
40
+ var host = ts.createCompilerHost(programConfig.options);
41
+ var realGetSourceFile = host.getSourceFile;
42
+ host.getSourceFile = function (filePath, languageVersion, onError) {
43
+ // first check if watcher is watching file - if not - check it's mtime
44
+ if (!watcher.isWatchingFile(filePath)) {
45
+ try {
46
+ var stats = fs.statSync(filePath);
47
+ files.setMtime(filePath, stats.mtime.valueOf());
48
+ }
49
+ catch (e) {
50
+ // probably file does not exists
51
+ files.remove(filePath);
52
+ }
53
+ }
54
+ // get source file only if there is no source in files register
55
+ if (!files.has(filePath) || !files.getData(filePath).source) {
56
+ files.mutateData(filePath, function (data) {
57
+ data.source = realGetSourceFile(filePath, languageVersion, onError);
58
+ });
59
+ }
60
+ return files.getData(filePath).source;
61
+ };
62
+ return ts.createProgram(programConfig.fileNames, programConfig.options, host, oldProgram // re-use old program
63
+ );
64
+ };
65
+ IncrementalChecker.createLinter = function (program) {
66
+ var tslint = require('tslint');
67
+ return new tslint.Linter({ fix: false }, program);
68
+ };
69
+ IncrementalChecker.isFileExcluded = function (filePath, linterExclusions) {
70
+ return endsWith(filePath, '.d.ts') || linterExclusions.some(function (matcher) { return matcher.match(filePath); });
71
+ };
72
+ IncrementalChecker.prototype.nextIteration = function () {
73
+ var _this = this;
74
+ if (!this.watcher) {
75
+ this.watcher = new FilesWatcher(this.watchPaths, ['.ts', '.tsx']);
76
+ // connect watcher with register
77
+ this.watcher.on('change', function (filePath, stats) {
78
+ _this.files.setMtime(filePath, stats.mtime.valueOf());
79
+ });
80
+ this.watcher.on('unlink', function (filePath) {
81
+ _this.files.remove(filePath);
82
+ });
83
+ this.watcher.watch();
84
+ }
85
+ if (!this.programConfig) {
86
+ this.programConfig = IncrementalChecker.loadProgramConfig(this.programConfigFile);
87
+ }
88
+ if (!this.linterConfig && this.linterConfigFile) {
89
+ this.linterConfig = IncrementalChecker.loadLinterConfig(this.linterConfigFile);
90
+ if (this.linterConfig.linterOptions && this.linterConfig.linterOptions.exclude) {
91
+ // Pre-build minimatch patterns to avoid additional overhead later on.
92
+ // Note: Resolving the path is required to properly match against the full file paths,
93
+ // and also deals with potential cross-platform problems regarding path separators.
94
+ this.linterExclusions = this.linterConfig.linterOptions.exclude.map(function (pattern) { return new minimatch.Minimatch(path.resolve(pattern)); });
95
+ }
96
+ }
97
+ this.program = IncrementalChecker.createProgram(this.programConfig, this.files, this.watcher, this.program);
98
+ if (this.linterConfig) {
99
+ this.linter = IncrementalChecker.createLinter(this.program);
100
+ }
101
+ };
102
+ IncrementalChecker.prototype.hasLinter = function () {
103
+ return this.linter !== undefined;
24
104
  };
25
- });
26
- }
105
+ IncrementalChecker.prototype.getDiagnostics = function (cancellationToken) {
106
+ var _this = this;
107
+ var diagnostics = [];
108
+ // select files to check (it's semantic check - we have to include all files :/)
109
+ var filesToCheck = this.program.getSourceFiles();
110
+ // calculate subset of work to do
111
+ var workSet = new WorkSet(filesToCheck, this.workNumber, this.workDivision);
112
+ // check given work set
113
+ workSet.forEach(function (sourceFile) {
114
+ if (cancellationToken) {
115
+ cancellationToken.throwIfCancellationRequested();
116
+ }
117
+ var diagnosticsToRegister = _this.checkSyntacticErrors
118
+ ? []
119
+ .concat(_this.program.getSemanticDiagnostics(sourceFile, cancellationToken))
120
+ .concat(_this.program.getSyntacticDiagnostics(sourceFile, cancellationToken))
121
+ : _this.program.getSemanticDiagnostics(sourceFile, cancellationToken);
122
+ diagnostics.push.apply(diagnostics, diagnosticsToRegister);
123
+ });
124
+ // normalize and deduplicate diagnostics
125
+ return NormalizedMessage.deduplicate(diagnostics.map(NormalizedMessage.createFromDiagnostic));
126
+ };
127
+ IncrementalChecker.prototype.getLints = function (cancellationToken) {
128
+ var _this = this;
129
+ if (!this.hasLinter()) {
130
+ throw new Error('Cannot get lints - checker has no linter.');
131
+ }
132
+ // select files to lint
133
+ var filesToLint = this.files.keys().filter(function (filePath) {
134
+ return !_this.files.getData(filePath).linted && !IncrementalChecker.isFileExcluded(filePath, _this.linterExclusions);
135
+ });
136
+ // calculate subset of work to do
137
+ var workSet = new WorkSet(filesToLint, this.workNumber, this.workDivision);
138
+ // lint given work set
139
+ workSet.forEach(function (fileName) {
140
+ cancellationToken.throwIfCancellationRequested();
141
+ try {
142
+ _this.linter.lint(fileName, undefined, _this.linterConfig);
143
+ }
144
+ catch (e) {
145
+ if (fs.existsSync(fileName)) {
146
+ // it's not because file doesn't exist - throw error
147
+ throw e;
148
+ }
149
+ }
150
+ });
151
+ // set lints in files register
152
+ this.linter.getResult().failures.forEach(function (lint) {
153
+ var filePath = lint.getFileName();
154
+ _this.files.mutateData(filePath, function (data) {
155
+ data.linted = true;
156
+ data.lints.push(lint);
157
+ });
158
+ });
159
+ // set all files as linted
160
+ this.files.keys().forEach(function (filePath) {
161
+ _this.files.mutateData(filePath, function (data) {
162
+ data.linted = true;
163
+ });
164
+ });
165
+ // get all lints
166
+ var lints = this.files.keys().reduce(function (innerLints, filePath) {
167
+ return innerLints.concat(_this.files.getData(filePath).lints);
168
+ }, []);
169
+ // normalize and deduplicate lints
170
+ return NormalizedMessage.deduplicate(lints.map(NormalizedMessage.createFromLint));
171
+ };
172
+ return IncrementalChecker;
173
+ }());
27
174
  module.exports = IncrementalChecker;
28
-
29
- IncrementalChecker.loadProgramConfig = function (configFile) {
30
- return ts.parseJsonConfigFileContent(
31
- // Regardless of the setting in the tsconfig.json we want isolatedModules to be false
32
- Object.assign(ts.readConfigFile(configFile, ts.sys.readFile).config, { isolatedModules: false }),
33
- ts.sys,
34
- path.dirname(configFile)
35
- );
36
- };
37
-
38
- IncrementalChecker.loadLinterConfig = function (configFile) {
39
- var tslint = require('tslint');
40
-
41
- return tslint.Configuration.loadConfigurationFromPath(configFile);
42
- };
43
-
44
- IncrementalChecker.createProgram = function (programConfig, files, watcher, oldProgram) {
45
- var host = ts.createCompilerHost(programConfig.options);
46
- var realGetSourceFile = host.getSourceFile;
47
-
48
- host.getSourceFile = function (filePath, languageVersion, onError) {
49
- // first check if watcher is watching file - if not - check it's mtime
50
- if (!watcher.isWatchingFile(filePath)) {
51
- try {
52
- var stats = fs.statSync(filePath);
53
-
54
- files.setMtime(filePath, stats.mtime.valueOf());
55
- } catch (e) {
56
- // probably file does not exists
57
- files.remove(filePath);
58
- }
59
- }
60
-
61
- // get source file only if there is no source in files register
62
- if (!files.has(filePath) || !files.getData(filePath).source) {
63
- files.mutateData(filePath, function (data) {
64
- data.source = realGetSourceFile(filePath, languageVersion, onError);
65
- });
66
- }
67
-
68
- return files.getData(filePath).source;
69
- };
70
-
71
- return ts.createProgram(
72
- programConfig.fileNames,
73
- programConfig.options,
74
- host,
75
- oldProgram // re-use old program
76
- );
77
- };
78
-
79
- IncrementalChecker.createLinter = function (program) {
80
- var tslint = require('tslint');
81
-
82
- return new tslint.Linter({ fix: false }, program);
83
- };
84
-
85
- IncrementalChecker.prototype.nextIteration = function () {
86
- if (!this.watcher) {
87
- this.watcher = new FilesWatcher(this.watchPaths, ['.ts', '.tsx']);
88
-
89
- // connect watcher with register
90
- this.watcher.on('change', function (filePath, stats) { this.files.setMtime(filePath, stats.mtime.valueOf()); }.bind(this));
91
- this.watcher.on('unlink', function (filePath) { this.files.remove(filePath); }.bind(this));
92
-
93
- this.watcher.watch();
94
- }
95
-
96
- if (!this.programConfig) {
97
- this.programConfig = IncrementalChecker.loadProgramConfig(this.programConfigFile);
98
- }
99
-
100
- if (!this.linterConfig && this.linterConfigFile) {
101
- this.linterConfig = IncrementalChecker.loadLinterConfig(this.linterConfigFile);
102
- }
103
-
104
- this.program = IncrementalChecker.createProgram(this.programConfig, this.files, this.watcher, this.program);
105
- if (this.linterConfig) {
106
- this.linter = IncrementalChecker.createLinter(this.program);
107
- }
108
- };
109
-
110
- IncrementalChecker.prototype.hasLinter = function () {
111
- return this.linter !== undefined;
112
- };
113
-
114
- IncrementalChecker.prototype.getDiagnostics = function (cancellationToken) {
115
- var diagnostics = [];
116
- // select files to check (it's semantic check - we have to include all files :/)
117
- var filesToCheck = this.program.getSourceFiles();
118
-
119
- // calculate subset of work to do
120
- var workSet = new WorkSet(filesToCheck, this.workNumber, this.workDivision);
121
-
122
- // check given work set
123
- workSet.forEach(function (sourceFile) {
124
- if (cancellationToken) {
125
- cancellationToken.throwIfCancellationRequested();
126
- }
127
-
128
- diagnostics.push.apply(diagnostics, this.program.getSemanticDiagnostics(sourceFile, cancellationToken));
129
- }.bind(this));
130
-
131
- // normalize and deduplicate diagnostics
132
- return NormalizedMessage.deduplicate(
133
- diagnostics.map(NormalizedMessage.createFromDiagnostic)
134
- );
135
- };
136
-
137
- IncrementalChecker.prototype.getLints = function (cancellationToken) {
138
- if (!this.hasLinter()) {
139
- throw new Error('Cannot get lints - checker has no linter.');
140
- }
141
-
142
- // select files to lint
143
- var filesToLint = this.files.keys().filter(function (filePath) {
144
- return !endsWith(filePath, '.d.ts') && !this.files.getData(filePath).linted;
145
- }.bind(this));
146
-
147
- // calculate subset of work to do
148
- var workSet = new WorkSet(filesToLint, this.workNumber, this.workDivision);
149
-
150
- // lint given work set
151
- workSet.forEach(function (fileName) {
152
- cancellationToken.throwIfCancellationRequested();
153
-
154
- try {
155
- this.linter.lint(fileName, undefined, this.linterConfig);
156
- } catch (e) {
157
- if (fs.existsSync(fileName)) {
158
- // it's not because file doesn't exist - throw error
159
- throw e;
160
- }
161
- }
162
- }.bind(this));
163
-
164
- // set lints in files register
165
- this.linter.getResult().failures.forEach(function (lint) {
166
- var filePath = lint.getFileName();
167
-
168
- this.files.mutateData(filePath, function (data) {
169
- data.linted = true;
170
- data.lints.push(lint);
171
- });
172
- }.bind(this));
173
-
174
- // set all files as linted
175
- this.files.keys().forEach(function (filePath) {
176
- this.files.mutateData(filePath, function (data) {
177
- data.linted = true;
178
- });
179
- }.bind(this));
180
-
181
- // get all lints
182
- var lints = this.files.keys().reduce(function (lints, filePath) {
183
- return lints.concat(this.files.getData(filePath).lints);
184
- }.bind(this), []);
185
-
186
- // normalize and deduplicate lints
187
- return NormalizedMessage.deduplicate(
188
- lints.map(NormalizedMessage.createFromLint)
189
- );
190
- };
package/lib/Message.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });