source: imaps-frontend/node_modules/@babel/template/lib/parse.js@ 0c6b92a

main
Last change on this file since 0c6b92a was d565449, checked in by stefan toskovski <stefantoska84@…>, 3 months ago

Update repo after prototype presentation

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