|
| 1 | +/** |
| 2 | + * This module provides utilities for creating Vim expressions in TypeScript. |
| 3 | + * |
| 4 | + * @module |
| 5 | + */ |
| 6 | + |
| 7 | +import type { Predicate } from "@core/unknownutil/type"; |
| 8 | +import { isIntersectionOf } from "@core/unknownutil/is/intersection-of"; |
| 9 | +import { isLiteralOf } from "@core/unknownutil/is/literal-of"; |
| 10 | +import { isObjectOf } from "@core/unknownutil/is/object-of"; |
| 11 | +import { |
| 12 | + isVimEvaluatable, |
| 13 | + type VimEvaluatable, |
| 14 | + vimExpressionOf, |
| 15 | +} from "./vim_evaluatable.ts"; |
| 16 | +import { stringify } from "./stringify.ts"; |
| 17 | + |
| 18 | +/** |
| 19 | + * An object that defines a Vim's expression. |
| 20 | + * |
| 21 | + * Note that although it inherits from primitive `string` for convenience, it |
| 22 | + * actually inherits from the `String` wrapper object. |
| 23 | + * |
| 24 | + * ```typescript |
| 25 | + * import { assertEquals } from "jsr:@std/assert/equals"; |
| 26 | + * import { expr } from "jsr:@denops/std/eval/expression"; |
| 27 | + * |
| 28 | + * const s: string = expr`foo`; |
| 29 | + * assertEquals(typeof s, "object"); // is not "string" |
| 30 | + * ``` |
| 31 | + */ |
| 32 | +export type Expression = string & ExpressionProps; |
| 33 | + |
| 34 | +interface ExpressionProps extends VimEvaluatable { |
| 35 | + /** |
| 36 | + * Used by the `JSON.stringify` to enable the transformation of an object's data to JSON serialization. |
| 37 | + */ |
| 38 | + toJSON(): string; |
| 39 | + |
| 40 | + readonly [Symbol.toStringTag]: "Expression"; |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Create a {@linkcode Expression} that evaluates as a Vim expression. |
| 45 | + * |
| 46 | + * `raw_vim_expression` is a string text representing a raw Vim expression. |
| 47 | + * Backslashes are treated as Vim syntax rather than JavaScript string escape |
| 48 | + * sequences. Note that the raw Vim expression has not been verified and is |
| 49 | + * therefore **UNSAFE**. |
| 50 | + * |
| 51 | + * `${value}` is the expression to be inserted at the current position, whose |
| 52 | + * value is converted to the corresponding Vim expression. The conversion is |
| 53 | + * safe and an error is thrown if it cannot be converted. Note that if the |
| 54 | + * value contains `Expression` it will be inserted as-is, this is **UNSAFE**. |
| 55 | + * |
| 56 | + * ```typescript |
| 57 | + * import { assertEquals } from "jsr:@std/assert/equals"; |
| 58 | + * import { expr } from "jsr:@denops/std/eval/expression"; |
| 59 | + * |
| 60 | + * assertEquals( |
| 61 | + * expr`raw_vim_expression`.toString(), |
| 62 | + * "raw_vim_expression", |
| 63 | + * ); |
| 64 | + * |
| 65 | + * const value = ["foo", 123, null]; |
| 66 | + * assertEquals( |
| 67 | + * expr`raw_vim_expression + ${value}`.toString(), |
| 68 | + * "raw_vim_expression + ['foo',123,v:null]" |
| 69 | + * ); |
| 70 | + * ``` |
| 71 | + */ |
| 72 | +export function expr( |
| 73 | + template: TemplateStringsArray, |
| 74 | + ...substitutions: TemplateSubstitutions |
| 75 | +): Expression { |
| 76 | + const values = substitutions.map(stringify); |
| 77 | + const raw = String.raw(template, ...values); |
| 78 | + return new ExpressionImpl(raw) as unknown as Expression; |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Returns `true` if the value is a {@linkcode Expression}. |
| 83 | + * |
| 84 | + * ```typescript |
| 85 | + * import { assert, assertFalse } from "jsr:@std/assert"; |
| 86 | + * import { isExpression, expr } from "jsr:@denops/std/eval/expression"; |
| 87 | + * |
| 88 | + * assert(isExpression(expr`123`)); |
| 89 | + * |
| 90 | + * assertFalse(isExpression("456")); |
| 91 | + * ``` |
| 92 | + */ |
| 93 | +export const isExpression: Predicate<Expression> = isIntersectionOf([ |
| 94 | + // NOTE: Do not check `isString` here, because `Expression` has a different type in definition (primitive `string`) and implementation (`String`). |
| 95 | + isObjectOf({ |
| 96 | + [Symbol.toStringTag]: isLiteralOf("Expression"), |
| 97 | + }), |
| 98 | + isVimEvaluatable, |
| 99 | +]) as unknown as Predicate<Expression>; |
| 100 | + |
| 101 | +// deno-lint-ignore no-explicit-any |
| 102 | +type TemplateSubstitutions = any[]; |
| 103 | + |
| 104 | +class ExpressionImpl extends String implements ExpressionProps { |
| 105 | + get [Symbol.toStringTag]() { |
| 106 | + return "Expression" as const; |
| 107 | + } |
| 108 | + |
| 109 | + [vimExpressionOf](): string { |
| 110 | + return this.valueOf(); |
| 111 | + } |
| 112 | + |
| 113 | + toJSON(): string { |
| 114 | + return this[vimExpressionOf](); |
| 115 | + } |
| 116 | + |
| 117 | + [Symbol.for("Deno.customInspect")]() { |
| 118 | + return `[${this[Symbol.toStringTag]}: ${Deno.inspect(this.valueOf())}]`; |
| 119 | + } |
| 120 | +} |
0 commit comments