Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

start introducing strict-boolean-expressions #855

Merged
merged 4 commits into from
Oct 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 5.2.2

* [feat: Micro-optimizations](https://github.com/TypeStrong/ts-loader/pull/855) - thanks @johnnyreilly

## 5.2.1

* [feat: Lists typescript as a peer dependency](https://github.com/TypeStrong/ts-loader/pull/841) - thanks @arcanis!
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": "ts-loader",
"version": "5.2.1",
"version": "5.2.2",
"description": "TypeScript loader for webpack",
"main": "index.js",
"types": "dist/types/index.d.ts",
Expand Down
41 changes: 22 additions & 19 deletions src/after-compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ function provideCompilerOptionDiagnosticErrorsToWebpack(
const { languageService, loaderOptions, compiler, program } = instance;

const errorsToAdd = formatErrors(
program
? program.getOptionsDiagnostics()
: languageService!.getCompilerOptionsDiagnostics(),
program === undefined
? languageService!.getCompilerOptionsDiagnostics()
: program.getOptionsDiagnostics(),
loaderOptions,
instance.colors,
compiler,
Expand Down Expand Up @@ -177,34 +177,37 @@ function provideErrorsToWebpack(
otherFiles
} = instance;

const filePathRegex = !!compilerOptions.checkJs
? constants.dtsTsTsxJsJsxRegex
: constants.dtsTsTsxRegex;
const filePathRegex =
compilerOptions.checkJs === true
? constants.dtsTsTsxJsJsxRegex
: constants.dtsTsTsxRegex;

for (const filePath of filesToCheckForErrors.keys()) {
if (!filePath.match(filePathRegex)) {
if (filePath.match(filePathRegex) === null) {
continue;
}

const sourceFile = program && program.getSourceFile(filePath);
const sourceFile =
program === undefined ? undefined : program.getSourceFile(filePath);

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

const errors = program
? [
...program.getSyntacticDiagnostics(sourceFile),
...program.getSemanticDiagnostics(sourceFile)
]
: [
...languageService!.getSyntacticDiagnostics(filePath),
...languageService!.getSemanticDiagnostics(filePath)
];
const errors =
program === undefined
? [
...languageService!.getSyntacticDiagnostics(filePath),
...languageService!.getSemanticDiagnostics(filePath)
]
: [
...program.getSyntacticDiagnostics(sourceFile),
...program.getSemanticDiagnostics(sourceFile)
];
if (errors.length > 0) {
const fileWithError = files.get(filePath) || otherFiles.get(filePath);
filesWithErrors.set(filePath, fileWithError!);
Expand Down Expand Up @@ -255,7 +258,7 @@ function provideDeclarationFilesToWebpack(
compilation: WebpackCompilation
) {
for (const filePath of filesToCheckForErrors.keys()) {
if (!filePath.match(constants.tsTsxRegex)) {
if (filePath.match(constants.tsTsxRegex) === null) {
continue;
}

Expand Down
5 changes: 4 additions & 1 deletion src/compilerSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ export function getCompiler(loaderOptions: LoaderOptions, log: logger.Logger) {
}`;
compilerCompatible = false;
if (loaderOptions.compiler === 'typescript') {
if (compiler!.version && semver.gte(compiler!.version, '2.4.1')) {
if (
compiler!.version !== undefined &&
semver.gte(compiler!.version, '2.4.1')
) {
// don't log yet in this case, if a tsconfig.json exists we want to combine the message
compilerCompatible = true;
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function findConfigFile(
// If `configFile` is a relative path, resolve it.
// We define a relative path as: starts with
// one or two dots + a common directory delimiter
if (configFile.match(/^\.\.?(\/|\\)/)) {
if (configFile.match(/^\.\.?(\/|\\)/) !== null) {
const resolvedPath = path.resolve(requestDirPath, configFile);
return compiler.sys.fileExists(resolvedPath) ? resolvedPath : undefined;

Expand Down
29 changes: 18 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const loaderOptionsCache: LoaderOptionsCache = {};
* The entry point for ts-loader
*/
function loader(this: Webpack, contents: string) {
// tslint:disable-next-line:no-unused-expression
// tslint:disable-next-line:no-unused-expression strict-boolean-expressions
this.cacheable && this.cacheable();
const callback = this.async();
const options = getLoaderOptions(this);
Expand Down Expand Up @@ -80,7 +80,7 @@ function successLoader(
),
path.relative(loaderContext.rootContext, filePath)
];
if (referencedProject.commandLine.options.outFile) {
if (referencedProject.commandLine.options.outFile !== undefined) {
throw new Error(
`The referenced project at ${relativeProjectConfigPath} is using ` +
`the outFile' option, which is not supported with ts-loader.`
Expand Down Expand Up @@ -184,7 +184,7 @@ function makeSourceMapAndFinish(
);

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

if (loaderOptions.context && !path.isAbsolute(loaderOptions.context)) {
if (
loaderOptions.context !== undefined &&
!path.isAbsolute(loaderOptions.context)
) {
throw new Error(
`Option 'context' has to be an absolute path. Given '${
loaderOptions.context
Expand Down Expand Up @@ -342,7 +345,7 @@ function updateFileInCache(
instance.otherFiles.delete(filePath);
instance.files.set(filePath, file);
} else {
if (instance.watchHost) {
if (instance.watchHost !== undefined) {
fileWatcherEventKind = instance.compiler.FileWatcherEventKind.Created;
}
file = { version: 0 };
Expand All @@ -351,20 +354,23 @@ function updateFileInCache(
instance.changedFilesList = true;
}

if (instance.watchHost && contents === undefined) {
if (instance.watchHost !== undefined && contents === undefined) {
fileWatcherEventKind = instance.compiler.FileWatcherEventKind.Deleted;
}

if (file.text !== contents) {
file.version++;
file.text = contents;
instance.version!++;
if (instance.watchHost && fileWatcherEventKind === undefined) {
if (
instance.watchHost !== undefined &&
fileWatcherEventKind === undefined
) {
fileWatcherEventKind = instance.compiler.FileWatcherEventKind.Changed;
}
}

if (instance.watchHost && fileWatcherEventKind !== undefined) {
if (instance.watchHost !== undefined && fileWatcherEventKind !== undefined) {
instance.hasUnaccountedModifiedFiles = true;
instance.watchHost.invokeFileWatcher(filePath, fileWatcherEventKind);
instance.watchHost.invokeDirectoryWatcher(path.dirname(filePath), filePath);
Expand Down Expand Up @@ -418,7 +424,7 @@ function getEmit(
: originalFileName;
});

if (additionalDependencies) {
if (additionalDependencies.length > 0) {
additionalDependencies.forEach(addDependency);
}

Expand All @@ -434,12 +440,13 @@ function getEmit(
const outputFile = outputFiles
.filter(file => file.name.match(constants.jsJsx))
.pop();
const outputText = outputFile ? outputFile.text : undefined;
const outputText = outputFile === undefined ? undefined : outputFile.text;

const sourceMapFile = outputFiles
.filter(file => file.name.match(constants.jsJsxMap))
.pop();
const sourceMapText = sourceMapFile ? sourceMapFile.text : undefined;
const sourceMapText =
sourceMapFile === undefined ? undefined : sourceMapFile.text;

return { outputText, sourceMapText };
}
Expand Down
16 changes: 9 additions & 7 deletions src/instances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,10 @@ function successfulTypeScriptInstance(
}

// if allowJs is set then we should accept js(x) files
const scriptRegex = configParseResult.options.allowJs
? /\.tsx?$|\.jsx?$/i
: /\.tsx?$/i;
const scriptRegex =
configParseResult.options.allowJs === true
? /\.tsx?$|\.jsx?$/i
: /\.tsx?$/i;

const instance: TSInstance = (instances[loaderOptions.instance] = {
compiler,
Expand Down Expand Up @@ -297,7 +298,7 @@ function successfulTypeScriptInstance(

export function getEmitOutput(instance: TSInstance, filePath: string) {
const program = ensureProgram(instance);
if (program) {
if (program !== undefined) {
const outputFiles: typescript.OutputFile[] = [];
const writeFile = (
fileName: string,
Expand All @@ -318,8 +319,9 @@ export function getEmitOutput(instance: TSInstance, filePath: string) {
return outputFiles;
} else {
// Emit Javascript
return instance.languageService!.getProgram()!.getSourceFile(filePath)
? instance.languageService!.getEmitOutput(filePath).outputFiles
: [];
return instance.languageService!.getProgram()!.getSourceFile(filePath) ===
undefined
? []
: instance.languageService!.getEmitOutput(filePath).outputFiles;
}
}
23 changes: 12 additions & 11 deletions src/servicesHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,16 @@ export function makeWatchHost(
invokeDirectoryWatcher,
updateRootFileNames: () => {
instance.changedFilesList = false;
if (instance.watchOfFilesAndCompilerOptions) {
if (instance.watchOfFilesAndCompilerOptions !== undefined) {
instance.watchOfFilesAndCompilerOptions.updateRootFileNames(
getRootFileNames()
);
}
},
createProgram: projectReferences
? createBuilderProgramWithReferences
: compiler.createAbstractBuilder
createProgram:
projectReferences === undefined
? compiler.createAbstractBuilder
: createBuilderProgramWithReferences
};
return watchHost;

Expand Down Expand Up @@ -303,7 +304,7 @@ export function makeWatchHost(
fileName: string,
eventKind?: typescript.FileWatcherEventKind
) {
if (callbacks) {
if (callbacks !== undefined) {
// The array copy is made to ensure that even if one of the callback removes the callbacks,
// we dont miss any callbacks following it
const cbs = callbacks.slice();
Expand Down Expand Up @@ -352,16 +353,16 @@ export function makeWatchHost(
): typescript.FileWatcher {
file = path.normalize(file);
const existing = callbacks[file];
if (existing) {
existing.push(callback);
} else {
if (existing === undefined) {
callbacks[file] = [callback];
} else {
existing.push(callback);
}
return {
close: () => {
// tslint:disable-next-line:no-shadowed-variable
const existing = callbacks[file];
if (existing) {
if (existing !== undefined) {
unorderedRemoveItem(existing, callback);
}
}
Expand All @@ -383,7 +384,7 @@ export function makeWatchHost(
) {
return createWatcher(
fileName,
recursive ? watchedDirectoriesRecursive : watchedDirectories,
recursive === true ? watchedDirectoriesRecursive : watchedDirectories,
callback
);
}
Expand Down Expand Up @@ -489,7 +490,7 @@ function resolveModuleName(
)
: originalFileName;

if (resolvedFileName.match(scriptRegex)) {
if (resolvedFileName.match(scriptRegex) !== null) {
resolutionResult = { resolvedFileName, originalFileName };
}
// tslint:disable-next-line:no-empty
Expand Down
17 changes: 10 additions & 7 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,17 @@ export function formatErrors(
merge: { file?: string; module?: WebpackModule },
context: string
): WebpackError[] {
return diagnostics
? diagnostics
return diagnostics === undefined
? []
: diagnostics
.filter(diagnostic => {
if (loaderOptions.ignoreDiagnostics.indexOf(diagnostic.code) !== -1) {
return false;
}
if (loaderOptions.reportFiles.length > 0 && diagnostic.file) {
if (
loaderOptions.reportFiles.length > 0 &&
diagnostic.file !== undefined
) {
const relativeFileName = path.relative(
context,
diagnostic.file.fileName
Expand Down Expand Up @@ -104,8 +108,7 @@ export function formatErrors(
);

return Object.assign(error, merge) as WebpackError;
})
: [];
});
}

export function readFile(
Expand Down Expand Up @@ -140,7 +143,7 @@ export function appendSuffixIfMatch(
): string {
if (patterns.length > 0) {
for (const regexp of patterns) {
if (filePath.match(regexp)) {
if (filePath.match(regexp) !== null) {
return filePath + suffix;
}
}
Expand Down Expand Up @@ -367,7 +370,7 @@ export function getAndCacheOutputJSFileName(
projectReference
);

if (file) {
if (file !== undefined) {
file.projectReference = file.projectReference || {
project: projectReference
};
Expand Down
8 changes: 4 additions & 4 deletions src/watch-run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function makeWatchRun(instance: TSInstance) {
for (const [filePath, date] of times) {
if (
date > (lastTimes.get(filePath) || startTime) &&
filePath.match(constants.tsTsxJsJsxRegex)
filePath.match(constants.tsTsxJsJsxRegex) !== null
) {
continue;
}
Expand All @@ -36,8 +36,8 @@ export function makeWatchRun(instance: TSInstance) {
// (skip @types/* and modules with typings)
for (const filePath of instance.files.keys()) {
if (
filePath.match(constants.dtsDtsxOrDtsDtsxMapRegex) &&
!filePath.match(constants.nodeModules)
filePath.match(constants.dtsDtsxOrDtsDtsxMapRegex) !== null &&
filePath.match(constants.nodeModules) === null
) {
updateFile(instance, filePath);
}
Expand All @@ -56,7 +56,7 @@ function updateFile(instance: TSInstance, filePath: string) {
file.version++;
instance.version!++;
instance.modifiedFiles!.set(nFilePath, file);
if (instance.watchHost) {
if (instance.watchHost !== undefined) {
instance.watchHost.invokeFileWatcher(
nFilePath,
instance.compiler.FileWatcherEventKind.Changed
Expand Down