Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: default to first declaration for getWellKnownSymbolPropertyOfType properties #16

Merged
merged 9 commits into from
Feb 19, 2023
10 changes: 10 additions & 0 deletions src/nodes/typeGuards/compound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ export function isJsxTagNamePropertyAccess(
);
}

/**
* @remarks This always returns true.
* @see /~https://github.com/JoshuaKGoldberg/ts-api-utils/pull/16#discussion_r1106579774
*/
export function isNamedDeclaration(
node: ts.Declaration
): node is ts.NamedDeclaration {
return true;
}
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
RebeccaStevens marked this conversation as resolved.
Show resolved Hide resolved

export function isNamespaceDeclaration(
node: ts.Node
): node is ts.NamespaceDeclaration {
Expand Down
2 changes: 1 addition & 1 deletion src/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function createSourceFileAndTypeChecker(
sourceText: string,
fileName = "file.ts"
): SourceFileAndTypeChecker {
const compilerOptions = {};
const compilerOptions = { target: ts.ScriptTarget.ES2018 };
RebeccaStevens marked this conversation as resolved.
Show resolved Hide resolved
const fsMap = tsvfs
.createDefaultMapFromNodeModules(compilerOptions)
.set(fileName, sourceText);
Expand Down
27 changes: 27 additions & 0 deletions src/types/getters.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import * as ts from "typescript";
import { describe, expect, it } from "vitest";

import { createSourceFileAndTypeChecker } from "../test/utils.js";
import { getWellKnownSymbolPropertyOfType } from "./getters.js";

describe("getWellKnownSymbolPropertyOfType", () => {
// /~https://github.com/JoshuaKGoldberg/ts-api-tools/issues/15
it("gets the property when it does not have a value declaration", () => {
const { sourceFile, typeChecker } = createSourceFileAndTypeChecker(`
declare const x: Omit<{
[Symbol.asyncIterator](): AsyncIterator<any>;
}, 'z'>
`);

const node = (sourceFile.statements[0] as ts.VariableStatement)
.declarationList.declarations[0].name;

const type = typeChecker.getTypeAtLocation(node);

expect(
getWellKnownSymbolPropertyOfType(type, "asyncIterator", typeChecker)
).toMatchObject({
name: /^__@asyncIterator/,
});
});
});
21 changes: 14 additions & 7 deletions src/types/getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import * as ts from "typescript";

import { isNamedDeclaration } from "../nodes/typeGuards/index.js";
import {
isIntersectionType,
isUnionType,
Expand Down Expand Up @@ -50,15 +51,21 @@ export function getWellKnownSymbolPropertyOfType(
const prefix = "__@" + wellKnownSymbolName;

for (const prop of type.getProperties()) {
if (!prop.name.startsWith(prefix)) continue;
if (!prop.name.startsWith(prefix)) {
continue;
}

const declaration = prop.valueDeclaration ?? prop.getDeclarations()![0];
if (
!isNamedDeclaration(declaration) ||
declaration.name === undefined ||
!ts.isComputedPropertyName(declaration.name)
) {
continue;
}

const globalSymbol = typeChecker.getApparentType(
typeChecker.getTypeAtLocation(
(
(prop.valueDeclaration as ts.NamedDeclaration)
.name as ts.ComputedPropertyName
).expression
)
typeChecker.getTypeAtLocation(declaration.name.expression)
).symbol;

if (
Expand Down