UNPKG

5.2 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = parseAndBuildMetadata;
7
8var t = _interopRequireWildcard(require("@babel/types"));
9
10var _parser = require("@babel/parser");
11
12var _codeFrame = require("@babel/code-frame");
13
14function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
15
16function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
17
18const PATTERN = /^[_$A-Z0-9]+$/;
19
20function parseAndBuildMetadata(formatter, code, opts) {
21 const ast = parseWithCodeFrame(code, opts.parser);
22 const {
23 placeholderWhitelist,
24 placeholderPattern,
25 preserveComments,
26 syntacticPlaceholders
27 } = opts;
28 t.removePropertiesDeep(ast, {
29 preserveComments
30 });
31 formatter.validate(ast);
32 const syntactic = {
33 placeholders: [],
34 placeholderNames: new Set()
35 };
36 const legacy = {
37 placeholders: [],
38 placeholderNames: new Set()
39 };
40 const isLegacyRef = {
41 value: undefined
42 };
43 t.traverse(ast, placeholderVisitorHandler, {
44 syntactic,
45 legacy,
46 isLegacyRef,
47 placeholderWhitelist,
48 placeholderPattern,
49 syntacticPlaceholders
50 });
51 return Object.assign({
52 ast
53 }, isLegacyRef.value ? legacy : syntactic);
54}
55
56function placeholderVisitorHandler(node, ancestors, state) {
57 var _state$placeholderWhi;
58
59 let name;
60
61 if (t.isPlaceholder(node)) {
62 if (state.syntacticPlaceholders === false) {
63 throw new Error("%%foo%%-style placeholders can't be used when " + "'.syntacticPlaceholders' is false.");
64 } else {
65 name = node.name.name;
66 state.isLegacyRef.value = false;
67 }
68 } else if (state.isLegacyRef.value === false || state.syntacticPlaceholders) {
69 return;
70 } else if (t.isIdentifier(node) || t.isJSXIdentifier(node)) {
71 name = node.name;
72 state.isLegacyRef.value = true;
73 } else if (t.isStringLiteral(node)) {
74 name = node.value;
75 state.isLegacyRef.value = true;
76 } else {
77 return;
78 }
79
80 if (!state.isLegacyRef.value && (state.placeholderPattern != null || state.placeholderWhitelist != null)) {
81 throw new Error("'.placeholderWhitelist' and '.placeholderPattern' aren't compatible" + " with '.syntacticPlaceholders: true'");
82 }
83
84 if (state.isLegacyRef.value && (state.placeholderPattern === false || !(state.placeholderPattern || PATTERN).test(name)) && !((_state$placeholderWhi = state.placeholderWhitelist) == null ? void 0 : _state$placeholderWhi.has(name))) {
85 return;
86 }
87
88 ancestors = ancestors.slice();
89 const {
90 node: parent,
91 key
92 } = ancestors[ancestors.length - 1];
93 let type;
94
95 if (t.isStringLiteral(node) || t.isPlaceholder(node, {
96 expectedNode: "StringLiteral"
97 })) {
98 type = "string";
99 } else if (t.isNewExpression(parent) && key === "arguments" || t.isCallExpression(parent) && key === "arguments" || t.isFunction(parent) && key === "params") {
100 type = "param";
101 } else if (t.isExpressionStatement(parent) && !t.isPlaceholder(node)) {
102 type = "statement";
103 ancestors = ancestors.slice(0, -1);
104 } else if (t.isStatement(node) && t.isPlaceholder(node)) {
105 type = "statement";
106 } else {
107 type = "other";
108 }
109
110 const {
111 placeholders,
112 placeholderNames
113 } = state.isLegacyRef.value ? state.legacy : state.syntactic;
114 placeholders.push({
115 name,
116 type,
117 resolve: ast => resolveAncestors(ast, ancestors),
118 isDuplicate: placeholderNames.has(name)
119 });
120 placeholderNames.add(name);
121}
122
123function resolveAncestors(ast, ancestors) {
124 let parent = ast;
125
126 for (let i = 0; i < ancestors.length - 1; i++) {
127 const {
128 key,
129 index
130 } = ancestors[i];
131
132 if (index === undefined) {
133 parent = parent[key];
134 } else {
135 parent = parent[key][index];
136 }
137 }
138
139 const {
140 key,
141 index
142 } = ancestors[ancestors.length - 1];
143 return {
144 parent,
145 key,
146 index
147 };
148}
149
150function parseWithCodeFrame(code, parserOpts) {
151 parserOpts = Object.assign(Object.assign({
152 allowReturnOutsideFunction: true,
153 allowSuperOutsideMethod: true,
154 sourceType: "module"
155 }, parserOpts), {}, {
156 plugins: (parserOpts.plugins || []).concat("placeholders")
157 });
158
159 try {
160 return (0, _parser.parse)(code, parserOpts);
161 } catch (err) {
162 const loc = err.loc;
163
164 if (loc) {
165 err.message += "\n" + (0, _codeFrame.codeFrameColumns)(code, {
166 start: loc
167 });
168 err.code = "BABEL_TEMPLATE_PARSE_ERROR";
169 }
170
171 throw err;
172 }
173}
\No newline at end of file