Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
0x706b committed Jan 8, 2023
1 parent b5bd82e commit 19a26f7
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 48 deletions.
41 changes: 38 additions & 3 deletions src/compiler/checker.ts
Expand Up @@ -2001,8 +2001,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
getExtensionsForDeclaration: (node) => {
return collectTsPlusStaticTags(node)
.concat(collectTsPlusFluentTags(node))
.concat(collectTsPlusPipeableTags(node))
.concat(collectTsPlusGetterTags(node))
.concat(collectTsPlusOperatorTags(node))
.concat(collectTsPlusPipeableOperatorTags(node))
},
getTsPlusFiles: () => tsPlusFilesFinal,
getTsPlusGlobalImports: () => tsPlusGlobalImportCache,
Expand Down Expand Up @@ -4188,6 +4190,13 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
}
}
if (!result && originalLocation) {
const globalSymbol = getGlobalSymbol(name, SymbolFlags.Type, undefined);
if (globalSymbol && companionSymbolCache.has(globalSymbol)) {
result = globalSymbol;
getSymbolLinks(globalSymbol).isPossibleCompanionReference = true;
}
}
// TSPLUS EXTENSION END

// We just climbed up parents looking for the name, meaning that we started in a descendant node of `lastLocation`.
Expand Down Expand Up @@ -5571,7 +5580,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (!nodeIsSynthesized(name) && isEntityName(name) && (symbol.flags & SymbolFlags.Alias || name.parent.kind === SyntaxKind.ExportAssignment)) {
markSymbolOfAliasDeclarationIfTypeOnly(getAliasDeclarationFromName(name), symbol, /*finalTarget*/ undefined, /*overwriteEmpty*/ true);
}
return (symbol.flags & meaning) || dontResolveAlias ? symbol : resolveAlias(symbol);
return (symbol.flags & meaning) || dontResolveAlias ||
// TSPLUS EXTENSION BEGIN
getSymbolLinks(symbol).isPossibleCompanionReference
// TSPLUS EXTENSION END
? symbol
: resolveAlias(symbol);
}

