| 1 |
import { |
| 2 |
isCallExpression, |
| 3 |
isExpressionStatement, |
| 4 |
isFunction, |
| 5 |
isIdentifier, |
| 6 |
isJSXIdentifier, |
| 7 |
isNewExpression, |
| 8 |
isPlaceholder, |
| 9 |
isStatement, |
| 10 |
isStringLiteral, |
| 11 |
removePropertiesDeep, |
| 12 |
traverse, |
| 13 |
} from "@babel/types"; |
| 14 |
import type * as t from "@babel/types"; |
| 15 |
import type { TraversalAncestors, TraversalHandler } from "@babel/types"; |
| 16 |
import { parse } from "@babel/parser"; |
| 17 |
import { codeFrameColumns } from "@babel/code-frame"; |
| 18 |
import type { TemplateOpts, ParserOpts } from "./options"; |
| 19 |
import type { Formatter } from "./formatters"; |
| 20 |
|
| 21 |
export type Metadata = { |
| 22 |
ast: t.File; |
| 23 |
placeholders: Array<Placeholder>; |
| 24 |
placeholderNames: Set<string>; |
| 25 |
}; |
| 26 |
|
| 27 |
type PlaceholderType = "string" | "param" | "statement" | "other"; |
| 28 |
export type Placeholder = { |
| 29 |
name: string; |
| 30 |
resolve: (a: t.File) => { parent: t.Node; key: string; index?: number }; |
| 31 |
type: PlaceholderType; |
| 32 |
isDuplicate: boolean; |
| 33 |
}; |
| 34 |
|
| 35 |
const PATTERN = /^[_$A-Z0-9]+$/; |
| 36 |
|
| 37 |
export default function parseAndBuildMetadata<T>( |
| 38 |
formatter: Formatter<T>, |
| 39 |
code: string, |
| 40 |
opts: TemplateOpts, |
| 41 |
): Metadata { |
| 42 |
const { |
| 43 |
placeholderWhitelist, |
| 44 |
placeholderPattern, |
| 45 |
preserveComments, |
| 46 |
syntacticPlaceholders, |
| 47 |
} = opts; |
| 48 |
|
| 49 |
const ast = parseWithCodeFrame(code, opts.parser, syntacticPlaceholders); |
| 50 |
|
| 51 |
removePropertiesDeep(ast, { |
| 52 |
preserveComments, |
| 53 |
}); |
| 54 |
|
| 55 |
formatter.validate(ast); |
| 56 |
|
| 57 |
const syntactic: MetadataState["syntactic"] = { |
| 58 |
placeholders: [], |
| 59 |
placeholderNames: new Set(), |
| 60 |
}; |
| 61 |
const legacy: MetadataState["legacy"] = { |
| 62 |
placeholders: [], |
| 63 |
placeholderNames: new Set(), |
| 64 |
}; |
| 65 |
const isLegacyRef: MetadataState["isLegacyRef"] = { value: undefined }; |
| 66 |
|
| 67 |
traverse(ast, placeholderVisitorHandler as TraversalHandler<any>, { |
| 68 |
syntactic, |
| 69 |
legacy, |
| 70 |
isLegacyRef, |
| 71 |
placeholderWhitelist, |
| 72 |
placeholderPattern, |
| 73 |
syntacticPlaceholders, |
| 74 |
}); |
| 75 |
|
| 76 |
return { |
| 77 |
ast, |
| 78 |
...(isLegacyRef.value ? legacy : syntactic), |
| 79 |
}; |
| 80 |
} |
| 81 |
|
| 82 |
function placeholderVisitorHandler( |
| 83 |
node: t.Node, |
| 84 |
ancestors: TraversalAncestors, |
| 85 |
state: MetadataState, |
| 86 |
) { |
| 87 |
let name: string; |
| 88 |
|
| 89 |
if (isPlaceholder(node)) { |
| 90 |
if (state.syntacticPlaceholders === false) { |
| 91 |
throw new Error( |
| 92 |
"%%foo%%-style placeholders can't be used when " + |
| 93 |
"'.syntacticPlaceholders' is false.", |
| 94 |
); |
| 95 |
} else { |
| 96 |
name = node.name.name; |
| 97 |
state.isLegacyRef.value = false; |
| 98 |
} |
| 99 |
} else if (state.isLegacyRef.value === false || state.syntacticPlaceholders) { |
| 100 |
return; |
| 101 |
} else if (isIdentifier(node) || isJSXIdentifier(node)) { |
| 102 |
name = node.name; |
| 103 |
state.isLegacyRef.value = true; |
| 104 |
} else if (isStringLiteral(node)) { |
| 105 |
name = node.value; |
| 106 |
state.isLegacyRef.value = true; |
| 107 |
} else { |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
if ( |
| 112 |
!state.isLegacyRef.value && |
| 113 |
(state.placeholderPattern != null || state.placeholderWhitelist != null) |
| 114 |
) { |
| 115 |
// This check is also in options.js. We need it there to handle the default |
| 116 |
// .syntacticPlaceholders behavior. |
| 117 |
throw new Error( |
| 118 |
"'.placeholderWhitelist' and '.placeholderPattern' aren't compatible" + |
| 119 |
" with '.syntacticPlaceholders: true'", |
| 120 |
); |
| 121 |
} |
| 122 |
|
| 123 |
if ( |
| 124 |
state.isLegacyRef.value && |
| 125 |
(state.placeholderPattern === false || |
| 126 |
!(state.placeholderPattern || PATTERN).test(name)) && |
| 127 |
!state.placeholderWhitelist?.has(name) |
| 128 |
) { |
| 129 |
return; |
| 130 |
} |
| 131 |
|
| 132 |
// Keep our own copy of the ancestors so we can use it in .resolve(). |
| 133 |
ancestors = ancestors.slice(); |
| 134 |
|
| 135 |
const { node: parent, key } = ancestors[ancestors.length - 1]; |
| 136 |
|
| 137 |
let type: PlaceholderType; |
| 138 |
if ( |
| 139 |
isStringLiteral(node) || |
| 140 |
isPlaceholder(node, { expectedNode: "StringLiteral" }) |
| 141 |
) { |
| 142 |
type = "string"; |
| 143 |
} else if ( |
| 144 |
(isNewExpression(parent) && key === "arguments") || |
| 145 |
(isCallExpression(parent) && key === "arguments") || |
| 146 |
(isFunction(parent) && key === "params") |
| 147 |
) { |
| 148 |
type = "param"; |
| 149 |
} else if (isExpressionStatement(parent) && !isPlaceholder(node)) { |
| 150 |
type = "statement"; |
| 151 |
ancestors = ancestors.slice(0, -1); |
| 152 |
} else if (isStatement(node) && isPlaceholder(node)) { |
| 153 |
type = "statement"; |
| 154 |
} else { |
| 155 |
type = "other"; |
| 156 |
} |
| 157 |
|
| 158 |
const { placeholders, placeholderNames } = state.isLegacyRef.value |
| 159 |
? state.legacy |
| 160 |
: state.syntactic; |
| 161 |
|
| 162 |
placeholders.push({ |
| 163 |
name, |
| 164 |
type, |
| 165 |
resolve: ast => resolveAncestors(ast, ancestors), |
| 166 |
isDuplicate: placeholderNames.has(name), |
| 167 |
}); |
| 168 |
placeholderNames.add(name); |
| 169 |
} |
| 170 |
|
| 171 |
function resolveAncestors(ast: t.File, ancestors: TraversalAncestors) { |
| 172 |
let parent: t.Node = ast; |
| 173 |
for (let i = 0; i < ancestors.length - 1; i++) { |
| 174 |
const { key, index } = ancestors[i]; |
| 175 |
|
| 176 |
if (index === undefined) { |
| 177 |
parent = (parent as any)[key]; |
| 178 |
} else { |
| 179 |
parent = (parent as any)[key][index]; |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
const { key, index } = ancestors[ancestors.length - 1]; |
| 184 |
|
| 185 |
return { parent, key, index }; |
| 186 |
} |
| 187 |
|
| 188 |
type MetadataState = { |
| 189 |
syntactic: { |
| 190 |
placeholders: Array<Placeholder>; |
| 191 |
placeholderNames: Set<string>; |
| 192 |
}; |
| 193 |
legacy: { |
| 194 |
placeholders: Array<Placeholder>; |
| 195 |
placeholderNames: Set<string>; |
| 196 |
}; |
| 197 |
isLegacyRef: { |
| 198 |
value?: boolean; |
| 199 |
}; |
| 200 |
placeholderWhitelist?: Set<string>; |
| 201 |
placeholderPattern?: RegExp | false; |
| 202 |
syntacticPlaceholders?: boolean; |
| 203 |
}; |
| 204 |
|
| 205 |
function parseWithCodeFrame( |
| 206 |
code: string, |
| 207 |
parserOpts: ParserOpts, |
| 208 |
syntacticPlaceholders?: boolean, |
| 209 |
): t.File { |
| 210 |
const plugins = (parserOpts.plugins || []).slice(); |
| 211 |
if (syntacticPlaceholders !== false) { |
| 212 |
plugins.push("placeholders"); |
| 213 |
} |
| 214 |
|
| 215 |
parserOpts = { |
| 216 |
allowReturnOutsideFunction: true, |
| 217 |
allowSuperOutsideMethod: true, |
| 218 |
sourceType: "module", |
| 219 |
...parserOpts, |
| 220 |
plugins, |
| 221 |
}; |
| 222 |
|
| 223 |
try { |
| 224 |
// @ts-expect-error todo: use babel-types ast typings in Babel parser |
| 225 |
return parse(code, parserOpts); |
| 226 |
} catch (err) { |
| 227 |
const loc = err.loc; |
| 228 |
if (loc) { |
| 229 |
err.message += "\n" + codeFrameColumns(code, { start: loc }); |
| 230 |
err.code = "BABEL_TEMPLATE_PARSE_ERROR"; |
| 231 |
} |
| 232 |
throw err; |
| 233 |
} |
| 234 |
} |