Skip to content

Commit

Permalink
Merge pull request #443 from microsoft/main
Browse files Browse the repository at this point in the history
Create a new pull request by comparing changes across two branches
  • Loading branch information
GulajavaMinistudio authored Jul 7, 2023
2 parents bf8fe7a + e374eba commit 1c3cd3e
Show file tree
Hide file tree
Showing 173 changed files with 2,021 additions and 1,156 deletions.
24 changes: 12 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 14 additions & 9 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2068,7 +2068,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
/** Key is "/path/to/a.ts|/path/to/b.ts". */
var amalgamatedDuplicates: Map<string, DuplicateInfoForFiles> | undefined;
var reverseMappedCache = new Map<string, Type | undefined>();
var inInferTypeForHomomorphicMappedType = false;
var homomorphicMappedTypeInferenceStack: string[] = [];
var ambientModulesCache: Symbol[] | undefined;
/**
* List of every ambient module with a "*" wildcard.
Expand Down Expand Up @@ -7440,6 +7440,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (typeArguments) {
node.typeArguments = factory.createNodeArray(typeArguments);
}
if (signature.declaration?.kind === SyntaxKind.JSDocSignature && signature.declaration.parent.kind === SyntaxKind.JSDocOverloadTag) {
const comment = getTextOfNode(signature.declaration.parent.parent, /*includeTrivia*/ true).slice(2, -2).split(/\r\n|\n|\r/).map(line => line.replace(/^\s+/, " ")).join("\n");
addSyntheticLeadingComment(node, SyntaxKind.MultiLineCommentTrivia, comment, /*hasTrailingNewLine*/ true);
}

