Skip to content

Commit

Permalink
add isLiteralExpressionValue utility
Browse files Browse the repository at this point in the history
  • Loading branch information
souporserious committed Jun 21, 2024
1 parent 81f9c69 commit 9748dfe
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
9 changes: 9 additions & 0 deletions .changeset/ten-geckos-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@tsxmod/utils': minor
---

Adds `isLiteralExpressionValue` utility to help determine if a literal expression value was resolved or not since `undefined` is a valid value.

```ts

```
1 change: 1 addition & 0 deletions packages/utils/src/expressions/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export {
resolveLiteralExpression,
isLiteralExpressionValue,
type LiteralExpressionValue,
} from './resolveLiteralExpression'
export { resolveObjectLiteralExpression } from './resolveObjectLiteralExpression'
16 changes: 15 additions & 1 deletion packages/utils/src/expressions/resolveLiteralExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ export type LiteralExpressionValue =
| Record<string, any>
| LiteralExpressionValue[]

const EMPTY_LITERAL_EXPRESSION_VALUE = Symbol('EMPTY_LITERAL_EXPRESSION_VALUE')

/** Recursively resolves an expression into a literal value. */
export function resolveLiteralExpression(
expression: Expression
): LiteralExpressionValue | LiteralExpressionValue[] | undefined {
):
| LiteralExpressionValue
| LiteralExpressionValue[]
| typeof EMPTY_LITERAL_EXPRESSION_VALUE {
if (Node.isNullLiteral(expression)) {
return null
}
Expand Down Expand Up @@ -65,4 +70,13 @@ export function resolveLiteralExpression(
if (Node.isSpreadElement(expression) || Node.isAsExpression(expression)) {
return resolveLiteralExpression(expression.getExpression())
}

return EMPTY_LITERAL_EXPRESSION_VALUE
}

/** Determines when a value was resolved in `resolveLiteralExpression`. */
export function isLiteralExpressionValue(
value: ReturnType<typeof resolveLiteralExpression>
) {
return value !== EMPTY_LITERAL_EXPRESSION_VALUE
}

0 comments on commit 9748dfe

Please sign in to comment.