/**
Expand Down Expand Up @@ -31931,6 +31945,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
nodeLinks.tsPlusResolvedType = companionExt.type
return companionExt.type;
}
return;
}
const fluentExtType = getFluentExtension(leftType, right.escapedText.toString());
if (fluentExtType && isCallExpression(node.parent) && node.parent.expression === node) {
Expand Down Expand Up @@ -48370,11 +48385,16 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
returnStatement.expression.type,
undefined
);

newDecl.jsDoc = pipeable.jsDoc;
newDecl.jsDocCache = pipeable.jsDocCache;
newDecl.symbol = createTsPlusPipeableDeclarationSymbol(name, pipeable);
setParent(newDecl, pipeable.parent);
setOriginalNode(newDecl, pipeable)

newSig.declaration = newDecl;
newSig.tsPlusDeclaration = pipeable;

if (thisify) {
const thisified = thisifyTsPlusSignature(pipeable, newSig, exportName, file, reportDiagnostic);
if (!thisified) {
Expand Down Expand Up @@ -48414,11 +48434,16 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
rsig.declaration?.type as TypeNode,
undefined
);

newDecl.jsDoc = pipeable.jsDoc;
newDecl.jsDocCache = pipeable.jsDocCache;
newDecl.symbol = createTsPlusPipeableDeclarationSymbol(name, pipeable);
setParent(newDecl, pipeable.parent);
setOriginalNode(newDecl, pipeable);

newSig.declaration = newDecl;
newSig.tsPlusDeclaration = pipeable;

if (thisify) {
const thisifiedSignature = thisifyTsPlusSignature(pipeable, newSig, exportName, file, reportDiagnostic);
if (!thisifiedSignature) {
Expand Down Expand Up @@ -48507,11 +48532,16 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
factory.createBlock([])
);
}

newDecl.jsDoc = pipeable.jsDoc;
newDecl.jsDocCache = pipeable.jsDocCache;
newDecl.symbol = createTsPlusPipeableDeclarationSymbol(name, pipeable);
setParent(newDecl, pipeable.parent);
setOriginalNode(newDecl, pipeable);

newSig.declaration = newDecl;
newSig.tsPlusDeclaration = pipeable;

if (thisify) {
const thisifiedSignature = thisifyTsPlusSignature(pipeable, newSig, exportName, file, reportDiagnostic);
if (!thisifiedSignature) {
Expand Down Expand Up @@ -48553,11 +48583,16 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
factory.createNodeArray([...returnFn.parameters, ...(sig.declaration?.parameters as NodeArray<ParameterDeclaration> ?? [])]),
returnFn.type
);
newDecl.jsDoc = pipeable.jsDoc;
newDecl.jsDocCache = pipeable.jsDocCache;

newDecl.jsDoc = pipeable.parent.parent.jsDoc;
newDecl.jsDocCache = pipeable.parent.parent.jsDocCache;
newDecl.symbol = createTsPlusPipeableDeclarationSymbol(name, pipeable);
setParent(newDecl, pipeable.parent);
setOriginalNode(newDecl, pipeable);

newSig.declaration = newDecl;
newSig.tsPlusDeclaration = pipeable;

if (thisify) {
const thisifiedSignature = thisifyTsPlusSignature(pipeable, newSig, exportName, file, reportDiagnostic);
if (!thisifiedSignature) {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/commandLineParser.ts
Expand Up @@ -3201,7 +3201,7 @@ function parseConfig(
basePath: string,
configFileName: string | undefined,
resolutionStack: string[],
errors: Push<Diagnostic>,
errors: Diagnostic[],
extendedConfigCache?: Map<string, ExtendedConfigCacheEntry>
): ParsedTsconfig {
const config = parseConfigOriginal(
Expand Down
78 changes: 54 additions & 24 deletions src/compiler/parser.ts
Expand Up @@ -1583,6 +1583,9 @@ namespace Parser {
// attached to the EOF token.
let parseErrorBeforeNextFinishedNode = false;

const tsPlusExternalTypeCache = new Map<string, Record<string, TsPlusTypeDefinition[]>>()
const tsPlusResolvedPathsCache = new Map<string, string[]>()
const tsPlusResolvedModuleCache = new Map<string, any>()
let currentTsPlusTypes: TsPlusTypeDefinition[] | null = null;
let currentTsPlusFile: string | null = null;

Expand Down Expand Up @@ -1821,16 +1824,20 @@ namespace Parser {
}
}

function parseTsPlusExternalTypes(fileName: string, options: CompilerOptions) {
function getTsPlusExternalTypesPaths(fileName: string, options: CompilerOptions) {
if (options.configFilePath) {
if (tsPlusResolvedPathsCache.has(options.configFilePath)) {
return tsPlusResolvedPathsCache.get(options.configFilePath)!;
}

let resolvedPaths: string[] = [];
if (options.tsPlusTypes) {
for (const path of options.tsPlusTypes) {
if (pathIsRelative(path)) {
resolvedPaths.push(resolvePath(options.configFilePath.split("/").slice(0, -1).join('/'), path));
}
else {
const { resolvedModule } = resolveModuleName(path, options.configFilePath, options, sys);
const resolvedModule = resolveModuleName(path, options.configFilePath, options, sys).resolvedModule ?? resolveModuleName(path, fileName, options, sys).resolvedModule;
if (resolvedModule) {
resolvedPaths.push(resolvedModule.resolvedFileName);
break
Expand Down Expand Up @@ -1872,22 +1879,37 @@ namespace Parser {
}
}

if (resolvedPaths.length === 0) {
return;
}
tsPlusResolvedPathsCache.set(options.configFilePath, resolvedPaths);
return resolvedPaths;
}
return [];
}

for (const resolvedPath of resolvedPaths) {
function parseTsPlusExternalTypes(fileName: string, options: CompilerOptions) {
const resolvedPaths = getTsPlusExternalTypesPaths(fileName, options);
if (!resolvedPaths || resolvedPaths.length === 0) {
return
}
for (const resolvedPath of resolvedPaths) {
let json = tsPlusExternalTypeCache.get(resolvedPath);
if (!json) {
const text = sys.readFile(resolvedPath);
if (text) {
const json = JSON.parse(text);
for (const moduleName in json) {
const { resolvedModule } = resolveModuleName(moduleName, resolvedPath, options, sys);
if (resolvedModule && resolvedModule.resolvedFileName === fileName) {
currentTsPlusTypes = json[moduleName];
currentTsPlusFile = moduleName;
return;
}
}
json = JSON.parse(text);
}
}
if (!json) return;
for (const moduleName in json) {
const key = `${options.configFilePath ?? fileName}+${moduleName}`;
let resolvedModule = tsPlusResolvedModuleCache.get(key);
if (!resolvedModule) {
resolvedModule = resolveModuleName(moduleName, resolvedPath, options, sys).resolvedModule ?? resolveModuleName(moduleName, fileName, options, sys).resolvedModule;
tsPlusResolvedModuleCache.set(key, resolvedModule);
}
if (resolvedModule && resolvedModule.resolvedFileName === fileName) {
currentTsPlusTypes = json[moduleName];
currentTsPlusFile = moduleName;
return;
}
}
}
Expand All @@ -1896,6 +1918,15 @@ namespace Parser {
function addTsPlusTagsFromExternalTypes(declaration: VariableDeclaration | FunctionDeclaration | InterfaceDeclaration | ClassDeclaration | TypeAliasDeclaration, jsDocNode?: HasJSDoc): void {
if (currentTsPlusTypes !== null) {
if (declaration.name && declaration.name.kind === SyntaxKind.Identifier) {
if (!jsDocNode) {
jsDocNode = declaration;
}
if (jsDocNode.jsDoc &&
jsDocNode.jsDoc[0] &&
jsDocNode.jsDoc[0].tags &&
jsDocNode.jsDoc[0].tags.find((tag) => tag.tagName.escapedText === 'tsplus')) {
return;
}
const extensions = currentTsPlusTypes.filter(
(type) =>
(declaration.kind === SyntaxKind.VariableDeclaration ? (type.definitionKind === "const")
Expand All @@ -1916,15 +1947,15 @@ namespace Parser {
newTags.push(factory.createJSDocUnknownTag(factory.createIdentifier("tsplus"), comment));
}
newTags.push(factory.createJSDocUnknownTag(factory.createIdentifier("tsplus"), `location "${currentTsPlusFile}"`))
if (!jsDocNode) {
jsDocNode = declaration;
}
if (jsDocNode.jsDoc && jsDocNode.jsDoc[0]) {
const jsDocTags = factory.createNodeArray(Array.from(jsDocNode.jsDoc[0].tags ?? []).concat(newTags))
const jsDocTags = Array.from(jsDocNode.jsDoc[0].tags ?? []).concat(newTags);
// @ts-expect-error
jsDocNode.jsDoc[0].tags = jsDocTags;
} else {
jsDocNode.jsDoc = [factory.createJSDocComment(undefined, newTags)]
jsDocNode.jsDoc[0].tags = factory.createNodeArray(jsDocTags);
jsDocNode.jsDocCache = jsDocTags;
}
else {
jsDocNode.jsDoc = [factory.createJSDocComment(undefined, newTags)];
jsDocNode.jsDocCache = newTags;
}
}
}
Expand Down Expand Up @@ -7797,8 +7828,6 @@ namespace Parser {
const pipeableIndexTags: string[] = [];
let isImplicit = false;

addTsPlusTagsFromExternalTypes(declaration)

for (const doc of jsDoc ?? []) {
if (doc.tags) {
for (const tag of doc.tags) {
Expand Down Expand Up @@ -7940,6 +7969,7 @@ namespace Parser {
const node = factory.createFunctionDeclaration(modifiers, asteriskToken, name, typeParameters, parameters, type, body);
(node as Mutable<FunctionDeclaration>).illegalDecorators = decorators;
const finished = withJSDoc(finishNode(node, pos), hasJSDoc);
addTsPlusTagsFromExternalTypes(finished);
addTsPlusValueTags(finished, finished.jsDoc);
return finished;
}
Expand Down

0 comments on commit 19a26f7

Please sign in to comment.