| 1 | import { declarescip-typescript npm @babel/helper-plugin-utils 7.20.2 src/`index.ts`/declare(). } from "@babel/helper-plugin-utils"scip-typescript npm @babel/helper-plugin-utils 7.20.2 src/`index.ts`/; |
| 2 | import { template, types as tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/ } from "@babel/core"scip-typescript npm @babel/core 7.20.12 src/`index.ts`/; |
| 3 | import type { NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath# } from "@babel/traverse"scip-typescript npm @babel/traverse 7.20.13 src/`index.ts`/; |
| 4 | |
| 5 | export interface Optionsscip-typescript npm @babel/plugin-transform-template-literals 7.18.9 src/`index.ts`/Options# { |
| 6 | loosescip-typescript npm @babel/plugin-transform-template-literals 7.18.9 src/`index.ts`/Options#loose.?: boolean; |
| 7 | } |
| 8 | |
| 9 | export default declarescip-typescript npm @babel/helper-plugin-utils 7.20.2 src/`index.ts`/declare().((api, options: Optionsscip-typescript npm @babel/plugin-transform-template-literals 7.18.9 src/`index.ts`/Options#) => { |
| 10 | api.assertVersionscip-typescript npm @babel/core 7.20.12 src/config/helpers/`config-api.ts`/ConfigAPI#typeLiteral7:assertVersion.(7); |
| 11 | |
| 12 | const ignoreToPrimitiveHint = |
| 13 | api.assumption("ignoreToPrimitiveHint") ?? options.loosescip-typescript npm @babel/plugin-transform-template-literals 7.18.9 src/`index.ts`/Options#loose.; |
| 14 | const mutableTemplateObject = |
| 15 | api.assumption("mutableTemplateObject") ?? options.loosescip-typescript npm @babel/plugin-transform-template-literals 7.18.9 src/`index.ts`/Options#loose.; |
| 16 | |
| 17 | let helperName = "taggedTemplateLiteral"; |
| 18 | if (mutableTemplateObject) helperName += "Loose"; |
| 19 | |
| 20 | /** |
| 21 | * This function groups the objects into multiple calls to `.concat()` in |
| 22 | * order to preserve execution order of the primitive conversion, e.g. |
| 23 | * |
| 24 | * "".concat(obj.foo, "foo", obj2.foo, "foo2") |
| 25 | * |
| 26 | * would evaluate both member expressions _first_ then, `concat` will |
| 27 | * convert each one to a primitive, whereas |
| 28 | * |
| 29 | * "".concat(obj.foo, "foo").concat(obj2.foo, "foo2") |
| 30 | * |
| 31 | * would evaluate the member, then convert it to a primitive, then evaluate |
| 32 | * the second member and convert that one, which reflects the spec behavior |
| 33 | * of template literals. |
| 34 | */ |
| 35 | function buildConcatCallExpressions(items: tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.Expressionscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Expression#[]): tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.CallExpressionscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/CallExpression# { |
| 36 | let avail = true; |
| 37 | // @ts-expect-error items must not be empty |
| 38 | return items.reducescip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#reduce().(function (left, right) { |
| 39 | let canBeInserted = tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.isLiteralscip-typescript npm @babel/types 7.20.7 src/validators/generated/`index.ts`/isLiteral().(right); |
| 40 | |
| 41 | if (!canBeInserted && avail) { |
| 42 | canBeInserted = true; |
| 43 | avail = false; |
| 44 | } |
| 45 | if (canBeInserted && tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.isCallExpressionscip-typescript npm @babel/types 7.20.7 src/validators/generated/`index.ts`/isCallExpression().(left)) { |
| 46 | left.argumentsscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/CallExpression#arguments..pushscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#push().(right); |
| 47 | return left; |
| 48 | } |
| 49 | return tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.callExpressionscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/callExpression().( |
| 50 | tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.memberExpressionscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/memberExpression().(left, tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.identifierscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/identifier().("concat")), |
| 51 | [right], |
| 52 | ); |
| 53 | }); |
| 54 | } |
| 55 | |
| 56 | return { |
| 57 | namescip-typescript npm @babel/core 7.20.12 src/config/validation/`plugins.ts`/PluginObject#typeLiteral0:name.: "transform-template-literals", |
| 58 | |
| 59 | visitorscip-typescript npm @babel/core 7.20.12 src/config/validation/`plugins.ts`/PluginObject#typeLiteral0:visitor.: { |
| 60 | TaggedTemplateExpression(path) { |
| 61 | const { nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node. } = path; |
| 62 | const { quasiscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/TaggedTemplateExpression#quasi. } = node; |
| 63 | |
| 64 | const strings = []; |
| 65 | const raws = []; |
| 66 | |
| 67 | // Flag variable to check if contents of strings and raw are equal |
| 68 | let isStringsRawEqual = true; |
| 69 | |
| 70 | for (const elem of quasi.quasisscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/TemplateLiteral#quasis.) { |
| 71 | const { rawscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/TemplateElement#value.typeLiteral267:raw., cookedscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/TemplateElement#value.typeLiteral267:cooked. } = elem.valuescip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/TemplateElement#value.; |
| 72 | const value = |
| 73 | cooked == null |
| 74 | ? path.scopescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#scope..buildUndefinedNodescip-typescript npm @babel/traverse 7.20.13 src/scope/`index.ts`/Scope#buildUndefinedNode().() |
| 75 | : tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.stringLiteralscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/stringLiteral().(cooked); |
| 76 | |
| 77 | strings.pushscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#push().(value); |
| 78 | raws.pushscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#push().(tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.stringLiteralscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/stringLiteral().(raw)); |
| 79 | |
| 80 | if (raw !== cooked) { |
| 81 | // false even if one of raw and cooked are not equal |
| 82 | isStringsRawEqual = false; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | const helperArgs = [tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.arrayExpressionscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/arrayExpression().(strings)]; |
| 87 | // only add raw arrayExpression if there is any difference between raws and strings |
| 88 | if (!isStringsRawEqual) { |
| 89 | helperArgs.pushscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#push().(tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.arrayExpressionscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/arrayExpression().(raws)); |
| 90 | } |
| 91 | |
| 92 | const tmp = path.scopescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#scope..generateUidIdentifierscip-typescript npm @babel/traverse 7.20.13 src/scope/`index.ts`/Scope#generateUidIdentifier().("templateObject"); |
| 93 | path.scopescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#scope..getProgramParentscip-typescript npm @babel/traverse 7.20.13 src/scope/`index.ts`/Scope#getProgramParent().().pushscip-typescript npm @babel/traverse 7.20.13 src/scope/`index.ts`/Scope#push().({ idscip-typescript npm @babel/traverse 7.20.13 src/scope/`index.ts`/Scope#push().(opts)typeLiteral191:id.: tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.cloneNode(tmp) }); |
| 94 | |
| 95 | path.replaceWithscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWith().( |
| 96 | tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.callExpressionscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/callExpression().(node.tagscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/TaggedTemplateExpression#tag., [ |
| 97 | template.expressionscip-typescript npm @babel/template 7.20.7 src/`index.ts`/expression0:.astscip-typescript npm @babel/template 7.20.7 src/`builder.ts`/TemplateBuilder#typeLiteral0:ast.` |
| 98 | ${tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.cloneNode(tmp)} || ( |
| 99 | ${tmp} = ${this.addHelperscip-typescript npm @babel/core 7.20.12 src/transformation/`plugin-pass.ts`/PluginPass#addHelper().(helperName)}(${helperArgs}) |
| 100 | ) |
| 101 | `, |
| 102 | // @ts-expect-error Fixme: quasi.expressions may contain TSAnyKeyword |
| 103 | ...quasi.expressionsscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/TemplateLiteral#expressions., |
| 104 | ]), |
| 105 | ); |
| 106 | }, |
| 107 | |
| 108 | TemplateLiteral(path) { |
| 109 | // Skip TemplateLiteral in TSLiteralType |
| 110 | if (path.parentscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#parent..typescip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/ArrayExpression#type. === "TSLiteralType") { |
| 111 | return; |
| 112 | } |
| 113 | const nodes: tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.Expressionscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Expression#[] = []; |
| 114 | const expressions = path.get("expressions") as NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#<tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.Expressionscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Expression#>[]; |
| 115 | |
| 116 | let index = 0; |
| 117 | for (const elem of path.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node..quasisscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/TemplateLiteral#quasis.) { |
| 118 | if (elem.valuescip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/TemplateElement#value..cookedscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/TemplateElement#value.typeLiteral267:cooked.) { |
| 119 | nodes.pushscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#push().(tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.stringLiteralscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/stringLiteral().(elem.valuescip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/TemplateElement#value..cookedscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/TemplateElement#value.typeLiteral267:cooked.)); |
| 120 | } |
| 121 | |
| 122 | if (index < expressions.lengthscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#length.) { |
| 123 | const expr = expressions[index++]; |
| 124 | const node = expr.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node.; |
| 125 | if (!tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.isStringLiteralscip-typescript npm @babel/types 7.20.7 src/validators/generated/`index.ts`/isStringLiteral().(node, { valuescip-typescript npm @babel/plugin-transform-template-literals 7.18.9 src/`index.ts`/value0:: "" })) { |
| 126 | nodes.pushscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#push().(node); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // since `+` is left-to-right associative |
| 132 | // ensure the first node is a string if first/second isn't |
| 133 | if ( |
| 134 | !tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.isStringLiteralscip-typescript npm @babel/types 7.20.7 src/validators/generated/`index.ts`/isStringLiteral().(nodes[0]) && |
| 135 | !(ignoreToPrimitiveHint && tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.isStringLiteralscip-typescript npm @babel/types 7.20.7 src/validators/generated/`index.ts`/isStringLiteral().(nodes[1])) |
| 136 | ) { |
| 137 | nodes.unshiftscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#unshift().(tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.stringLiteralscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/stringLiteral().("")); |
| 138 | } |
| 139 | let root = nodes[0]; |
| 140 | |
| 141 | if (ignoreToPrimitiveHint) { |
| 142 | for (let i = 1; i < nodes.lengthscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#length.; i++) { |
| 143 | root = tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.binaryExpressionscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/binaryExpression().("+", root, nodes[i]); |
| 144 | } |
| 145 | } else if (nodes.lengthscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#length. > 1) { |
| 146 | root = buildConcatCallExpressions(nodes); |
| 147 | } |
| 148 | |
| 149 | path.replaceWithscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWith().(root); |
| 150 | }, |
| 151 | }, |
| 152 | }; |
| 153 | }); |