Skip to content

Commit

Permalink
Revert "Convert Extension to a string enum (#16425)"
Browse files Browse the repository at this point in the history
This reverts commit 09321b3.
  • Loading branch information
mhegazy committed Jun 15, 2017
1 parent 096f8cc commit 8b87d79
Show file tree
Hide file tree
Showing 16 changed files with 70 additions and 55 deletions.
36 changes: 25 additions & 11 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2131,13 +2131,13 @@ namespace ts {
export function getScriptKindFromFileName(fileName: string): ScriptKind {
const ext = fileName.substr(fileName.lastIndexOf("."));
switch (ext.toLowerCase()) {
case Extension.Js:
case ".js":
return ScriptKind.JS;
case Extension.Jsx:
case ".jsx":
return ScriptKind.JSX;
case Extension.Ts:
case ".ts":
return ScriptKind.TS;
case Extension.Tsx:
case ".tsx":
return ScriptKind.TSX;
default:
return ScriptKind.Unknown;
Expand All @@ -2147,18 +2147,18 @@ namespace ts {
/**
* List of supported extensions in order of file resolution precedence.
*/
export const supportedTypeScriptExtensions = [Extension.Ts, Extension.Tsx, Extension.Dts];
export const supportedTypeScriptExtensions = [".ts", ".tsx", ".d.ts"];
/** Must have ".d.ts" first because if ".ts" goes first, that will be detected as the extension instead of ".d.ts". */
export const supportedTypescriptExtensionsForExtractExtension = [Extension.Dts, Extension.Ts, Extension.Tsx];
export const supportedJavascriptExtensions = [Extension.Js, Extension.Jsx];
export const supportedTypescriptExtensionsForExtractExtension = [".d.ts", ".ts", ".tsx"];
export const supportedJavascriptExtensions = [".js", ".jsx"];
const allSupportedExtensions = supportedTypeScriptExtensions.concat(supportedJavascriptExtensions);

export function getSupportedExtensions(options?: CompilerOptions, extraFileExtensions?: JsFileExtensionInfo[]): string[] {
const needAllExtensions = options && options.allowJs;
if (!extraFileExtensions || extraFileExtensions.length === 0 || !needAllExtensions) {
return needAllExtensions ? allSupportedExtensions : supportedTypeScriptExtensions;
}
const extensions: string[] = allSupportedExtensions.slice(0);
const extensions = allSupportedExtensions.slice(0);
for (const extInfo of extraFileExtensions) {
if (extensions.indexOf(extInfo.extension) === -1) {
extensions.push(extInfo.extension);
Expand Down Expand Up @@ -2237,7 +2237,7 @@ namespace ts {
}
}

const extensionsToRemove = [Extension.Dts, Extension.Ts, Extension.Js, Extension.Tsx, Extension.Jsx];
const extensionsToRemove = [".d.ts", ".ts", ".js", ".tsx", ".jsx"];
export function removeFileExtension(path: string): string {
for (const ext of extensionsToRemove) {
const extensionless = tryRemoveExtension(path, ext);
Expand Down Expand Up @@ -2491,7 +2491,7 @@ namespace ts {

/** True if an extension is one of the supported TypeScript extensions. */
export function extensionIsTypeScript(ext: Extension): boolean {
return ext === Extension.Ts || ext === Extension.Tsx || ext === Extension.Dts;
return ext <= Extension.LastTypeScriptExtension;
}

/**
Expand All @@ -2506,7 +2506,21 @@ namespace ts {
Debug.fail(`File ${path} has unknown extension.`);
}
export function tryGetExtensionFromPath(path: string): Extension | undefined {
return find(supportedTypescriptExtensionsForExtractExtension, e => fileExtensionIs(path, e)) || find(supportedJavascriptExtensions, e => fileExtensionIs(path, e));
if (fileExtensionIs(path, ".d.ts")) {
return Extension.Dts;
}
if (fileExtensionIs(path, ".ts")) {
return Extension.Ts;
}
if (fileExtensionIs(path, ".tsx")) {
return Extension.Tsx;
}
if (fileExtensionIs(path, ".js")) {
return Extension.Js;
}
if (fileExtensionIs(path, ".jsx")) {
return Extension.Jsx;
}
}

export function isCheckJsEnabledForFile(sourceFile: SourceFile, compilerOptions: CompilerOptions) {
Expand Down
10 changes: 5 additions & 5 deletions src/compiler/moduleNameResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -815,15 +815,15 @@ namespace ts {

switch (extensions) {
case Extensions.DtsOnly:
return tryExtension(Extension.Dts);
return tryExtension(".d.ts", Extension.Dts);
case Extensions.TypeScript:
return tryExtension(Extension.Ts) || tryExtension(Extension.Tsx) || tryExtension(Extension.Dts);
return tryExtension(".ts", Extension.Ts) || tryExtension(".tsx", Extension.Tsx) || tryExtension(".d.ts", Extension.Dts);
case Extensions.JavaScript:
return tryExtension(Extension.Js) || tryExtension(Extension.Jsx);
return tryExtension(".js", Extension.Js) || tryExtension(".jsx", Extension.Jsx);
}

function tryExtension(extension: Extension): Resolved | undefined {
const path = tryFile(candidate + extension, failedLookupLocations, onlyRecordFailures, state);
function tryExtension(ext: string, extension: Extension): Resolved | undefined {
const path = tryFile(candidate + ext, failedLookupLocations, onlyRecordFailures, state);
return path && { path, extension };
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ namespace ts {
sourceFile.languageVersion = languageVersion;
sourceFile.fileName = normalizePath(fileName);
sourceFile.languageVariant = getLanguageVariant(scriptKind);
sourceFile.isDeclarationFile = fileExtensionIs(sourceFile.fileName, Extension.Dts);
sourceFile.isDeclarationFile = fileExtensionIs(sourceFile.fileName, ".d.ts");
sourceFile.scriptKind = scriptKind;

return sourceFile;
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1495,7 +1495,7 @@ namespace ts {
}

const sourceFileWithAddedExtension = forEach(supportedExtensions, extension => getSourceFile(fileName + extension));
if (fail && !sourceFileWithAddedExtension) fail(Diagnostics.File_0_not_found, fileName + Extension.Ts);
if (fail && !sourceFileWithAddedExtension) fail(Diagnostics.File_0_not_found, fileName + ".ts");
return sourceFileWithAddedExtension;
}
}
Expand Down
13 changes: 7 additions & 6 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3883,12 +3883,13 @@ namespace ts {
extension: Extension;
}

export const enum Extension {
Ts = ".ts",
Tsx = ".tsx",
Dts = ".d.ts",
Js = ".js",
Jsx = ".jsx"
export enum Extension {
Ts,
Tsx,
Dts,
Js,
Jsx,
LastTypeScriptExtension = Dts
}

export interface ResolvedModuleWithFailedLookupLocations {
Expand Down
14 changes: 7 additions & 7 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2501,7 +2501,7 @@ namespace ts {
const path = outputDir
? getSourceFilePathInNewDir(sourceFile, host, outputDir)
: sourceFile.fileName;
return removeFileExtension(path) + Extension.Dts;
return removeFileExtension(path) + ".d.ts";
}

export interface EmitFileNames {
Expand Down Expand Up @@ -2560,7 +2560,7 @@ namespace ts {
if (sourceFiles.length) {
const jsFilePath = options.outFile || options.out;
const sourceMapFilePath = getSourceMapFilePath(jsFilePath, options);
const declarationFilePath = options.declaration ? removeFileExtension(jsFilePath) + Extension.Dts : "";
const declarationFilePath = options.declaration ? removeFileExtension(jsFilePath) + ".d.ts" : "";
action({ jsFilePath, sourceMapFilePath, declarationFilePath }, createBundle(sourceFiles), emitOnlyDtsFiles);
}
}
Expand All @@ -2581,19 +2581,19 @@ namespace ts {
// JavaScript files are always LanguageVariant.JSX, as JSX syntax is allowed in .js files also.
// So for JavaScript files, '.jsx' is only emitted if the input was '.jsx', and JsxEmit.Preserve.
// For TypeScript, the only time to emit with a '.jsx' extension, is on JSX input, and JsxEmit.Preserve
function getOutputExtension(sourceFile: SourceFile, options: CompilerOptions): Extension {
function getOutputExtension(sourceFile: SourceFile, options: CompilerOptions): string {
if (options.jsx === JsxEmit.Preserve) {
if (isSourceFileJavaScript(sourceFile)) {
if (fileExtensionIs(sourceFile.fileName, Extension.Jsx)) {
return Extension.Jsx;
if (fileExtensionIs(sourceFile.fileName, ".jsx")) {
return ".jsx";
}
}
else if (sourceFile.languageVariant === LanguageVariant.JSX) {
// TypeScript source file preserving JSX syntax
return Extension.Jsx;
return ".jsx";
}
}
return Extension.Js;
return ".js";
}

export function getSourceFilePathInNewDir(sourceFile: SourceFile, host: EmitHost, newDirPath: string) {
Expand Down
2 changes: 1 addition & 1 deletion src/harness/compilerRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class CompilerBaselineRunner extends RunnerBase {
}

lastUnit = units[units.length - 1];
hasNonDtsFiles = ts.forEach(units, unit => !ts.fileExtensionIs(unit.name, ts.Extension.Dts));
hasNonDtsFiles = ts.forEach(units, unit => !ts.fileExtensionIs(unit.name, ".d.ts"));
// We need to assemble the list of input files for the compiler and other related files on the 'filesystem' (ie in a multi-file test)
// If the last file in a test uses require or a triple slash reference we'll assume all other files will be brought in via references,
// otherwise, assume all files are just meant to be in the same compilation session without explicit references to one another.
Expand Down
4 changes: 2 additions & 2 deletions src/harness/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1467,7 +1467,7 @@ namespace FourSlash {
let baselineFile = this.testData.globalOptions[metadataOptionNames.baselineFile];
if (!baselineFile) {
baselineFile = this.activeFile.fileName.replace(this.basePath + "/breakpointValidation", "bpSpan");
baselineFile = baselineFile.replace(ts.Extension.Ts, ".baseline");
baselineFile = baselineFile.replace(".ts", ".baseline");

}
Harness.Baseline.runBaseline(
Expand Down Expand Up @@ -1537,7 +1537,7 @@ namespace FourSlash {
public baselineQuickInfo() {
let baselineFile = this.testData.globalOptions[metadataOptionNames.baselineFile];
if (!baselineFile) {
baselineFile = ts.getBaseFileName(this.activeFile.fileName).replace(ts.Extension.Ts, ".baseline");
baselineFile = ts.getBaseFileName(this.activeFile.fileName).replace(".ts", ".baseline");
}

Harness.Baseline.runBaseline(
Expand Down
18 changes: 9 additions & 9 deletions src/harness/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1249,7 +1249,7 @@ namespace Harness {
sourceFileName = outFile;
}

const dTsFileName = ts.removeFileExtension(sourceFileName) + ts.Extension.Dts;
const dTsFileName = ts.removeFileExtension(sourceFileName) + ".d.ts";

return ts.forEach(result.declFilesCode, declFile => declFile.fileName === dTsFileName ? declFile : undefined);
}
Expand Down Expand Up @@ -1465,7 +1465,7 @@ namespace Harness {
// When calling this function from rwc-runner, the baselinePath will have no extension.
// As rwc test- file is stored in json which ".json" will get stripped off.
// When calling this function from compiler-runner, the baselinePath will then has either ".ts" or ".tsx" extension
const outputFileName = ts.endsWith(baselinePath, ts.Extension.Ts) || ts.endsWith(baselinePath, ts.Extension.Tsx) ?
const outputFileName = ts.endsWith(baselinePath, ".ts") || ts.endsWith(baselinePath, ".tsx") ?
baselinePath.replace(/\.tsx?/, fullExtension) : baselinePath.concat(fullExtension);
Harness.Baseline.runBaseline(outputFileName, () => fullBaseLine, opts);
}
Expand Down Expand Up @@ -1563,7 +1563,7 @@ namespace Harness {
}

// check js output
Harness.Baseline.runBaseline(baselinePath.replace(/\.tsx?/, ts.Extension.Js), () => {
Harness.Baseline.runBaseline(baselinePath.replace(/\.tsx?/, ".js"), () => {
let tsCode = "";
const tsSources = otherFiles.concat(toBeCompiled);
if (tsSources.length > 1) {
Expand Down Expand Up @@ -1651,22 +1651,22 @@ namespace Harness {
}

export function isTS(fileName: string) {
return ts.endsWith(fileName, ts.Extension.Ts);
return ts.endsWith(fileName, ".ts");
}

export function isTSX(fileName: string) {
return ts.endsWith(fileName, ts.Extension.Tsx);
return ts.endsWith(fileName, ".tsx");
}

export function isDTS(fileName: string) {
return ts.endsWith(fileName, ts.Extension.Dts);
return ts.endsWith(fileName, ".d.ts");
}

export function isJS(fileName: string) {
return ts.endsWith(fileName, ts.Extension.Js);
return ts.endsWith(fileName, ".js");
}
export function isJSX(fileName: string) {
return ts.endsWith(fileName, ts.Extension.Jsx);
return ts.endsWith(fileName, ".jsx");
}

export function isJSMap(fileName: string) {
Expand Down Expand Up @@ -1973,7 +1973,7 @@ namespace Harness {
export function isDefaultLibraryFile(filePath: string): boolean {
// We need to make sure that the filePath is prefixed with "lib." not just containing "lib." and end with ".d.ts"
const fileName = ts.getBaseFileName(ts.normalizeSlashes(filePath));
return ts.startsWith(fileName, "lib.") && ts.endsWith(fileName, ts.Extension.Dts);
return ts.startsWith(fileName, "lib.") && ts.endsWith(fileName, ".d.ts");
}

export function isBuiltFile(filePath: string): boolean {
Expand Down
8 changes: 4 additions & 4 deletions src/harness/projectsRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,8 @@ class ProjectRunner extends RunnerBase {
// we need to instead create files that can live in the project reference folder
// but make sure extension of these files matches with the fileName the compiler asked to write
diskRelativeName = "diskFile" + nonSubfolderDiskFiles +
(Harness.Compiler.isDTS(fileName) ? ts.Extension.Dts :
Harness.Compiler.isJS(fileName) ? ts.Extension.Js : ".js.map");
(Harness.Compiler.isDTS(fileName) ? ".d.ts" :
Harness.Compiler.isJS(fileName) ? ".js" : ".js.map");
nonSubfolderDiskFiles++;
}

Expand Down Expand Up @@ -386,14 +386,14 @@ class ProjectRunner extends RunnerBase {
emitOutputFilePathWithoutExtension = ts.removeFileExtension(sourceFile.fileName);
}

const outputDtsFileName = emitOutputFilePathWithoutExtension + ts.Extension.Dts;
const outputDtsFileName = emitOutputFilePathWithoutExtension + ".d.ts";
const file = findOutputDtsFile(outputDtsFileName);
if (file) {
allInputFiles.unshift(file);
}
}
else {
const outputDtsFileName = ts.removeFileExtension(compilerOptions.outFile || compilerOptions.out) + ts.Extension.Dts;
const outputDtsFileName = ts.removeFileExtension(compilerOptions.outFile || compilerOptions.out) + ".d.ts";
const outputDtsFile = findOutputDtsFile(outputDtsFileName);
if (!ts.contains(allInputFiles, outputDtsFile)) {
allInputFiles.unshift(outputDtsFile);
Expand Down
4 changes: 2 additions & 2 deletions src/harness/unittests/compileOnSave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ namespace ts.projectSystem {
const lines = ["var x = 1;", "var y = 2;"];
const path = "/a/app";
const f = {
path: path + ts.Extension.Ts,
path: path + ".ts",
content: lines.join(newLine)
};
const host = createServerHost([f], { newLine });
Expand All @@ -530,7 +530,7 @@ namespace ts.projectSystem {
command: "compileOnSaveEmitFile",
arguments: { file: f.path }
});
const emitOutput = host.readFile(path + ts.Extension.Js);
const emitOutput = host.readFile(path + ".js");
assert.equal(emitOutput, f.content + newLine, "content of emit output should be identical with the input + newline");
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/harness/unittests/moduleResolution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace ts {
if (!expected === !actual) {
if (expected) {
assert.isTrue(expected.resolvedFileName === actual.resolvedFileName, `'resolvedFileName': expected '${expected.resolvedFileName}' to be equal to '${actual.resolvedFileName}'`);
assert.isTrue(expected.extension === actual.extension, `'ext': expected '${expected.extension}' to be equal to '${actual.extension}'`);
assert.isTrue(expected.extension === actual.extension, `'ext': expected '${Extension[expected.extension]}' to be equal to '${Extension[actual.extension]}'`);
assert.isTrue(expected.isExternalLibraryImport === actual.isExternalLibraryImport, `'isExternalLibraryImport': expected '${expected.isExternalLibraryImport}' to be equal to '${actual.isExternalLibraryImport}'`);
}
return true;
Expand Down
4 changes: 2 additions & 2 deletions src/harness/unittests/transpile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace ts {

transpileOptions.reportDiagnostics = true;

justName = "transpile/" + name.replace(/[^a-z0-9\-. ]/ig, "") + (transpileOptions.compilerOptions.jsx ? Extension.Tsx : Extension.Ts);
justName = "transpile/" + name.replace(/[^a-z0-9\-. ]/ig, "") + (transpileOptions.compilerOptions.jsx ? ".tsx" : ".ts");
toBeCompiled = [{
unitName: transpileOptions.fileName,
content: input
Expand Down Expand Up @@ -88,7 +88,7 @@ namespace ts {
}

it("Correct output for " + justName, () => {
Harness.Baseline.runBaseline(justName.replace(/\.tsx?$/, ts.Extension.Js), () => {
Harness.Baseline.runBaseline(justName.replace(/\.tsx?$/, ".js"), () => {
if (transpileResult.outputText) {
return transpileResult.outputText;
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace ts.server {
result.jsx += 1;
break;
case ScriptKind.TS:
fileExtensionIs(info.fileName, Extension.Dts)
fileExtensionIs(info.fileName, ".d.ts")
? result.dts += 1
: result.ts += 1;
break;
Expand Down
2 changes: 1 addition & 1 deletion src/server/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1575,7 +1575,7 @@ namespace ts.server {
else {
const info = this.projectService.getScriptInfo(fileNameInProject);
if (!info.isScriptOpen()) {
if (fileNameInProject.indexOf(Extension.Dts) > 0) {
if (fileNameInProject.indexOf(".d.ts") > 0) {
veryLowPriorityFiles.push(fileNameInProject);
}
else {
Expand Down
2 changes: 1 addition & 1 deletion src/services/navigateTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace ts.NavigateTo {
for (const sourceFile of sourceFiles) {
cancellationToken.throwIfCancellationRequested();

if (excludeDtsFiles && fileExtensionIs(sourceFile.fileName, Extension.Dts)) {
if (excludeDtsFiles && fileExtensionIs(sourceFile.fileName, ".d.ts")) {
continue;
}

Expand Down

0 comments on commit 8b87d79

Please sign in to comment.