Skip to content

Commit 35b9a5b

Browse files
authored
start introducing strict-boolean-expressions (TypeStrong#855)
* start introducing strict-boolean-expressions * more micro optimisation * prepare to release
1 parent 4b36306 commit 35b9a5b

File tree

10 files changed

+85
-62
lines changed

10 files changed

+85
-62
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 5.2.2
4+
5+
* [feat: Micro-optimizations](https://github.com/TypeStrong/ts-loader/pull/855) - thanks @johnnyreilly
6+
37
## 5.2.1
48

59
* [feat: Lists typescript as a peer dependency](https://github.com/TypeStrong/ts-loader/pull/841) - thanks @arcanis!

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ts-loader",
3-
"version": "5.2.1",
3+
"version": "5.2.2",
44
"description": "TypeScript loader for webpack",
55
"main": "index.js",
66
"types": "dist/types/index.d.ts",

src/after-compile.ts

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ function provideCompilerOptionDiagnosticErrorsToWebpack(
8484
const { languageService, loaderOptions, compiler, program } = instance;
8585

8686
const errorsToAdd = formatErrors(
87-
program
88-
? program.getOptionsDiagnostics()
89-
: languageService!.getCompilerOptionsDiagnostics(),
87+
program === undefined
88+
? languageService!.getCompilerOptionsDiagnostics()
89+
: program.getOptionsDiagnostics(),
9090
loaderOptions,
9191
instance.colors,
9292
compiler,
@@ -177,34 +177,37 @@ function provideErrorsToWebpack(
177177
otherFiles
178178
} = instance;
179179

180-
const filePathRegex = !!compilerOptions.checkJs
181-
? constants.dtsTsTsxJsJsxRegex
182-
: constants.dtsTsTsxRegex;
180+
const filePathRegex =
181+
compilerOptions.checkJs === true
182+
? constants.dtsTsTsxJsJsxRegex
183+
: constants.dtsTsTsxRegex;
183184

184185
for (const filePath of filesToCheckForErrors.keys()) {
185-
if (!filePath.match(filePathRegex)) {
186+
if (filePath.match(filePathRegex) === null) {
186187
continue;
187188
}
188189

189-
const sourceFile = program && program.getSourceFile(filePath);
190+
const sourceFile =
191+
program === undefined ? undefined : program.getSourceFile(filePath);
190192

191193
// If the source file is undefined, that probably means it’s actually part of an unbuilt project reference,
192194
// which will have already produced a more useful error than the one we would get by proceeding here.
193195
// If it’s undefined and we’re not using project references at all, I guess carry on so the user will
194196
// get a useful error about which file was unexpectedly missing.
195-
if (isUsingProjectReferences(instance) && !sourceFile) {
197+
if (isUsingProjectReferences(instance) && sourceFile === undefined) {
196198
continue;
197199
}
198200

199-
const errors = program
200-
? [
201-
...program.getSyntacticDiagnostics(sourceFile),
202-
...program.getSemanticDiagnostics(sourceFile)
203-
]
204-
: [
205-
...languageService!.getSyntacticDiagnostics(filePath),
206-
...languageService!.getSemanticDiagnostics(filePath)
207-
];
201+
const errors =
202+
program === undefined
203+
? [
204+
...languageService!.getSyntacticDiagnostics(filePath),
205+
...languageService!.getSemanticDiagnostics(filePath)
206+
]
207+
: [
208+
...program.getSyntacticDiagnostics(sourceFile),
209+
...program.getSemanticDiagnostics(sourceFile)
210+
];
208211
if (errors.length > 0) {
209212
const fileWithError = files.get(filePath) || otherFiles.get(filePath);
210213
filesWithErrors.set(filePath, fileWithError!);
@@ -255,7 +258,7 @@ function provideDeclarationFilesToWebpack(
255258
compilation: WebpackCompilation
256259
) {
257260
for (const filePath of filesToCheckForErrors.keys()) {
258-
if (!filePath.match(constants.tsTsxRegex)) {
261+
if (filePath.match(constants.tsTsxRegex) === null) {
259262
continue;
260263
}
261264

src/compilerSetup.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ export function getCompiler(loaderOptions: LoaderOptions, log: logger.Logger) {
2828
}`;
2929
compilerCompatible = false;
3030
if (loaderOptions.compiler === 'typescript') {
31-
if (compiler!.version && semver.gte(compiler!.version, '2.4.1')) {
31+
if (
32+
compiler!.version !== undefined &&
33+
semver.gte(compiler!.version, '2.4.1')
34+
) {
3235
// don't log yet in this case, if a tsconfig.json exists we want to combine the message
3336
compilerCompatible = true;
3437
} else {

src/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ function findConfigFile(
9898
// If `configFile` is a relative path, resolve it.
9999
// We define a relative path as: starts with
100100
// one or two dots + a common directory delimiter
101-
if (configFile.match(/^\.\.?(\/|\\)/)) {
101+
if (configFile.match(/^\.\.?(\/|\\)/) !== null) {
102102
const resolvedPath = path.resolve(requestDirPath, configFile);
103103
return compiler.sys.fileExists(resolvedPath) ? resolvedPath : undefined;
104104

src/index.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const loaderOptionsCache: LoaderOptionsCache = {};
3030
* The entry point for ts-loader
3131
*/
3232
function loader(this: Webpack, contents: string) {
33-
// tslint:disable-next-line:no-unused-expression
33+
// tslint:disable-next-line:no-unused-expression strict-boolean-expressions
3434
this.cacheable && this.cacheable();
3535
const callback = this.async();
3636
const options = getLoaderOptions(this);
@@ -80,7 +80,7 @@ function successLoader(
8080
),
8181
path.relative(loaderContext.rootContext, filePath)
8282
];
83-
if (referencedProject.commandLine.options.outFile) {
83+
if (referencedProject.commandLine.options.outFile !== undefined) {
8484
throw new Error(
8585
`The referenced project at ${relativeProjectConfigPath} is using ` +
8686
`the outFile' option, which is not supported with ts-loader.`
@@ -184,7 +184,7 @@ function makeSourceMapAndFinish(
184184
);
185185

186186
// _module.meta is not available inside happypack
187-
if (!options.happyPackMode && loaderContext._module.buildMeta) {
187+
if (!options.happyPackMode && loaderContext._module.buildMeta !== undefined) {
188188
// Make sure webpack is aware that even though the emitted JavaScript may be the same as
189189
// a previously cached version the TypeScript may be different and therefore should be
190190
// treated as new
@@ -278,7 +278,10 @@ ${validLoaderOptions.join(' / ')}
278278
}
279279
}
280280

281-
if (loaderOptions.context && !path.isAbsolute(loaderOptions.context)) {
281+
if (
282+
loaderOptions.context !== undefined &&
283+
!path.isAbsolute(loaderOptions.context)
284+
) {
282285
throw new Error(
283286
`Option 'context' has to be an absolute path. Given '${
284287
loaderOptions.context
@@ -342,7 +345,7 @@ function updateFileInCache(
342345
instance.otherFiles.delete(filePath);
343346
instance.files.set(filePath, file);
344347
} else {
345-
if (instance.watchHost) {
348+
if (instance.watchHost !== undefined) {
346349
fileWatcherEventKind = instance.compiler.FileWatcherEventKind.Created;
347350
}
348351
file = { version: 0 };
@@ -351,20 +354,23 @@ function updateFileInCache(
351354
instance.changedFilesList = true;
352355
}
353356

354-
if (instance.watchHost && contents === undefined) {
357+
if (instance.watchHost !== undefined && contents === undefined) {
355358
fileWatcherEventKind = instance.compiler.FileWatcherEventKind.Deleted;
356359
}
357360

358361
if (file.text !== contents) {
359362
file.version++;
360363
file.text = contents;
361364
instance.version!++;
362-
if (instance.watchHost && fileWatcherEventKind === undefined) {
365+
if (
366+
instance.watchHost !== undefined &&
367+
fileWatcherEventKind === undefined
368+
) {
363369
fileWatcherEventKind = instance.compiler.FileWatcherEventKind.Changed;
364370
}
365371
}
366372

367-
if (instance.watchHost && fileWatcherEventKind !== undefined) {
373+
if (instance.watchHost !== undefined && fileWatcherEventKind !== undefined) {
368374
instance.hasUnaccountedModifiedFiles = true;
369375
instance.watchHost.invokeFileWatcher(filePath, fileWatcherEventKind);
370376
instance.watchHost.invokeDirectoryWatcher(path.dirname(filePath), filePath);
@@ -418,7 +424,7 @@ function getEmit(
418424
: originalFileName;
419425
});
420426

421-
if (additionalDependencies) {
427+
if (additionalDependencies.length > 0) {
422428
additionalDependencies.forEach(addDependency);
423429
}
424430

@@ -434,12 +440,13 @@ function getEmit(
434440
const outputFile = outputFiles
435441
.filter(file => file.name.match(constants.jsJsx))
436442
.pop();
437-
const outputText = outputFile ? outputFile.text : undefined;
443+
const outputText = outputFile === undefined ? undefined : outputFile.text;
438444

439445
const sourceMapFile = outputFiles
440446
.filter(file => file.name.match(constants.jsJsxMap))
441447
.pop();
442-
const sourceMapText = sourceMapFile ? sourceMapFile.text : undefined;
448+
const sourceMapText =
449+
sourceMapFile === undefined ? undefined : sourceMapFile.text;
443450

444451
return { outputText, sourceMapText };
445452
}

src/instances.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,10 @@ function successfulTypeScriptInstance(
222222
}
223223

224224
// if allowJs is set then we should accept js(x) files
225-
const scriptRegex = configParseResult.options.allowJs
226-
? /\.tsx?$|\.jsx?$/i
227-
: /\.tsx?$/i;
225+
const scriptRegex =
226+
configParseResult.options.allowJs === true
227+
? /\.tsx?$|\.jsx?$/i
228+
: /\.tsx?$/i;
228229

229230
const instance: TSInstance = (instances[loaderOptions.instance] = {
230231
compiler,
@@ -297,7 +298,7 @@ function successfulTypeScriptInstance(
297298

298299
export function getEmitOutput(instance: TSInstance, filePath: string) {
299300
const program = ensureProgram(instance);
300-
if (program) {
301+
if (program !== undefined) {
301302
const outputFiles: typescript.OutputFile[] = [];
302303
const writeFile = (
303304
fileName: string,
@@ -318,8 +319,9 @@ export function getEmitOutput(instance: TSInstance, filePath: string) {
318319
return outputFiles;
319320
} else {
320321
// Emit Javascript
321-
return instance.languageService!.getProgram()!.getSourceFile(filePath)
322-
? instance.languageService!.getEmitOutput(filePath).outputFiles
323-
: [];
322+
return instance.languageService!.getProgram()!.getSourceFile(filePath) ===
323+
undefined
324+
? []
325+
: instance.languageService!.getEmitOutput(filePath).outputFiles;
324326
}
325327
}

src/servicesHost.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -251,15 +251,16 @@ export function makeWatchHost(
251251
invokeDirectoryWatcher,
252252
updateRootFileNames: () => {
253253
instance.changedFilesList = false;
254-
if (instance.watchOfFilesAndCompilerOptions) {
254+
if (instance.watchOfFilesAndCompilerOptions !== undefined) {
255255
instance.watchOfFilesAndCompilerOptions.updateRootFileNames(
256256
getRootFileNames()
257257
);
258258
}
259259
},
260-
createProgram: projectReferences
261-
? createBuilderProgramWithReferences
262-
: compiler.createAbstractBuilder
260+
createProgram:
261+
projectReferences === undefined
262+
? compiler.createAbstractBuilder
263+
: createBuilderProgramWithReferences
263264
};
264265
return watchHost;
265266

@@ -303,7 +304,7 @@ export function makeWatchHost(
303304
fileName: string,
304305
eventKind?: typescript.FileWatcherEventKind
305306
) {
306-
if (callbacks) {
307+
if (callbacks !== undefined) {
307308
// The array copy is made to ensure that even if one of the callback removes the callbacks,
308309
// we dont miss any callbacks following it
309310
const cbs = callbacks.slice();
@@ -352,16 +353,16 @@ export function makeWatchHost(
352353
): typescript.FileWatcher {
353354
file = path.normalize(file);
354355
const existing = callbacks[file];
355-
if (existing) {
356-
existing.push(callback);
357-
} else {
356+
if (existing === undefined) {
358357
callbacks[file] = [callback];
358+
} else {
359+
existing.push(callback);
359360
}
360361
return {
361362
close: () => {
362363
// tslint:disable-next-line:no-shadowed-variable
363364
const existing = callbacks[file];
364-
if (existing) {
365+
if (existing !== undefined) {
365366
unorderedRemoveItem(existing, callback);
366367
}
367368
}
@@ -383,7 +384,7 @@ export function makeWatchHost(
383384
) {
384385
return createWatcher(
385386
fileName,
386-
recursive ? watchedDirectoriesRecursive : watchedDirectories,
387+
recursive === true ? watchedDirectoriesRecursive : watchedDirectories,
387388
callback
388389
);
389390
}
@@ -489,7 +490,7 @@ function resolveModuleName(
489490
)
490491
: originalFileName;
491492

492-
if (resolvedFileName.match(scriptRegex)) {
493+
if (resolvedFileName.match(scriptRegex) !== null) {
493494
resolutionResult = { resolvedFileName, originalFileName };
494495
}
495496
// tslint:disable-next-line:no-empty

src/utils.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,17 @@ export function formatErrors(
4848
merge: { file?: string; module?: WebpackModule },
4949
context: string
5050
): WebpackError[] {
51-
return diagnostics
52-
? diagnostics
51+
return diagnostics === undefined
52+
? []
53+
: diagnostics
5354
.filter(diagnostic => {
5455
if (loaderOptions.ignoreDiagnostics.indexOf(diagnostic.code) !== -1) {
5556
return false;
5657
}
57-
if (loaderOptions.reportFiles.length > 0 && diagnostic.file) {
58+
if (
59+
loaderOptions.reportFiles.length > 0 &&
60+
diagnostic.file !== undefined
61+
) {
5862
const relativeFileName = path.relative(
5963
context,
6064
diagnostic.file.fileName
@@ -104,8 +108,7 @@ export function formatErrors(
104108
);
105109

106110
return Object.assign(error, merge) as WebpackError;
107-
})
108-
: [];
111+
});
109112
}
110113

111114
export function readFile(
@@ -140,7 +143,7 @@ export function appendSuffixIfMatch(
140143
): string {
141144
if (patterns.length > 0) {
142145
for (const regexp of patterns) {
143-
if (filePath.match(regexp)) {
146+
if (filePath.match(regexp) !== null) {
144147
return filePath + suffix;
145148
}
146149
}
@@ -367,7 +370,7 @@ export function getAndCacheOutputJSFileName(
367370
projectReference
368371
);
369372

370-
if (file) {
373+
if (file !== undefined) {
371374
file.projectReference = file.projectReference || {
372375
project: projectReference
373376
};

src/watch-run.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function makeWatchRun(instance: TSInstance) {
2222
for (const [filePath, date] of times) {
2323
if (
2424
date > (lastTimes.get(filePath) || startTime) &&
25-
filePath.match(constants.tsTsxJsJsxRegex)
25+
filePath.match(constants.tsTsxJsJsxRegex) !== null
2626
) {
2727
continue;
2828
}
@@ -36,8 +36,8 @@ export function makeWatchRun(instance: TSInstance) {
3636
// (skip @types/* and modules with typings)
3737
for (const filePath of instance.files.keys()) {
3838
if (
39-
filePath.match(constants.dtsDtsxOrDtsDtsxMapRegex) &&
40-
!filePath.match(constants.nodeModules)
39+
filePath.match(constants.dtsDtsxOrDtsDtsxMapRegex) !== null &&
40+
filePath.match(constants.nodeModules) === null
4141
) {
4242
updateFile(instance, filePath);
4343
}
@@ -56,7 +56,7 @@ function updateFile(instance: TSInstance, filePath: string) {
5656
file.version++;
5757
instance.version!++;
5858
instance.modifiedFiles!.set(nFilePath, file);
59-
if (instance.watchHost) {
59+
if (instance.watchHost !== undefined) {
6060
instance.watchHost.invokeFileWatcher(
6161
nFilePath,
6262
instance.compiler.FileWatcherEventKind.Changed

0 commit comments

Comments
 (0)