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