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: handle const Type Parameters #519

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"@phenomnomnominal/tsquery": "^6.1.3",
"@release-it/conventional-changelog": "^8.0.1",
"@types/eslint": "^8.56.5",
"@types/semver": "^7.5.8",
"@typescript-eslint/eslint-plugin": "^7.3.1",
"@typescript-eslint/parser": "^7.3.1",
"@typescript/vfs": "^1.5.0",
Expand Down Expand Up @@ -81,6 +82,7 @@
"prettier-plugin-curly": "^0.2.1",
"prettier-plugin-packagejson": "^2.4.7",
"release-it": "^17.0.1",
"semver": "^7.6.2",
"sentences-per-line": "^0.2.1",
"should-semantic-release": "^0.3.0",
"tsup": "^8.0.2",
Expand Down
67 changes: 43 additions & 24 deletions pnpm-lock.yaml

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

11 changes: 11 additions & 0 deletions src/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ export const isSymbolFlagSet: (
flag: ts.SymbolFlags,
) => boolean = isFlagSetOnObject;

/**
* Test if the given symbol's links has the given `CheckFlags` set.
* @internal
*/
export function isTransientSymbolLinksFlagSet(
links: ts.TransientSymbolLinks,
flag: ts.CheckFlags,
): boolean {
return isFlagSet(links.checkFlags, flag);
}

/**
* Test if the given node has the given `TypeFlags` set.
* @category Nodes - Flag Utilities
Expand Down
47 changes: 45 additions & 2 deletions src/nodes/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import ts from "typescript";

import { isTransientSymbolLinksFlagSet } from "../flags";
import {
isConstAssertionExpression,
isEntityNameExpression,
Expand All @@ -28,10 +29,13 @@ export function isBindableObjectDefinePropertyCall(
}

/**
* Detects whether an expression is affected by an enclosing `as const` assertion and therefore treated literally.
* Detects whether the property assignment is affected by an enclosing `as const` assertion or const type parameter and therefore treated literally.
* @internal
*/
export function isInConstContext(node: ts.Expression): boolean {
export function isInConstContext(
node: ts.PropertyAssignment | ts.ShorthandPropertyAssignment,
typeChecker: ts.TypeChecker,
): boolean {
let current: ts.Node = node;
while (true) {
const parent = current.parent;
Expand Down Expand Up @@ -69,6 +73,45 @@ export function isInConstContext(node: ts.Expression): boolean {
case ts.SyntaxKind.TemplateExpression:
current = parent;
break;
case ts.SyntaxKind.CallExpression:
if (!ts.isExpression(current)) {
return false;
}

const functionSignature = typeChecker.getResolvedSignature(
parent as ts.CallExpression,
);
if (functionSignature === undefined) {
return false;
}

const argumentIndex = (parent as ts.CallExpression).arguments.indexOf(
current,
);
if (argumentIndex < 0) {
return false;
}

const parameterSymbol =
functionSignature.getParameters()[argumentIndex];
if (parameterSymbol === undefined || !("links" in parameterSymbol)) {
return false;
}

const parameterSymbolLinks = (parameterSymbol as ts.TransientSymbol)
.links;

const propertySymbol =
parameterSymbolLinks.type?.getProperties()?.[argumentIndex];
if (propertySymbol === undefined || !("links" in propertySymbol)) {
return false;
}

// I believe we only need to check one level deep, regardless of how deep `node` is.
return isTransientSymbolLinksFlagSet(
(propertySymbol as ts.TransientSymbol).links,
ts.CheckFlags.Readonly,
);
default:
return false;
}
Expand Down
Loading
Loading