Skip to content

Commit 883b8d9

Browse files
committed
Merge pull request #5471 from Microsoft/jsFileCompilation
Compilation of Js Files
2 parents 7d6100b + a19307d commit 883b8d9

File tree

1,006 files changed

+7669
-2715
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,006 files changed

+7669
-2715
lines changed

.gitignore

-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
11
node_modules/
22
built/*
3-
tests/cases/*.js
4-
tests/cases/*/*.js
5-
tests/cases/*/*/*.js
6-
tests/cases/*/*/*/*.js
7-
tests/cases/*/*/*/*/*.js
8-
tests/cases/*.js.map
9-
tests/cases/*/*.js.map
10-
tests/cases/*/*/*.js.map
11-
tests/cases/*/*/*/*.js.map
12-
tests/cases/*/*/*/*/*.js.map
133
tests/cases/rwc/*
144
tests/cases/test262/*
155
tests/cases/perf/*

src/compiler/checker.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -11034,6 +11034,7 @@ namespace ts {
1103411034

1103511035
const symbol = getSymbolOfNode(node);
1103611036
const firstDeclaration = getDeclarationOfKind(symbol, node.kind);
11037+
1103711038
// Only type check the symbol once
1103811039
if (node === firstDeclaration) {
1103911040
checkFunctionOrConstructorSymbol(symbol);
@@ -12064,7 +12065,14 @@ namespace ts {
1206412065
const symbol = getSymbolOfNode(node);
1206512066
const localSymbol = node.localSymbol || symbol;
1206612067

12067-
const firstDeclaration = getDeclarationOfKind(localSymbol, node.kind);
12068+
// Since the javascript won't do semantic analysis like typescript,
12069+
// if the javascript file comes before the typescript file and both contain same name functions,
12070+
// checkFunctionOrConstructorSymbol wouldn't be called if we didnt ignore javascript function.
12071+
const firstDeclaration = forEach(localSymbol.declarations,
12072+
// Get first non javascript function declaration
12073+
declaration => declaration.kind === node.kind && !isSourceFileJavaScript(getSourceFile(declaration)) ?
12074+
declaration : undefined);
12075+
1206812076
// Only type check the symbol once
1206912077
if (node === firstDeclaration) {
1207012078
checkFunctionOrConstructorSymbol(localSymbol);

src/compiler/commandLineParser.ts

+32-17
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,11 @@ namespace ts {
279279
name: "forceConsistentCasingInFileNames",
280280
type: "boolean",
281281
description: Diagnostics.Disallow_inconsistently_cased_references_to_the_same_file
282+
},
283+
{
284+
name: "allowJs",
285+
type: "boolean",
286+
description: Diagnostics.Allow_javascript_files_to_be_compiled,
282287
}
283288
];
284289

@@ -474,15 +479,16 @@ namespace ts {
474479
* @param basePath A root directory to resolve relative path entries in the config
475480
* file to. e.g. outDir
476481
*/
477-
export function parseJsonConfigFileContent(json: any, host: ParseConfigHost, basePath: string): ParsedCommandLine {
478-
const { options, errors } = convertCompilerOptionsFromJson(json["compilerOptions"], basePath);
482+
export function parseJsonConfigFileContent(json: any, host: ParseConfigHost, basePath: string, existingOptions: CompilerOptions = {}): ParsedCommandLine {
483+
const { options: optionsFromJsonConfigFile, errors } = convertCompilerOptionsFromJson(json["compilerOptions"], basePath);
479484

485+
const options = extend(existingOptions, optionsFromJsonConfigFile);
480486
return {
481487
options,
482488
fileNames: getFileNames(),
483489
errors
484490
};
485-
491+
486492
function getFileNames(): string[] {
487493
let fileNames: string[] = [];
488494
if (hasProperty(json, "files")) {
@@ -494,23 +500,32 @@ namespace ts {
494500
}
495501
}
496502
else {
503+
const filesSeen: Map<boolean> = {};
497504
const exclude = json["exclude"] instanceof Array ? map(<string[]>json["exclude"], normalizeSlashes) : undefined;
498-
const sysFiles = host.readDirectory(basePath, ".ts", exclude).concat(host.readDirectory(basePath, ".tsx", exclude));
499-
for (let i = 0; i < sysFiles.length; i++) {
500-
const name = sysFiles[i];
501-
if (fileExtensionIs(name, ".d.ts")) {
502-
const baseName = name.substr(0, name.length - ".d.ts".length);
503-
if (!contains(sysFiles, baseName + ".tsx") && !contains(sysFiles, baseName + ".ts")) {
504-
fileNames.push(name);
505+
const supportedExtensions = getSupportedExtensions(options);
506+
Debug.assert(indexOf(supportedExtensions, ".ts") < indexOf(supportedExtensions, ".d.ts"), "Changed priority of extensions to pick");
507+
508+
// Get files of supported extensions in their order of resolution
509+
for (const extension of supportedExtensions) {
510+
const filesInDirWithExtension = host.readDirectory(basePath, extension, exclude);
511+
for (const fileName of filesInDirWithExtension) {
512+
// .ts extension would read the .d.ts extension files too but since .d.ts is lower priority extension,
513+
// lets pick them when its turn comes up
514+
if (extension === ".ts" && fileExtensionIs(fileName, ".d.ts")) {
515+
continue;
505516
}
506-
}
507-
else if (fileExtensionIs(name, ".ts")) {
508-
if (!contains(sysFiles, name + "x")) {
509-
fileNames.push(name);
517+
518+
// If this is one of the output extension (which would be .d.ts and .js if we are allowing compilation of js files)
519+
// do not include this file if we included .ts or .tsx file with same base name as it could be output of the earlier compilation
520+
if (extension === ".d.ts" || (options.allowJs && contains(supportedJavascriptExtensions, extension))) {
521+
const baseName = fileName.substr(0, fileName.length - extension.length);
522+
if (hasProperty(filesSeen, baseName + ".ts") || hasProperty(filesSeen, baseName + ".tsx")) {
523+
continue;
524+
}
510525
}
511-
}
512-
else {
513-
fileNames.push(name);
526+
527+
filesSeen[fileName] = true;
528+
fileNames.push(fileName);
514529
}
515530
}
516531
}

src/compiler/core.ts

+13-8
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,8 @@ namespace ts {
297297
return <T>result;
298298
}
299299

300-
export function extend<T1, T2>(first: Map<T1>, second: Map<T2>): Map<T1 & T2> {
301-
const result: Map<T1 & T2> = {};
300+
export function extend<T1 extends Map<{}>, T2 extends Map<{}>>(first: T1 , second: T2): T1 & T2 {
301+
const result: T1 & T2 = <any>{};
302302
for (const id in first) {
303303
(result as any)[id] = first[id];
304304
}
@@ -714,7 +714,7 @@ namespace ts {
714714
}
715715

716716
export function getBaseFileName(path: string) {
717-
if (!path) {
717+
if (path === undefined) {
718718
return undefined;
719719
}
720720
const i = path.lastIndexOf(directorySeparator);
@@ -738,13 +738,18 @@ namespace ts {
738738
/**
739739
* List of supported extensions in order of file resolution precedence.
740740
*/
741-
export const supportedExtensions = [".ts", ".tsx", ".d.ts"];
742-
export const supportedJsExtensions = supportedExtensions.concat(".js", ".jsx");
741+
export const supportedTypeScriptExtensions = [".ts", ".tsx", ".d.ts"];
742+
export const supportedJavascriptExtensions = [".js", ".jsx"];
743+
const allSupportedExtensions = supportedTypeScriptExtensions.concat(supportedJavascriptExtensions);
743744

744-
export function isSupportedSourceFileName(fileName: string) {
745+
export function getSupportedExtensions(options?: CompilerOptions): string[] {
746+
return options && options.allowJs ? allSupportedExtensions : supportedTypeScriptExtensions;
747+
}
748+
749+
export function isSupportedSourceFileName(fileName: string, compilerOptions?: CompilerOptions) {
745750
if (!fileName) { return false; }
746751

747-
for (const extension of supportedExtensions) {
752+
for (const extension of getSupportedExtensions(compilerOptions)) {
748753
if (fileExtensionIs(fileName, extension)) {
749754
return true;
750755
}
@@ -855,4 +860,4 @@ namespace ts {
855860
}
856861
return copiedList;
857862
}
858-
}
863+
}

0 commit comments

Comments
 (0)