Skip to content

Commit

Permalink
Merge branch 'main' into bug/isolated-modules-enum-restrictions
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbranch committed Mar 27, 2024
2 parents 392976d + b0d5ae6 commit ed0ea55
Show file tree
Hide file tree
Showing 12,014 changed files with 563,320 additions and 1,713 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
5 changes: 3 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8612,7 +8612,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const type = getDeclaredTypeOfSymbol(sym);
const name = sym.flags & SymbolFlags.TypeParameter ? typeParameterToName(type, context) : factory.cloneNode(node);
name.symbol = sym; // for quickinfo, which uses identifier symbol information
return { introducesError, node: setEmitFlags(setOriginalNode(name, node), EmitFlags.NoAsciiEscaping) };
return { introducesError, node: setTextRange(setEmitFlags(setOriginalNode(name, node), EmitFlags.NoAsciiEscaping), node) };
}
}

Expand Down Expand Up @@ -39783,7 +39783,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (getIsolatedModules(compilerOptions)) {
Debug.assert(!!(type.symbol.flags & SymbolFlags.ConstEnum));
const constEnumDeclaration = type.symbol.valueDeclaration as EnumDeclaration;
if (constEnumDeclaration.flags & NodeFlags.Ambient && !isValidTypeOnlyAliasUseSite(node)) {
const redirect = host.getRedirectReferenceForResolutionFromSourceOfProject(getSourceFileOfNode(constEnumDeclaration).resolvedPath);
if (constEnumDeclaration.flags & NodeFlags.Ambient && !isValidTypeOnlyAliasUseSite(node) && (!redirect || !shouldPreserveConstEnums(redirect.commandLine.options))) {
error(node, Diagnostics.Cannot_access_ambient_const_enums_when_0_is_enabled, isolatedModulesLikeFlagName);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1925,6 +1925,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
getResolvedProjectReferenceByPath,
forEachResolvedProjectReference,
isSourceOfProjectReferenceRedirect,
getRedirectReferenceForResolutionFromSourceOfProject,
emitBuildInfo,
fileExists,
readFile,
Expand Down
8 changes: 7 additions & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4734,9 +4734,14 @@ export interface Program extends ScriptReferenceHost {
getProjectReferences(): readonly ProjectReference[] | undefined;
getResolvedProjectReferences(): readonly (ResolvedProjectReference | undefined)[] | undefined;
/** @internal */ getProjectReferenceRedirect(fileName: string): string | undefined;
/** @internal */ getResolvedProjectReferenceToRedirect(fileName: string): ResolvedProjectReference | undefined;
/**
* @internal
* Get the referenced project if the file is input file from that reference project
*/
getResolvedProjectReferenceToRedirect(fileName: string): ResolvedProjectReference | undefined;
/** @internal */ forEachResolvedProjectReference<T>(cb: (resolvedProjectReference: ResolvedProjectReference) => T | undefined): T | undefined;
/** @internal */ getResolvedProjectReferenceByPath(projectReferencePath: Path): ResolvedProjectReference | undefined;
/** @internal */ getRedirectReferenceForResolutionFromSourceOfProject(filePath: Path): ResolvedProjectReference | undefined;
/** @internal */ isSourceOfProjectReferenceRedirect(fileName: string): boolean;
/** @internal */ getBuildInfo?(): BuildInfo;
/** @internal */ emitBuildInfo(writeFile?: WriteFileCallback, cancellationToken?: CancellationToken): EmitResult;
Expand Down Expand Up @@ -4853,6 +4858,7 @@ export interface TypeCheckerHost extends ModuleSpecifierResolutionHost {
getSourceFile(fileName: string): SourceFile | undefined;
getProjectReferenceRedirect(fileName: string): string | undefined;
isSourceOfProjectReferenceRedirect(fileName: string): boolean;
getRedirectReferenceForResolutionFromSourceOfProject(filePath: Path): ResolvedProjectReference | undefined;
getModeForUsageLocation(file: SourceFile, usage: StringLiteralLike): ResolutionMode;

getResolvedModule(f: SourceFile, moduleName: string, mode: ResolutionMode): ResolvedModuleWithFailedLookupLocations | undefined;
Expand Down
6 changes: 5 additions & 1 deletion src/harness/harnessIO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -856,8 +856,12 @@ export namespace Compiler {
}
lastIndexWritten = result.line;
const typeOrSymbolString = isSymbolBaseline ? result.symbol : result.type;
const formattedLine = result.sourceText.replace(/\r?\n/g, "") + " : " + typeOrSymbolString;
const lineText = result.sourceText.replace(/\r?\n/g, "");
const formattedLine = lineText + " : " + typeOrSymbolString;
typeLines += ">" + formattedLine + "\r\n";
if (result.underline) {
typeLines += ">" + " ".repeat(lineText.length) + " : " + result.underline + "\r\n";
}
}

lastIndexWritten ??= -1;
Expand Down
149 changes: 146 additions & 3 deletions src/harness/typeWriter.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import * as ts from "./_namespaces/ts";
import {
createPrinter,
createTextWriter,
memoize,
} from "./_namespaces/ts";

export interface TypeWriterTypeResult {
line: number;
syntaxKind: number;
sourceText: string;
type: string;
underline?: string;
}

export interface TypeWriterSymbolResult {
Expand All @@ -20,6 +26,7 @@ export interface TypeWriterResult {
sourceText: string;
symbol?: string;
type?: string;
underline?: string;
}

function* forEachASTNode(node: ts.Node) {
Expand All @@ -37,6 +44,134 @@ function* forEachASTNode(node: ts.Node) {
}
}

const createSyntheticNodeUnderliningPrinter = memoize((): { printer: ts.Printer; writer: ts.EmitTextWriter; underliner: ts.EmitTextWriter; reset(): void; } => {
let underlining = false;
const printer = createPrinter({ removeComments: true }, {
onEmitNode: (hint, node, cb) => {
if (ts.nodeIsSynthesized(node) !== underlining) {
// either node is synthetic and underlining needs to be enabled, or node is not synthetic and
// underlining needs to be disabled
underlining = !underlining;
const result = cb(hint, node);
underlining = !underlining;
return result;
}
// underlining does not need to change
return cb(hint, node);
},
});
const baseWriter = createTextWriter("");
const underliner = createTextWriter("");

return {
printer,
writer: {
write(s: string): void {
baseWriter.write(s);
underliner.write(underlineFor(s));
},
writeTrailingSemicolon(text: string): void {
baseWriter.writeTrailingSemicolon(text);
underliner.writeTrailingSemicolon(underlineFor(text));
},
writeComment(text: string): void {
baseWriter.writeComment(text);
underliner.writeComment(underlineFor(text));
},
getText(): string {
return baseWriter.getText();
},
rawWrite(s: string): void {
baseWriter.rawWrite(s);
underliner.rawWrite(underlineFor(s));
},
writeLiteral(s: string): void {
baseWriter.writeLiteral(s);
underliner.writeLiteral(underlineFor(s));
},
getTextPos(): number {
return baseWriter.getTextPos();
},
getLine(): number {
return baseWriter.getLine();
},
getColumn(): number {
return baseWriter.getColumn();
},
getIndent(): number {
return baseWriter.getIndent();
},
isAtStartOfLine(): boolean {
return baseWriter.isAtStartOfLine();
},
hasTrailingComment(): boolean {
return baseWriter.hasTrailingComment();
},
hasTrailingWhitespace(): boolean {
return baseWriter.hasTrailingWhitespace();
},
writeKeyword(text: string): void {
baseWriter.writeKeyword(text);
underliner.writeKeyword(underlineFor(text));
},
writeOperator(text: string): void {
baseWriter.writeOperator(text);
underliner.writeOperator(underlineFor(text));
},
writePunctuation(text: string): void {
baseWriter.writePunctuation(text);
underliner.writePunctuation(underlineFor(text));
},
writeSpace(text: string): void {
baseWriter.writeSpace(text);
underliner.writeSpace(underlineFor(text));
},
writeStringLiteral(text: string): void {
baseWriter.writeStringLiteral(text);
underliner.writeStringLiteral(underlineFor(text));
},
writeParameter(text: string): void {
baseWriter.writeParameter(text);
underliner.writeParameter(underlineFor(text));
},
writeProperty(text: string): void {
baseWriter.writeProperty(text);
underliner.writeProperty(underlineFor(text));
},
writeSymbol(text: string, symbol: ts.Symbol): void {
baseWriter.writeSymbol(text, symbol);
underliner.writeSymbol(underlineFor(text), symbol);
},
writeLine(force?: boolean | undefined): void {
baseWriter.writeLine(force);
underliner.writeLine(force);
},
increaseIndent(): void {
baseWriter.increaseIndent();
underliner.increaseIndent();
},
decreaseIndent(): void {
baseWriter.decreaseIndent();
underliner.decreaseIndent();
},
clear(): void {
baseWriter.clear();
underliner.clear();
},
},
underliner,
reset() {
underlining = false;
baseWriter.clear();
underliner.clear();
},
};

function underlineFor(s: string) {
return s.length === 0 ? s : (underlining ? "^" : " ").repeat(s.length);
}
});

export class TypeWriterWalker {
currentSourceFile!: ts.SourceFile;

Expand Down Expand Up @@ -123,6 +258,7 @@ export class TypeWriterWalker {
// return `error`s via `getTypeAtLocation`
// But this is generally expected, so we don't call those out, either
let typeString: string;
let underline: string | undefined;
if (
!this.hadErrorBaseline &&
type.flags & ts.TypeFlags.Any &&
Expand All @@ -139,18 +275,25 @@ export class TypeWriterWalker {
}
else {
const typeFormatFlags = ts.TypeFormatFlags.NoTruncation | ts.TypeFormatFlags.AllowUniqueESSymbolType | ts.TypeFormatFlags.GenerateNamesForShadowedTypeParams;
typeString = this.checker.typeToString(type, node.parent, typeFormatFlags);
if (ts.isIdentifier(node) && ts.isTypeAliasDeclaration(node.parent) && node.parent.name === node && typeString === ts.idText(node)) {
let typeNode = this.checker.typeToTypeNode(type, node.parent, (typeFormatFlags & ts.TypeFormatFlags.NodeBuilderFlagsMask) | ts.NodeBuilderFlags.IgnoreErrors)!;
if (ts.isIdentifier(node) && ts.isTypeAliasDeclaration(node.parent) && node.parent.name === node && ts.isIdentifier(typeNode) && ts.idText(typeNode) === ts.idText(node)) {
// for a complex type alias `type T = ...`, showing "T : T" isn't very helpful for type tests. When the type produced is the same as
// the name of the type alias, recreate the type string without reusing the alias name
typeString = this.checker.typeToString(type, node.parent, typeFormatFlags | ts.TypeFormatFlags.InTypeAlias);
typeNode = this.checker.typeToTypeNode(type, node.parent, ((typeFormatFlags | ts.TypeFormatFlags.InTypeAlias) & ts.TypeFormatFlags.NodeBuilderFlagsMask) | ts.NodeBuilderFlags.IgnoreErrors)!;
}

const { printer, writer, underliner, reset } = createSyntheticNodeUnderliningPrinter();
printer.writeNode(ts.EmitHint.Unspecified, typeNode, this.currentSourceFile, writer);
typeString = writer.getText();
underline = underliner.getText();
reset();
}
return {
line: lineAndCharacter.line,
syntaxKind: node.kind,
sourceText,
type: typeString,
underline,
};
}
const symbol = this.checker.getSymbolAtLocation(node);
Expand Down
57 changes: 30 additions & 27 deletions src/server/editorServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,16 @@ function createWatchFactoryHostUsingWatchEvents(service: ProjectService, canUseW
recursive ? watchedDirectoriesRecursive : watchedDirectories,
path,
callback,
id => ({ eventName: CreateDirectoryWatcherEvent, data: { id, path, recursive: !!recursive } }),
id => ({
eventName: CreateDirectoryWatcherEvent,
data: {
id,
path,
recursive: !!recursive,
// Special case node_modules as we watch it for changes to closed script infos as well
ignoreUpdate: !path.endsWith("/node_modules") ? true : undefined,
},
}),
);
}
function getOrCreateFileWatcher<T>(
Expand Down Expand Up @@ -963,37 +972,32 @@ function createWatchFactoryHostUsingWatchEvents(service: ProjectService, canUseW
},
};
}
function onWatchChange({ id, path, eventType }: protocol.WatchChangeRequestArgs) {
// console.log(`typescript-vscode-watcher:: Invoke:: ${id}:: ${path}:: ${eventType}`);
onFileWatcherCallback(id, path, eventType);
onDirectoryWatcherCallback(watchedDirectories, id, path, eventType);
onDirectoryWatcherCallback(watchedDirectoriesRecursive, id, path, eventType);
function onWatchChange(args: protocol.WatchChangeRequestArgs | readonly protocol.WatchChangeRequestArgs[]) {
if (isArray(args)) args.forEach(onWatchChangeRequestArgs);
else onWatchChangeRequestArgs(args);
}

function onFileWatcherCallback(
id: number,
eventPath: string,
eventType: "create" | "delete" | "update",
) {
watchedFiles.idToCallbacks.get(id)?.forEach(callback => {
const eventKind = eventType === "create" ?
FileWatcherEventKind.Created :
eventType === "delete" ?
FileWatcherEventKind.Deleted :
FileWatcherEventKind.Changed;
callback(eventPath, eventKind);
});
function onWatchChangeRequestArgs({ id, created, deleted, updated }: protocol.WatchChangeRequestArgs) {
onWatchEventType(id, created, FileWatcherEventKind.Created);
onWatchEventType(id, deleted, FileWatcherEventKind.Deleted);
onWatchEventType(id, updated, FileWatcherEventKind.Changed);
}

function onWatchEventType(id: number, paths: readonly string[] | undefined, eventKind: FileWatcherEventKind) {
if (!paths?.length) return;
forEachCallback(watchedFiles, id, paths, (callback, eventPath) => callback(eventPath, eventKind));
forEachCallback(watchedDirectories, id, paths, (callback, eventPath) => callback(eventPath));
forEachCallback(watchedDirectoriesRecursive, id, paths, (callback, eventPath) => callback(eventPath));
}

function onDirectoryWatcherCallback(
{ idToCallbacks }: HostWatcherMap<DirectoryWatcherCallback>,
function forEachCallback<T>(
hostWatcherMap: HostWatcherMap<T>,
id: number,
eventPath: string,
eventType: "create" | "delete" | "update",
eventPaths: readonly string[],
cb: (callback: T, eventPath: string) => void,
) {
if (eventType === "update") return;
idToCallbacks.get(id)?.forEach(callback => {
callback(eventPath);
hostWatcherMap.idToCallbacks.get(id)?.forEach(callback => {
eventPaths.forEach(eventPath => cb(callback, normalizeSlashes(eventPath)));
});
}
}
Expand Down Expand Up @@ -4678,7 +4682,6 @@ export class ProjectService {
(fileName, eventKind) => {
switch (eventKind) {
case FileWatcherEventKind.Created:
return Debug.fail();
case FileWatcherEventKind.Changed:
this.packageJsonCache.addOrUpdate(fileName, path);
this.onPackageJsonChange(result);
Expand Down
9 changes: 5 additions & 4 deletions src/server/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1849,13 +1849,13 @@ export interface CloseRequest extends FileRequest {

export interface WatchChangeRequest extends Request {
command: CommandTypes.WatchChange;
arguments: WatchChangeRequestArgs;
arguments: WatchChangeRequestArgs | readonly WatchChangeRequestArgs[];
}

export interface WatchChangeRequestArgs {
id: number;
path: string;
eventType: "create" | "delete" | "update";
created?: string[];
deleted?: string[];
updated?: string[];
}

/**
Expand Down Expand Up @@ -2656,6 +2656,7 @@ export interface CreateDirectoryWatcherEventBody {
readonly id: number;
readonly path: string;
readonly recursive: boolean;
readonly ignoreUpdate?: boolean;
}

export type CloseFileWatcherEventName = "closeFileWatcher";
Expand Down
3 changes: 2 additions & 1 deletion src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2706,7 +2706,8 @@ export function getCompletionEntriesFromSymbols(
}

function symbolAppearsToBeTypeOnly(symbol: Symbol): boolean {
return !(symbol.flags & SymbolFlags.Value) && (!isInJSFile(symbol.declarations?.[0]) || !!(symbol.flags & SymbolFlags.Type));
const flags = getCombinedLocalAndExportSymbolFlags(skipAlias(symbol, typeChecker));
return !(flags & SymbolFlags.Value) && (!isInJSFile(symbol.declarations?.[0]) || !!(flags & SymbolFlags.Type));
}
}

Expand Down
Loading

0 comments on commit ed0ea55

Please sign in to comment.