cleanup?.();
return node;
Expand Down Expand Up @@ -24122,17 +24126,18 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
* variable T[P] (i.e. we treat the type T[P] as the type variable we're inferring for).
*/
function inferTypeForHomomorphicMappedType(source: Type, target: MappedType, constraint: IndexType): Type | undefined {
if (inInferTypeForHomomorphicMappedType) {
return undefined;
const cacheKey = source.id + "," + target.id + "," + constraint.id;
if (reverseMappedCache.has(cacheKey)) {
return reverseMappedCache.get(cacheKey);
}
const key = source.id + "," + target.id + "," + constraint.id;
if (reverseMappedCache.has(key)) {
return reverseMappedCache.get(key);
const recursionKey = source.id + "," + (target.target || target).id;
if (contains(homomorphicMappedTypeInferenceStack, recursionKey)) {
return undefined;
}
inInferTypeForHomomorphicMappedType = true;
homomorphicMappedTypeInferenceStack.push(recursionKey);
const type = createReverseMappedType(source, target, constraint);
inInferTypeForHomomorphicMappedType = false;
reverseMappedCache.set(key, type);
homomorphicMappedTypeInferenceStack.pop();
reverseMappedCache.set(cacheKey, type);
return type;
}

Expand Down
21 changes: 10 additions & 11 deletions src/compiler/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1142,7 +1142,7 @@ export function createScanner(languageVersion: ScriptTarget,
// | NonOctalDecimalIntegerLiteral
// LegacyOctalIntegerLiteral ::= '0' [0-7]+
// NonOctalDecimalIntegerLiteral ::= '0' [0-7]* [89] [0-9]*
function scanNumber(): { type: SyntaxKind, value: string } {
function scanNumber(): SyntaxKind {
let start = pos;
let mainFragment: string;
if (text.charCodeAt(pos) === CharacterCodes._0) {
Expand Down Expand Up @@ -1173,7 +1173,7 @@ export function createScanner(languageVersion: ScriptTarget,
const literal = (withMinus ? "-" : "") + "0o" + (+tokenValue).toString(8);
if (withMinus) start--;
error(Diagnostics.Octal_literals_are_not_allowed_Use_the_syntax_0, start, pos - start, literal);
return { type: SyntaxKind.NumericLiteral, value: tokenValue };
return SyntaxKind.NumericLiteral;
}
}
else {
Expand Down Expand Up @@ -1217,21 +1217,21 @@ export function createScanner(languageVersion: ScriptTarget,
if (tokenFlags & TokenFlags.ContainsLeadingZero) {
error(Diagnostics.Decimals_with_leading_zeros_are_not_allowed, start, end - start);
// if a literal has a leading zero, it must not be bigint
return { type: SyntaxKind.NumericLiteral, value: "" + +result };
tokenValue = "" + +result;
return SyntaxKind.NumericLiteral;
}

if (decimalFragment !== undefined || tokenFlags & TokenFlags.Scientific) {
checkForIdentifierStartAfterNumericLiteral(start, decimalFragment === undefined && !!(tokenFlags & TokenFlags.Scientific));
return {
type: SyntaxKind.NumericLiteral,
value: "" + +result // if value is not an integer, it can be safely coerced to a number
};
// if value is not an integer, it can be safely coerced to a number
tokenValue = "" + +result;
return SyntaxKind.NumericLiteral;
}
else {
tokenValue = result;
const type = checkBigIntSuffix(); // if value is an integer, check whether it is a bigint
checkForIdentifierStartAfterNumericLiteral(start);
return { type, value: tokenValue };
return type;
}
}

Expand Down Expand Up @@ -1937,7 +1937,7 @@ export function createScanner(languageVersion: ScriptTarget,
return token = SyntaxKind.MinusToken;
case CharacterCodes.dot:
if (isDigit(text.charCodeAt(pos + 1))) {
tokenValue = scanNumber().value;
scanNumber();
return token = SyntaxKind.NumericLiteral;
}
if (text.charCodeAt(pos + 1) === CharacterCodes.dot && text.charCodeAt(pos + 2) === CharacterCodes.dot) {
Expand Down Expand Up @@ -2065,8 +2065,7 @@ export function createScanner(languageVersion: ScriptTarget,
case CharacterCodes._7:
case CharacterCodes._8:
case CharacterCodes._9:
({ type: token, value: tokenValue } = scanNumber());
return token;
return token = scanNumber();
case CharacterCodes.colon:
pos++;
return token = SyntaxKind.ColonToken;
Expand Down
33 changes: 21 additions & 12 deletions src/harness/fourslashImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -822,21 +822,29 @@ export class TestState {
});
}

public verifyInlayHints(expected: readonly FourSlashInterface.VerifyInlayHintsOptions[], span: ts.TextSpan = { start: 0, length: this.activeFile.content.length }, preference?: ts.UserPreferences) {
const hints = this.languageService.provideInlayHints(this.activeFile.fileName, span, preference);
assert.equal(hints.length, expected.length, "Number of hints");

public baselineInlayHints(span: ts.TextSpan = { start: 0, length: this.activeFile.content.length }, preferences?: ts.UserPreferences): void{
interface HasPosition { position: number; }
const sortHints = (a: HasPosition, b: HasPosition) => {
return a.position - b.position;
};
ts.zipWith(hints.sort(sortHints), [...expected].sort(sortHints), (actual, expected) => {
assert.equal(actual.text, expected.text, "Text");
assert.equal(actual.position, expected.position, "Position");
assert.equal(actual.kind, expected.kind, "Kind");
assert.equal(actual.whitespaceBefore, expected.whitespaceBefore, "whitespaceBefore");
assert.equal(actual.whitespaceAfter, expected.whitespaceAfter, "whitespaceAfter");

const baselineFile = this.getBaselineFileNameForContainingTestFile();
const fileName = this.activeFile.fileName;
const hints = this.languageService.provideInlayHints(fileName, span, preferences);
const annotations = ts.map(hints.sort(sortHints), hint => {
const span = { start: hint.position, length: hint.text.length };
const { character, line } = this.languageServiceAdapterHost.positionToLineAndCharacter(fileName, span.start);
const underline = " ".repeat(character) + "^";
let annotation = this.getFileContent(fileName).split(/\r?\n/)[line];
annotation += "\n" + underline + "\n" + JSON.stringify(hint, undefined, " ");
return annotation;
});

if (annotations.length === 0) {
annotations.push("=== No inlay hints ===");
}

Harness.Baseline.runBaseline(baselineFile, annotations.join("\n\n"));
}

public verifyCompletions(options: FourSlashInterface.VerifyCompletionsOptions) {
Expand Down Expand Up @@ -1445,6 +1453,7 @@ export class TestState {
}: BaselineDocumentSpansWithFileContentsOptions<T>,
spanToContextId: Map<T, number>,
) {
const isLibFile = /lib(?:.*)\.d\.ts$/.test(fileName);
let readableContents = `// === ${fileName} ===`;
let newContent = "";
interface Detail {
Expand Down Expand Up @@ -1619,12 +1628,12 @@ export class TestState {
if (locationLine - posLine > nLines) {
if (newContent) {
readableContents = readableContents + "\n" + readableJsoncBaseline(newContent + content.slice(pos, lineStarts[posLine + TestState.nLinesContext]) +
`--- (line: ${posLine + TestState.nLinesContext + 1}) skipped ---`);
`--- (line: ${isLibFile ? "--" : posLine + TestState.nLinesContext + 1}) skipped ---`);
if (location !== undefined) readableContents += "\n";
newContent = "";
}
if (location !== undefined) {
newContent += `--- (line: ${locationLine - TestState.nLinesContext + 1}) skipped ---\n` +
newContent += `--- (line: ${isLibFile ? "--" : locationLine - TestState.nLinesContext + 1}) skipped ---\n` +
content.slice(lineStarts[locationLine - TestState.nLinesContext + 1], location);
}
return;
Expand Down
6 changes: 3 additions & 3 deletions src/harness/fourslashInterfaceImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ export class Verify extends VerifyNegatable {
};
}

public getInlayHints(expected: readonly VerifyInlayHintsOptions[], span: ts.TextSpan, preference?: ts.UserPreferences) {
this.state.verifyInlayHints(expected, span, preference);
public baselineInlayHints(span: ts.TextSpan, preference?: ts.UserPreferences) {
this.state.baselineInlayHints(span, preference);
}

public quickInfoIs(expectedText: string, expectedDocumentation?: string, expectedTags?: { name: string; text: string; }[]) {
Expand Down Expand Up @@ -1952,4 +1952,4 @@ export type BaselineCommand = BaselineCommandWithMarkerOrRange | {
} | {
type: "customWork";
work: () => string | undefined;
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// user2

// === lib.d.ts ===
// --- (line: 1588) skipped ---
// --- (line: --) skipped ---
// /**
// * From T, pick a set of properties whose keys are in the union K
// */
Expand All @@ -19,7 +19,7 @@
//
// /**
// * Construct a type with a set of properties K of type T
// --- (line: 1598) skipped ---
// --- (line: --) skipped ---

// === Details ===
[
Expand Down Expand Up @@ -58,7 +58,7 @@
// /*GOTO TYPE*/user2

// === lib.d.ts ===
// --- (line: 1588) skipped ---
// --- (line: --) skipped ---
// /**
// * From T, pick a set of properties whose keys are in the union K
// */
Expand All @@ -68,7 +68,7 @@
//
// /**
// * Construct a type with a set of properties K of type T
// --- (line: 1598) skipped ---
// --- (line: --) skipped ---

// === Details ===
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// --- (line: 7) skipped ---

// === lib.d.ts ===
// --- (line: 1310) skipped ---
// --- (line: --) skipped ---
// slice(start?: number, end?: number): T[];
// }
//
Expand Down Expand Up @@ -199,17 +199,17 @@
//
// interface ArrayConstructor {
// new(arrayLength?: number): any[];
// --- (line: 1500) skipped ---
// --- (line: --) skipped ---

// --- (line: 1505) skipped ---
// --- (line: --) skipped ---
// readonly prototype: any[];
// }
//
// <|declare var [|{| defId: 2 |}Array|]: ArrayConstructor;|>
//
// interface TypedPropertyDescriptor<T> {
// enumerable?: boolean;
// --- (line: 1513) skipped ---
// --- (line: --) skipped ---

// === Details ===
[
Expand Down Expand Up @@ -262,7 +262,7 @@
// users3

// === lib.d.ts ===
// --- (line: 1310) skipped ---
// --- (line: --) skipped ---
// slice(start?: number, end?: number): T[];
// }
//
Expand Down Expand Up @@ -452,17 +452,17 @@
//
// interface ArrayConstructor {
// new(arrayLength?: number): any[];
// --- (line: 1500) skipped ---
// --- (line: --) skipped ---

// --- (line: 1505) skipped ---
// --- (line: --) skipped ---
// readonly prototype: any[];
// }
//
// <|declare var [|{| defId: 2 |}Array|]: ArrayConstructor;|>
//
// interface TypedPropertyDescriptor<T> {
// enumerable?: boolean;
// --- (line: 1513) skipped ---
// --- (line: --) skipped ---

// === Details ===
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// export {}

// === lib.d.ts ===
// --- (line: 1531) skipped ---
// --- (line: --) skipped ---
// /**
// * Represents the completion of an asynchronous operation
// */
Expand All @@ -31,7 +31,7 @@
//
// /**
// * Recursively unwraps the "awaited type" of a type. Non-promise "thenables" should resolve to `never`. This emulates the behavior of `await`.
// --- (line: 1554) skipped ---
// --- (line: --) skipped ---

// === Details ===
[
Expand Down Expand Up @@ -69,7 +69,7 @@
// export {}

// === lib.d.ts ===
// --- (line: 1531) skipped ---
// --- (line: --) skipped ---
// /**
// * Represents the completion of an asynchronous operation
// */
Expand All @@ -92,7 +92,7 @@
//
// /**
// * Recursively unwraps the "awaited type" of a type. Non-promise "thenables" should resolve to `never`. This emulates the behavior of `await`.
// --- (line: 1554) skipped ---
// --- (line: --) skipped ---

// === Details ===
[
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/inlayHintsCrash1.baseline
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
=== No inlay hints ===
17 changes: 17 additions & 0 deletions tests/baselines/reference/inlayHintsShouldWork1.baseline
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
foo(1, 2);
^
{
"text": "a:",
"position": 43,
"kind": "Parameter",
"whitespaceAfter": true
}

foo(1, 2);
^
{
"text": "b:",
"position": 46,
"kind": "Parameter",
"whitespaceAfter": true
}
Loading

0 comments on commit 1c3cd3e

Please sign in to comment.