1 |
|
2 |
|
3 |
|
4 | import XHTMLEntities from "../parser/plugins/jsx/xhtml";
|
5 | import {TokenType as tt} from "../parser/tokenizer/types";
|
6 | import {charCodes} from "../parser/util/charcodes";
|
7 |
|
8 | import getJSXPragmaInfo, {} from "../util/getJSXPragmaInfo";
|
9 |
|
10 | import Transformer from "./Transformer";
|
11 |
|
12 | const HEX_NUMBER = /^[\da-fA-F]+$/;
|
13 | const DECIMAL_NUMBER = /^\d+$/;
|
14 |
|
15 | export default class JSXTransformer extends Transformer {
|
16 | __init() {this.lastLineNumber = 1}
|
17 | __init2() {this.lastIndex = 0}
|
18 | __init3() {this.filenameVarName = null}
|
19 |
|
20 |
|
21 | constructor(
|
22 | rootTransformer,
|
23 | tokens,
|
24 | importProcessor,
|
25 | nameManager,
|
26 | options,
|
27 | ) {
|
28 | super();this.rootTransformer = rootTransformer;this.tokens = tokens;this.importProcessor = importProcessor;this.nameManager = nameManager;this.options = options;JSXTransformer.prototype.__init.call(this);JSXTransformer.prototype.__init2.call(this);JSXTransformer.prototype.__init3.call(this);;
|
29 | this.jsxPragmaInfo = getJSXPragmaInfo(options);
|
30 | }
|
31 |
|
32 | process() {
|
33 | if (this.tokens.matches1(tt.jsxTagStart)) {
|
34 | this.processJSXTag();
|
35 | return true;
|
36 | }
|
37 | return false;
|
38 | }
|
39 |
|
40 | getPrefixCode() {
|
41 | if (this.filenameVarName) {
|
42 | return `const ${this.filenameVarName} = ${JSON.stringify(this.options.filePath || "")};`;
|
43 | } else {
|
44 | return "";
|
45 | }
|
46 | }
|
47 |
|
48 | |
49 |
|
50 |
|
51 |
|
52 | getLineNumberForIndex(index) {
|
53 | const code = this.tokens.code;
|
54 | while (this.lastIndex < index && this.lastIndex < code.length) {
|
55 | if (code[this.lastIndex] === "\n") {
|
56 | this.lastLineNumber++;
|
57 | }
|
58 | this.lastIndex++;
|
59 | }
|
60 | return this.lastLineNumber;
|
61 | }
|
62 |
|
63 | getFilenameVarName() {
|
64 | if (!this.filenameVarName) {
|
65 | this.filenameVarName = this.nameManager.claimFreeName("_jsxFileName");
|
66 | }
|
67 | return this.filenameVarName;
|
68 | }
|
69 |
|
70 | processProps(firstTokenStart) {
|
71 | const lineNumber = this.getLineNumberForIndex(firstTokenStart);
|
72 | const devProps = this.options.production
|
73 | ? ""
|
74 | : `__self: this, __source: {fileName: ${this.getFilenameVarName()}, lineNumber: ${lineNumber}}`;
|
75 | if (!this.tokens.matches1(tt.jsxName) && !this.tokens.matches1(tt.braceL)) {
|
76 | if (devProps) {
|
77 | this.tokens.appendCode(`, {${devProps}}`);
|
78 | } else {
|
79 | this.tokens.appendCode(`, null`);
|
80 | }
|
81 | return;
|
82 | }
|
83 | this.tokens.appendCode(`, {`);
|
84 | while (true) {
|
85 | if (this.tokens.matches2(tt.jsxName, tt.eq)) {
|
86 | this.processPropKeyName();
|
87 | this.tokens.replaceToken(": ");
|
88 | if (this.tokens.matches1(tt.braceL)) {
|
89 | this.tokens.replaceToken("");
|
90 | this.rootTransformer.processBalancedCode();
|
91 | this.tokens.replaceToken("");
|
92 | } else if (this.tokens.matches1(tt.jsxTagStart)) {
|
93 | this.processJSXTag();
|
94 | } else {
|
95 | this.processStringPropValue();
|
96 | }
|
97 | } else if (this.tokens.matches1(tt.jsxName)) {
|
98 | this.processPropKeyName();
|
99 | this.tokens.appendCode(": true");
|
100 | } else if (this.tokens.matches1(tt.braceL)) {
|
101 | this.tokens.replaceToken("");
|
102 | this.rootTransformer.processBalancedCode();
|
103 | this.tokens.replaceToken("");
|
104 | } else {
|
105 | break;
|
106 | }
|
107 | this.tokens.appendCode(",");
|
108 | }
|
109 | if (devProps) {
|
110 | this.tokens.appendCode(` ${devProps}}`);
|
111 | } else {
|
112 | this.tokens.appendCode("}");
|
113 | }
|
114 | }
|
115 |
|
116 | processPropKeyName() {
|
117 | const keyName = this.tokens.identifierName();
|
118 | if (keyName.includes("-")) {
|
119 | this.tokens.replaceToken(`'${keyName}'`);
|
120 | } else {
|
121 | this.tokens.copyToken();
|
122 | }
|
123 | }
|
124 |
|
125 | processStringPropValue() {
|
126 | const token = this.tokens.currentToken();
|
127 | const valueCode = this.tokens.code.slice(token.start + 1, token.end - 1);
|
128 | const replacementCode = formatJSXTextReplacement(valueCode);
|
129 | const literalCode = formatJSXStringValueLiteral(valueCode);
|
130 | this.tokens.replaceToken(literalCode + replacementCode);
|
131 | }
|
132 |
|
133 | |
134 |
|
135 |
|
136 | processTagIntro() {
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 | let introEnd = this.tokens.currentIndex() + 1;
|
144 | while (
|
145 | this.tokens.tokens[introEnd].isType ||
|
146 | (!this.tokens.matches2AtIndex(introEnd - 1, tt.jsxName, tt.jsxName) &&
|
147 | !this.tokens.matches2AtIndex(introEnd - 1, tt.greaterThan, tt.jsxName) &&
|
148 | !this.tokens.matches1AtIndex(introEnd, tt.braceL) &&
|
149 | !this.tokens.matches1AtIndex(introEnd, tt.jsxTagEnd) &&
|
150 | !this.tokens.matches2AtIndex(introEnd, tt.slash, tt.jsxTagEnd))
|
151 | ) {
|
152 | introEnd++;
|
153 | }
|
154 | if (introEnd === this.tokens.currentIndex() + 1) {
|
155 | const tagName = this.tokens.identifierName();
|
156 | if (startsWithLowerCase(tagName)) {
|
157 | this.tokens.replaceToken(`'${tagName}'`);
|
158 | }
|
159 | }
|
160 | while (this.tokens.currentIndex() < introEnd) {
|
161 | this.rootTransformer.processToken();
|
162 | }
|
163 | }
|
164 |
|
165 | processChildren() {
|
166 | while (true) {
|
167 | if (this.tokens.matches2(tt.jsxTagStart, tt.slash)) {
|
168 |
|
169 | return;
|
170 | }
|
171 | if (this.tokens.matches1(tt.braceL)) {
|
172 | if (this.tokens.matches2(tt.braceL, tt.braceR)) {
|
173 |
|
174 |
|
175 | this.tokens.replaceToken("");
|
176 | this.tokens.replaceToken("");
|
177 | } else {
|
178 |
|
179 | this.tokens.replaceToken(", ");
|
180 | this.rootTransformer.processBalancedCode();
|
181 | this.tokens.replaceToken("");
|
182 | }
|
183 | } else if (this.tokens.matches1(tt.jsxTagStart)) {
|
184 |
|
185 | this.tokens.appendCode(", ");
|
186 | this.processJSXTag();
|
187 | } else if (this.tokens.matches1(tt.jsxText)) {
|
188 | this.processChildTextElement();
|
189 | } else {
|
190 | throw new Error("Unexpected token when processing JSX children.");
|
191 | }
|
192 | }
|
193 | }
|
194 |
|
195 | processChildTextElement() {
|
196 | const token = this.tokens.currentToken();
|
197 | const valueCode = this.tokens.code.slice(token.start, token.end);
|
198 | const replacementCode = formatJSXTextReplacement(valueCode);
|
199 | const literalCode = formatJSXTextLiteral(valueCode);
|
200 | if (literalCode === '""') {
|
201 | this.tokens.replaceToken(replacementCode);
|
202 | } else {
|
203 | this.tokens.replaceToken(`, ${literalCode}${replacementCode}`);
|
204 | }
|
205 | }
|
206 |
|
207 | processJSXTag() {
|
208 | const {jsxPragmaInfo} = this;
|
209 | const resolvedPragmaBaseName = this.importProcessor
|
210 | ? this.importProcessor.getIdentifierReplacement(jsxPragmaInfo.base) || jsxPragmaInfo.base
|
211 | : jsxPragmaInfo.base;
|
212 | const firstTokenStart = this.tokens.currentToken().start;
|
213 |
|
214 | this.tokens.replaceToken(`${resolvedPragmaBaseName}${jsxPragmaInfo.suffix}(`);
|
215 |
|
216 | if (this.tokens.matches1(tt.jsxTagEnd)) {
|
217 |
|
218 | const resolvedFragmentPragmaBaseName = this.importProcessor
|
219 | ? this.importProcessor.getIdentifierReplacement(jsxPragmaInfo.fragmentBase) ||
|
220 | jsxPragmaInfo.fragmentBase
|
221 | : jsxPragmaInfo.fragmentBase;
|
222 | this.tokens.replaceToken(
|
223 | `${resolvedFragmentPragmaBaseName}${jsxPragmaInfo.fragmentSuffix}, null`,
|
224 | );
|
225 |
|
226 | this.processChildren();
|
227 | while (!this.tokens.matches1(tt.jsxTagEnd)) {
|
228 | this.tokens.replaceToken("");
|
229 | }
|
230 | this.tokens.replaceToken(")");
|
231 | } else {
|
232 |
|
233 | this.processTagIntro();
|
234 | this.processProps(firstTokenStart);
|
235 |
|
236 | if (this.tokens.matches2(tt.slash, tt.jsxTagEnd)) {
|
237 |
|
238 | this.tokens.replaceToken("");
|
239 | this.tokens.replaceToken(")");
|
240 | } else if (this.tokens.matches1(tt.jsxTagEnd)) {
|
241 | this.tokens.replaceToken("");
|
242 |
|
243 | this.processChildren();
|
244 | while (!this.tokens.matches1(tt.jsxTagEnd)) {
|
245 | this.tokens.replaceToken("");
|
246 | }
|
247 | this.tokens.replaceToken(")");
|
248 | } else {
|
249 | throw new Error("Expected either /> or > at the end of the tag.");
|
250 | }
|
251 | }
|
252 | }
|
253 | }
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 | export function startsWithLowerCase(s) {
|
262 | const firstChar = s.charCodeAt(0);
|
263 | return firstChar >= charCodes.lowercaseA && firstChar <= charCodes.lowercaseZ;
|
264 | }
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 | function formatJSXTextLiteral(text) {
|
276 | let result = "";
|
277 | let whitespace = "";
|
278 |
|
279 | let isInInitialLineWhitespace = false;
|
280 | let seenNonWhitespace = false;
|
281 | for (let i = 0; i < text.length; i++) {
|
282 | const c = text[i];
|
283 | if (c === " " || c === "\t" || c === "\r") {
|
284 | if (!isInInitialLineWhitespace) {
|
285 | whitespace += c;
|
286 | }
|
287 | } else if (c === "\n") {
|
288 | whitespace = "";
|
289 | isInInitialLineWhitespace = true;
|
290 | } else {
|
291 | if (seenNonWhitespace && isInInitialLineWhitespace) {
|
292 | result += " ";
|
293 | }
|
294 | result += whitespace;
|
295 | whitespace = "";
|
296 | if (c === "&") {
|
297 | const {entity, newI} = processEntity(text, i + 1);
|
298 | i = newI - 1;
|
299 | result += entity;
|
300 | } else {
|
301 | result += c;
|
302 | }
|
303 | seenNonWhitespace = true;
|
304 | isInInitialLineWhitespace = false;
|
305 | }
|
306 | }
|
307 | if (!isInInitialLineWhitespace) {
|
308 | result += whitespace;
|
309 | }
|
310 | return JSON.stringify(result);
|
311 | }
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 | function formatJSXTextReplacement(text) {
|
319 | let numNewlines = 0;
|
320 | let numSpaces = 0;
|
321 | for (const c of text) {
|
322 | if (c === "\n") {
|
323 | numNewlines++;
|
324 | numSpaces = 0;
|
325 | } else if (c === " ") {
|
326 | numSpaces++;
|
327 | }
|
328 | }
|
329 | return "\n".repeat(numNewlines) + " ".repeat(numSpaces);
|
330 | }
|
331 |
|
332 |
|
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 | function formatJSXStringValueLiteral(text) {
|
339 | let result = "";
|
340 | for (let i = 0; i < text.length; i++) {
|
341 | const c = text[i];
|
342 | if (c === "\n") {
|
343 | if (/\s/.test(text[i + 1])) {
|
344 | result += " ";
|
345 | while (i < text.length && /\s/.test(text[i + 1])) {
|
346 | i++;
|
347 | }
|
348 | } else {
|
349 | result += "\n";
|
350 | }
|
351 | } else if (c === "&") {
|
352 | const {entity, newI} = processEntity(text, i + 1);
|
353 | result += entity;
|
354 | i = newI - 1;
|
355 | } else {
|
356 | result += c;
|
357 | }
|
358 | }
|
359 | return JSON.stringify(result);
|
360 | }
|
361 |
|
362 |
|
363 |
|
364 |
|
365 | function processEntity(text, indexAfterAmpersand) {
|
366 | let str = "";
|
367 | let count = 0;
|
368 | let entity;
|
369 | let i = indexAfterAmpersand;
|
370 |
|
371 | while (i < text.length && count++ < 10) {
|
372 | const ch = text[i];
|
373 | i++;
|
374 | if (ch === ";") {
|
375 | if (str[0] === "#") {
|
376 | if (str[1] === "x") {
|
377 | str = str.substr(2);
|
378 | if (HEX_NUMBER.test(str)) {
|
379 | entity = String.fromCodePoint(parseInt(str, 16));
|
380 | }
|
381 | } else {
|
382 | str = str.substr(1);
|
383 | if (DECIMAL_NUMBER.test(str)) {
|
384 | entity = String.fromCodePoint(parseInt(str, 10));
|
385 | }
|
386 | }
|
387 | } else {
|
388 | entity = XHTMLEntities[str];
|
389 | }
|
390 | break;
|
391 | }
|
392 | str += ch;
|
393 | }
|
394 | if (!entity) {
|
395 | return {entity: "&", newI: indexAfterAmpersand};
|
396 | }
|
397 | return {entity, newI: i};
|
398 | }
|
399 |
|
\ | No newline at end of file |