Skip to content

Commit d646c72

Browse files
author
Andy Hanson
committed
Merge branch 'master' into jsdoc
2 parents ba884bc + 38ece3b commit d646c72

File tree

50 files changed

+417
-321
lines changed

Some content is hidden

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

50 files changed

+417
-321
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7640,11 +7640,11 @@ namespace ts {
76407640
if (members.has(leftProp.name)) {
76417641
const rightProp = members.get(leftProp.name);
76427642
const rightType = getTypeOfSymbol(rightProp);
7643-
if (maybeTypeOfKind(rightType, TypeFlags.Undefined) || rightProp.flags & SymbolFlags.Optional) {
7643+
if (rightProp.flags & SymbolFlags.Optional) {
76447644
const declarations: Declaration[] = concatenate(leftProp.declarations, rightProp.declarations);
76457645
const flags = SymbolFlags.Property | (leftProp.flags & SymbolFlags.Optional);
76467646
const result = createSymbol(flags, leftProp.name);
7647-
result.type = getUnionType([getTypeOfSymbol(leftProp), getTypeWithFacts(rightType, TypeFacts.NEUndefined)]);
7647+
result.type = getUnionType([getTypeOfSymbol(leftProp), rightType]);
76487648
result.leftSpread = leftProp;
76497649
result.rightSpread = rightProp;
76507650
result.declarations = declarations;

src/compiler/program.ts

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,8 @@ namespace ts {
529529
getFileProcessingDiagnostics: () => fileProcessingDiagnostics,
530530
getResolvedTypeReferenceDirectives: () => resolvedTypeReferenceDirectives,
531531
isSourceFileFromExternalLibrary,
532-
dropDiagnosticsProducingTypeChecker
532+
dropDiagnosticsProducingTypeChecker,
533+
getSourceFileFromReference,
533534
};
534535

535536
verifyCompilerOptions();
@@ -1442,48 +1443,60 @@ namespace ts {
14421443
}
14431444
}
14441445

1445-
function processSourceFile(fileName: string, isDefaultLib: boolean, refFile?: SourceFile, refPos?: number, refEnd?: number) {
1446-
let diagnosticArgument: string[];
1447-
let diagnostic: DiagnosticMessage;
1446+
/** This should have similar behavior to 'processSourceFile' without diagnostics or mutation. */
1447+
function getSourceFileFromReference(referencingFile: SourceFile, ref: FileReference): SourceFile | undefined {
1448+
return getSourceFileFromReferenceWorker(resolveTripleslashReference(ref.fileName, referencingFile.fileName), fileName => filesByName.get(toPath(fileName, currentDirectory, getCanonicalFileName)));
1449+
}
1450+
1451+
function getSourceFileFromReferenceWorker(
1452+
fileName: string,
1453+
getSourceFile: (fileName: string) => SourceFile | undefined,
1454+
fail?: (diagnostic: DiagnosticMessage, ...argument: string[]) => void,
1455+
refFile?: SourceFile): SourceFile | undefined {
1456+
14481457
if (hasExtension(fileName)) {
14491458
if (!options.allowNonTsExtensions && !forEach(supportedExtensions, extension => fileExtensionIs(host.getCanonicalFileName(fileName), extension))) {
1450-
diagnostic = Diagnostics.File_0_has_unsupported_extension_The_only_supported_extensions_are_1;
1451-
diagnosticArgument = [fileName, "'" + supportedExtensions.join("', '") + "'"];
1452-
}
1453-
else if (!findSourceFile(fileName, toPath(fileName, currentDirectory, getCanonicalFileName), isDefaultLib, refFile, refPos, refEnd)) {
1454-
diagnostic = Diagnostics.File_0_not_found;
1455-
diagnosticArgument = [fileName];
1459+
if (fail) fail(Diagnostics.File_0_has_unsupported_extension_The_only_supported_extensions_are_1, fileName, "'" + supportedExtensions.join("', '") + "'");
1460+
return undefined;
14561461
}
1457-
else if (refFile && host.getCanonicalFileName(fileName) === host.getCanonicalFileName(refFile.fileName)) {
1458-
diagnostic = Diagnostics.A_file_cannot_have_a_reference_to_itself;
1459-
diagnosticArgument = [fileName];
1460-
}
1461-
}
1462-
else {
1463-
const nonTsFile: SourceFile = options.allowNonTsExtensions && findSourceFile(fileName, toPath(fileName, currentDirectory, getCanonicalFileName), isDefaultLib, refFile, refPos, refEnd);
1464-
if (!nonTsFile) {
1465-
if (options.allowNonTsExtensions) {
1466-
diagnostic = Diagnostics.File_0_not_found;
1467-
diagnosticArgument = [fileName];
1462+
1463+
const sourceFile = getSourceFile(fileName);
1464+
if (fail) {
1465+
if (!sourceFile) {
1466+
fail(Diagnostics.File_0_not_found, fileName);
14681467
}
1469-
else if (!forEach(supportedExtensions, extension => findSourceFile(fileName + extension, toPath(fileName + extension, currentDirectory, getCanonicalFileName), isDefaultLib, refFile, refPos, refEnd))) {
1470-
diagnostic = Diagnostics.File_0_not_found;
1471-
fileName += ".ts";
1472-
diagnosticArgument = [fileName];
1468+
else if (refFile && host.getCanonicalFileName(fileName) === host.getCanonicalFileName(refFile.fileName)) {
1469+
fail(Diagnostics.A_file_cannot_have_a_reference_to_itself, fileName);
14731470
}
14741471
}
1475-
}
1472+
return sourceFile;
1473+
} else {
1474+
const sourceFileNoExtension = options.allowNonTsExtensions && getSourceFile(fileName);
1475+
if (sourceFileNoExtension) return sourceFileNoExtension;
14761476

1477-
if (diagnostic) {
1478-
if (refFile !== undefined && refEnd !== undefined && refPos !== undefined) {
1479-
fileProcessingDiagnostics.add(createFileDiagnostic(refFile, refPos, refEnd - refPos, diagnostic, ...diagnosticArgument));
1480-
}
1481-
else {
1482-
fileProcessingDiagnostics.add(createCompilerDiagnostic(diagnostic, ...diagnosticArgument));
1477+
if (fail && options.allowNonTsExtensions) {
1478+
fail(Diagnostics.File_0_not_found, fileName);
1479+
return undefined;
14831480
}
1481+
1482+
const sourceFileWithAddedExtension = forEach(supportedExtensions, extension => getSourceFile(fileName + extension));
1483+
if (fail && !sourceFileWithAddedExtension) fail(Diagnostics.File_0_not_found, fileName + ".ts");
1484+
return sourceFileWithAddedExtension;
14841485
}
14851486
}
14861487

1488+
/** This has side effects through `findSourceFile`. */
1489+
function processSourceFile(fileName: string, isDefaultLib: boolean, refFile?: SourceFile, refPos?: number, refEnd?: number): void {
1490+
getSourceFileFromReferenceWorker(fileName,
1491+
fileName => findSourceFile(fileName, toPath(fileName, currentDirectory, getCanonicalFileName), isDefaultLib, refFile, refPos, refEnd),
1492+
(diagnostic, ...args) => {
1493+
fileProcessingDiagnostics.add(refFile !== undefined && refEnd !== undefined && refPos !== undefined
1494+
? createFileDiagnostic(refFile, refPos, refEnd - refPos, diagnostic, ...args)
1495+
: createCompilerDiagnostic(diagnostic, ...args));
1496+
},
1497+
refFile);
1498+
}
1499+
14871500
function reportFileNamesDifferOnlyInCasingError(fileName: string, existingFileName: string, refFile: SourceFile, refPos: number, refEnd: number): void {
14881501
if (refFile !== undefined && refPos !== undefined && refEnd !== undefined) {
14891502
fileProcessingDiagnostics.add(createFileDiagnostic(refFile, refPos, refEnd - refPos,

src/compiler/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2425,6 +2425,8 @@ namespace ts {
24252425
/* @internal */ isSourceFileFromExternalLibrary(file: SourceFile): boolean;
24262426
// For testing purposes only.
24272427
/* @internal */ structureIsReused?: StructureIsReused;
2428+
2429+
/* @internal */ getSourceFileFromReference(referencingFile: SourceFile, ref: FileReference): SourceFile | undefined;
24282430
}
24292431

24302432
/* @internal */

src/compiler/utilities.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1927,21 +1927,19 @@ namespace ts {
19271927
const isNoDefaultLibRegEx = /^(\/\/\/\s*<reference\s+no-default-lib\s*=\s*)('|")(.+?)\2\s*\/>/gim;
19281928
if (simpleReferenceRegEx.test(comment)) {
19291929
if (isNoDefaultLibRegEx.test(comment)) {
1930-
return {
1931-
isNoDefaultLib: true
1932-
};
1930+
return { isNoDefaultLib: true };
19331931
}
19341932
else {
19351933
const refMatchResult = fullTripleSlashReferencePathRegEx.exec(comment);
19361934
const refLibResult = !refMatchResult && fullTripleSlashReferenceTypeReferenceDirectiveRegEx.exec(comment);
1937-
if (refMatchResult || refLibResult) {
1938-
const start = commentRange.pos;
1939-
const end = commentRange.end;
1935+
const match = refMatchResult || refLibResult;
1936+
if (match) {
1937+
const pos = commentRange.pos + match[1].length + match[2].length;
19401938
return {
19411939
fileReference: {
1942-
pos: start,
1943-
end: end,
1944-
fileName: (refMatchResult || refLibResult)[3]
1940+
pos,
1941+
end: pos + match[3].length,
1942+
fileName: match[3]
19451943
},
19461944
isNoDefaultLib: false,
19471945
isTypeReferenceDirective: !!refLibResult

src/harness/fourslash.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,14 @@ namespace FourSlash {
10641064
}
10651065
}
10661066
};
1067+
if (fullActual === undefined || fullExpected === undefined) {
1068+
if (fullActual === fullExpected) {
1069+
return;
1070+
}
1071+
console.log("Expected:", stringify(fullExpected));
1072+
console.log("Actual: ", stringify(fullActual));
1073+
this.raiseError(msgPrefix);
1074+
}
10671075
recur(fullActual, fullExpected, "");
10681076

10691077
}

src/harness/unittests/services/preProcessFile.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ describe("PreProcessFile:", function () {
3737
/*readImportFile*/ true,
3838
/*detectJavaScriptImports*/ false,
3939
{
40-
referencedFiles: [{ fileName: "refFile1.ts", pos: 0, end: 37 }, { fileName: "refFile2.ts", pos: 38, end: 73 },
41-
{ fileName: "refFile3.ts", pos: 74, end: 109 }, { fileName: "..\\refFile4d.ts", pos: 110, end: 150 }],
40+
referencedFiles: [{ fileName: "refFile1.ts", pos: 22, end: 33 }, { fileName: "refFile2.ts", pos: 59, end: 70 },
41+
{ fileName: "refFile3.ts", pos: 94, end: 105 }, { fileName: "..\\refFile4d.ts", pos: 131, end: 146 }],
4242
importedFiles: <ts.FileReference[]>[],
4343
typeReferenceDirectives: [],
4444
ambientExternalModules: undefined,
@@ -104,7 +104,7 @@ describe("PreProcessFile:", function () {
104104
/*readImportFile*/ true,
105105
/*detectJavaScriptImports*/ false,
106106
{
107-
referencedFiles: [{ fileName: "refFile1.ts", pos: 0, end: 35 }, { fileName: "refFile2.ts", pos: 36, end: 71 }],
107+
referencedFiles: [{ fileName: "refFile1.ts", pos: 20, end: 31 }, { fileName: "refFile2.ts", pos: 57, end: 68 }],
108108
typeReferenceDirectives: [],
109109
importedFiles: [{ fileName: "r1.ts", pos: 92, end: 97 }, { fileName: "r2.ts", pos: 121, end: 126 }],
110110
ambientExternalModules: undefined,
@@ -117,7 +117,7 @@ describe("PreProcessFile:", function () {
117117
/*readImportFile*/ true,
118118
/*detectJavaScriptImports*/ false,
119119
{
120-
referencedFiles: [{ fileName: "refFile1.ts", pos: 0, end: 35 }],
120+
referencedFiles: [{ fileName: "refFile1.ts", pos: 20, end: 31 }],
121121
typeReferenceDirectives: [],
122122
importedFiles: [{ fileName: "r1.ts", pos: 91, end: 96 }, { fileName: "r3.ts", pos: 148, end: 153 }],
123123
ambientExternalModules: undefined,
@@ -442,12 +442,12 @@ describe("PreProcessFile:", function () {
442442
/*detectJavaScriptImports*/ false,
443443
{
444444
referencedFiles: [
445-
{ "pos": 13, "end": 38, "fileName": "a" },
446-
{ "pos": 91, "end": 117, "fileName": "a2" }
445+
{ "pos": 34, "end": 35, "fileName": "a" },
446+
{ "pos": 112, "end": 114, "fileName": "a2" }
447447
],
448448
typeReferenceDirectives: [
449-
{ "pos": 51, "end": 78, "fileName": "a1" },
450-
{ "pos": 130, "end": 157, "fileName": "a3" }
449+
{ "pos": 73, "end": 75, "fileName": "a1" },
450+
{ "pos": 152, "end": 154, "fileName": "a3" }
451451
],
452452
importedFiles: [],
453453
ambientExternalModules: undefined,

src/services/documentHighlights.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/* @internal */
22
namespace ts.DocumentHighlights {
3-
export function getDocumentHighlights(typeChecker: TypeChecker, cancellationToken: CancellationToken, sourceFile: SourceFile, position: number, sourceFilesToSearch: SourceFile[]): DocumentHighlights[] {
3+
export function getDocumentHighlights(program: Program, cancellationToken: CancellationToken, sourceFile: SourceFile, position: number, sourceFilesToSearch: SourceFile[]): DocumentHighlights[] {
44
const node = getTouchingWord(sourceFile, position, /*includeJsDocComment*/ true);
5-
return node && (getSemanticDocumentHighlights(node, typeChecker, cancellationToken, sourceFilesToSearch) || getSyntacticDocumentHighlights(node, sourceFile));
5+
return node && (getSemanticDocumentHighlights(node, program, cancellationToken, sourceFilesToSearch) || getSyntacticDocumentHighlights(node, sourceFile));
66
}
77

88
function getHighlightSpanForNode(node: Node, sourceFile: SourceFile): HighlightSpan {
@@ -16,8 +16,8 @@ namespace ts.DocumentHighlights {
1616
};
1717
}
1818

19-
function getSemanticDocumentHighlights(node: Node, typeChecker: TypeChecker, cancellationToken: CancellationToken, sourceFilesToSearch: SourceFile[]): DocumentHighlights[] {
20-
const referenceEntries = FindAllReferences.getReferenceEntriesForNode(node, sourceFilesToSearch, typeChecker, cancellationToken);
19+
function getSemanticDocumentHighlights(node: Node, program: Program, cancellationToken: CancellationToken, sourceFilesToSearch: SourceFile[]): DocumentHighlights[] {
20+
const referenceEntries = FindAllReferences.getReferenceEntriesForNode(node, program, sourceFilesToSearch, cancellationToken);
2121
return referenceEntries && convertReferencedSymbols(referenceEntries);
2222
}
2323

0 commit comments

Comments
 (0)