1 | "use strict";
|
2 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3 | if (k2 === undefined) k2 = k;
|
4 | Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
5 | }) : (function(o, m, k, k2) {
|
6 | if (k2 === undefined) k2 = k;
|
7 | o[k2] = m[k];
|
8 | }));
|
9 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
10 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
11 | }) : function(o, v) {
|
12 | o["default"] = v;
|
13 | });
|
14 | var __importStar = (this && this.__importStar) || function (mod) {
|
15 | if (mod && mod.__esModule) return mod;
|
16 | var result = {};
|
17 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
18 | __setModuleDefault(result, mod);
|
19 | return result;
|
20 | };
|
21 | Object.defineProperty(exports, "__esModule", { value: true });
|
22 | exports.firstDefined = exports.nodeHasTokens = exports.createError = exports.TSError = exports.convertTokens = exports.convertToken = exports.getTokenType = exports.isChildUnwrappableOptionalChain = exports.isChainExpression = exports.isOptional = exports.isComputedProperty = exports.unescapeStringLiteralText = exports.hasJSXAncestor = exports.findFirstMatchingAncestor = exports.findNextToken = exports.getTSNodeAccessibility = exports.getDeclarationKind = exports.isJSXToken = exports.isToken = exports.getRange = exports.canContainDirective = exports.getLocFor = exports.getLineAndCharacterFor = exports.getBinaryExpressionType = exports.isJSDocComment = exports.isComment = exports.isComma = exports.getLastModifier = exports.hasModifier = exports.isESTreeClassMember = exports.getTextForTokenKind = exports.isLogicalOperator = exports.isAssignmentOperator = void 0;
|
23 | const ts = __importStar(require("typescript"));
|
24 | const ts_estree_1 = require("./ts-estree");
|
25 | const xhtml_entities_1 = require("./jsx/xhtml-entities");
|
26 | const SyntaxKind = ts.SyntaxKind;
|
27 | const LOGICAL_OPERATORS = [
|
28 | SyntaxKind.BarBarToken,
|
29 | SyntaxKind.AmpersandAmpersandToken,
|
30 | SyntaxKind.QuestionQuestionToken,
|
31 | ];
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | function isAssignmentOperator(operator) {
|
38 | return (operator.kind >= SyntaxKind.FirstAssignment &&
|
39 | operator.kind <= SyntaxKind.LastAssignment);
|
40 | }
|
41 | exports.isAssignmentOperator = isAssignmentOperator;
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | function isLogicalOperator(operator) {
|
48 | return LOGICAL_OPERATORS.includes(operator.kind);
|
49 | }
|
50 | exports.isLogicalOperator = isLogicalOperator;
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | function getTextForTokenKind(kind) {
|
57 | return ts.tokenToString(kind);
|
58 | }
|
59 | exports.getTextForTokenKind = getTextForTokenKind;
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | function isESTreeClassMember(node) {
|
66 | return node.kind !== SyntaxKind.SemicolonClassElement;
|
67 | }
|
68 | exports.isESTreeClassMember = isESTreeClassMember;
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | function hasModifier(modifierKind, node) {
|
76 | return (!!node.modifiers &&
|
77 | !!node.modifiers.length &&
|
78 | node.modifiers.some(modifier => modifier.kind === modifierKind));
|
79 | }
|
80 | exports.hasModifier = hasModifier;
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | function getLastModifier(node) {
|
87 | return ((!!node.modifiers &&
|
88 | !!node.modifiers.length &&
|
89 | node.modifiers[node.modifiers.length - 1]) ||
|
90 | null);
|
91 | }
|
92 | exports.getLastModifier = getLastModifier;
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | function isComma(token) {
|
99 | return token.kind === SyntaxKind.CommaToken;
|
100 | }
|
101 | exports.isComma = isComma;
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 | function isComment(node) {
|
108 | return (node.kind === SyntaxKind.SingleLineCommentTrivia ||
|
109 | node.kind === SyntaxKind.MultiLineCommentTrivia);
|
110 | }
|
111 | exports.isComment = isComment;
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 | function isJSDocComment(node) {
|
118 | return node.kind === SyntaxKind.JSDocComment;
|
119 | }
|
120 | exports.isJSDocComment = isJSDocComment;
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 | function getBinaryExpressionType(operator) {
|
127 | if (isAssignmentOperator(operator)) {
|
128 | return ts_estree_1.AST_NODE_TYPES.AssignmentExpression;
|
129 | }
|
130 | else if (isLogicalOperator(operator)) {
|
131 | return ts_estree_1.AST_NODE_TYPES.LogicalExpression;
|
132 | }
|
133 | return ts_estree_1.AST_NODE_TYPES.BinaryExpression;
|
134 | }
|
135 | exports.getBinaryExpressionType = getBinaryExpressionType;
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 | function getLineAndCharacterFor(pos, ast) {
|
143 | const loc = ast.getLineAndCharacterOfPosition(pos);
|
144 | return {
|
145 | line: loc.line + 1,
|
146 | column: loc.character,
|
147 | };
|
148 | }
|
149 | exports.getLineAndCharacterFor = getLineAndCharacterFor;
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 | function getLocFor(start, end, ast) {
|
159 | return {
|
160 | start: getLineAndCharacterFor(start, ast),
|
161 | end: getLineAndCharacterFor(end, ast),
|
162 | };
|
163 | }
|
164 | exports.getLocFor = getLocFor;
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 | function canContainDirective(node) {
|
171 | if (node.kind === ts.SyntaxKind.Block) {
|
172 | switch (node.parent.kind) {
|
173 | case ts.SyntaxKind.Constructor:
|
174 | case ts.SyntaxKind.GetAccessor:
|
175 | case ts.SyntaxKind.SetAccessor:
|
176 | case ts.SyntaxKind.ArrowFunction:
|
177 | case ts.SyntaxKind.FunctionExpression:
|
178 | case ts.SyntaxKind.FunctionDeclaration:
|
179 | case ts.SyntaxKind.MethodDeclaration:
|
180 | return true;
|
181 | default:
|
182 | return false;
|
183 | }
|
184 | }
|
185 | return true;
|
186 | }
|
187 | exports.canContainDirective = canContainDirective;
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 | function getRange(node, ast) {
|
195 | return [node.getStart(ast), node.getEnd()];
|
196 | }
|
197 | exports.getRange = getRange;
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 | function isToken(node) {
|
204 | return (node.kind >= SyntaxKind.FirstToken && node.kind <= SyntaxKind.LastToken);
|
205 | }
|
206 | exports.isToken = isToken;
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 | function isJSXToken(node) {
|
213 | return (node.kind >= SyntaxKind.JsxElement && node.kind <= SyntaxKind.JsxAttribute);
|
214 | }
|
215 | exports.isJSXToken = isJSXToken;
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 | function getDeclarationKind(node) {
|
222 | if (node.flags & ts.NodeFlags.Let) {
|
223 | return 'let';
|
224 | }
|
225 | if (node.flags & ts.NodeFlags.Const) {
|
226 | return 'const';
|
227 | }
|
228 | return 'var';
|
229 | }
|
230 | exports.getDeclarationKind = getDeclarationKind;
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 | function getTSNodeAccessibility(node) {
|
237 | const modifiers = node.modifiers;
|
238 | if (!modifiers) {
|
239 | return null;
|
240 | }
|
241 | for (let i = 0; i < modifiers.length; i++) {
|
242 | const modifier = modifiers[i];
|
243 | switch (modifier.kind) {
|
244 | case SyntaxKind.PublicKeyword:
|
245 | return 'public';
|
246 | case SyntaxKind.ProtectedKeyword:
|
247 | return 'protected';
|
248 | case SyntaxKind.PrivateKeyword:
|
249 | return 'private';
|
250 | default:
|
251 | break;
|
252 | }
|
253 | }
|
254 | return null;
|
255 | }
|
256 | exports.getTSNodeAccessibility = getTSNodeAccessibility;
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 | function findNextToken(previousToken, parent, ast) {
|
266 | return find(parent);
|
267 | function find(n) {
|
268 | if (ts.isToken(n) && n.pos === previousToken.end) {
|
269 |
|
270 | return n;
|
271 | }
|
272 | return firstDefined(n.getChildren(ast), (child) => {
|
273 | const shouldDiveInChildNode =
|
274 |
|
275 | (child.pos <= previousToken.pos && child.end > previousToken.end) ||
|
276 |
|
277 | child.pos === previousToken.end;
|
278 | return shouldDiveInChildNode && nodeHasTokens(child, ast)
|
279 | ? find(child)
|
280 | : undefined;
|
281 | });
|
282 | }
|
283 | }
|
284 | exports.findNextToken = findNextToken;
|
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 | function findFirstMatchingAncestor(node, predicate) {
|
292 | while (node) {
|
293 | if (predicate(node)) {
|
294 | return node;
|
295 | }
|
296 | node = node.parent;
|
297 | }
|
298 | return undefined;
|
299 | }
|
300 | exports.findFirstMatchingAncestor = findFirstMatchingAncestor;
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 | function hasJSXAncestor(node) {
|
307 | return !!findFirstMatchingAncestor(node, isJSXToken);
|
308 | }
|
309 | exports.hasJSXAncestor = hasJSXAncestor;
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 | function unescapeStringLiteralText(text) {
|
316 | return text.replace(/&(?:#\d+|#x[\da-fA-F]+|[0-9a-zA-Z]+);/g, entity => {
|
317 | const item = entity.slice(1, -1);
|
318 | if (item[0] === '#') {
|
319 | const codePoint = item[1] === 'x'
|
320 | ? parseInt(item.slice(2), 16)
|
321 | : parseInt(item.slice(1), 10);
|
322 | return codePoint > 0x10ffff
|
323 | ? entity
|
324 | : String.fromCodePoint(codePoint);
|
325 | }
|
326 | return xhtml_entities_1.xhtmlEntities[item] || entity;
|
327 | });
|
328 | }
|
329 | exports.unescapeStringLiteralText = unescapeStringLiteralText;
|
330 |
|
331 |
|
332 |
|
333 |
|
334 |
|
335 | function isComputedProperty(node) {
|
336 | return node.kind === SyntaxKind.ComputedPropertyName;
|
337 | }
|
338 | exports.isComputedProperty = isComputedProperty;
|
339 |
|
340 |
|
341 |
|
342 |
|
343 |
|
344 | function isOptional(node) {
|
345 | return node.questionToken
|
346 | ? node.questionToken.kind === SyntaxKind.QuestionToken
|
347 | : false;
|
348 | }
|
349 | exports.isOptional = isOptional;
|
350 |
|
351 |
|
352 |
|
353 | function isChainExpression(node) {
|
354 | return node.type === ts_estree_1.AST_NODE_TYPES.ChainExpression;
|
355 | }
|
356 | exports.isChainExpression = isChainExpression;
|
357 |
|
358 |
|
359 |
|
360 | function isChildUnwrappableOptionalChain(node, child) {
|
361 | return (isChainExpression(child) &&
|
362 |
|
363 | node.expression.kind !== ts.SyntaxKind.ParenthesizedExpression);
|
364 | }
|
365 | exports.isChildUnwrappableOptionalChain = isChildUnwrappableOptionalChain;
|
366 |
|
367 |
|
368 |
|
369 |
|
370 |
|
371 | function getTokenType(token) {
|
372 | if ('originalKeywordKind' in token && token.originalKeywordKind) {
|
373 | if (token.originalKeywordKind === SyntaxKind.NullKeyword) {
|
374 | return ts_estree_1.AST_TOKEN_TYPES.Null;
|
375 | }
|
376 | else if (token.originalKeywordKind >= SyntaxKind.FirstFutureReservedWord &&
|
377 | token.originalKeywordKind <= SyntaxKind.LastKeyword) {
|
378 | return ts_estree_1.AST_TOKEN_TYPES.Identifier;
|
379 | }
|
380 | return ts_estree_1.AST_TOKEN_TYPES.Keyword;
|
381 | }
|
382 | if (token.kind >= SyntaxKind.FirstKeyword &&
|
383 | token.kind <= SyntaxKind.LastFutureReservedWord) {
|
384 | if (token.kind === SyntaxKind.FalseKeyword ||
|
385 | token.kind === SyntaxKind.TrueKeyword) {
|
386 | return ts_estree_1.AST_TOKEN_TYPES.Boolean;
|
387 | }
|
388 | return ts_estree_1.AST_TOKEN_TYPES.Keyword;
|
389 | }
|
390 | if (token.kind >= SyntaxKind.FirstPunctuation &&
|
391 | token.kind <= SyntaxKind.LastPunctuation) {
|
392 | return ts_estree_1.AST_TOKEN_TYPES.Punctuator;
|
393 | }
|
394 | if (token.kind >= SyntaxKind.NoSubstitutionTemplateLiteral &&
|
395 | token.kind <= SyntaxKind.TemplateTail) {
|
396 | return ts_estree_1.AST_TOKEN_TYPES.Template;
|
397 | }
|
398 | switch (token.kind) {
|
399 | case SyntaxKind.NumericLiteral:
|
400 | return ts_estree_1.AST_TOKEN_TYPES.Numeric;
|
401 | case SyntaxKind.JsxText:
|
402 | return ts_estree_1.AST_TOKEN_TYPES.JSXText;
|
403 | case SyntaxKind.StringLiteral:
|
404 |
|
405 |
|
406 | if (token.parent &&
|
407 | (token.parent.kind === SyntaxKind.JsxAttribute ||
|
408 | token.parent.kind === SyntaxKind.JsxElement)) {
|
409 | return ts_estree_1.AST_TOKEN_TYPES.JSXText;
|
410 | }
|
411 | return ts_estree_1.AST_TOKEN_TYPES.String;
|
412 | case SyntaxKind.RegularExpressionLiteral:
|
413 | return ts_estree_1.AST_TOKEN_TYPES.RegularExpression;
|
414 | case SyntaxKind.Identifier:
|
415 | case SyntaxKind.ConstructorKeyword:
|
416 | case SyntaxKind.GetKeyword:
|
417 | case SyntaxKind.SetKeyword:
|
418 |
|
419 | default:
|
420 | }
|
421 |
|
422 | if (token.parent && token.kind === SyntaxKind.Identifier) {
|
423 | if (isJSXToken(token.parent)) {
|
424 | return ts_estree_1.AST_TOKEN_TYPES.JSXIdentifier;
|
425 | }
|
426 | if (token.parent.kind === SyntaxKind.PropertyAccessExpression &&
|
427 | hasJSXAncestor(token)) {
|
428 | return ts_estree_1.AST_TOKEN_TYPES.JSXIdentifier;
|
429 | }
|
430 | }
|
431 | return ts_estree_1.AST_TOKEN_TYPES.Identifier;
|
432 | }
|
433 | exports.getTokenType = getTokenType;
|
434 |
|
435 |
|
436 |
|
437 |
|
438 |
|
439 |
|
440 | function convertToken(token, ast) {
|
441 | const start = token.kind === SyntaxKind.JsxText
|
442 | ? token.getFullStart()
|
443 | : token.getStart(ast);
|
444 | const end = token.getEnd();
|
445 | const value = ast.text.slice(start, end);
|
446 | const tokenType = getTokenType(token);
|
447 | if (tokenType === ts_estree_1.AST_TOKEN_TYPES.RegularExpression) {
|
448 | return {
|
449 | type: tokenType,
|
450 | value,
|
451 | range: [start, end],
|
452 | loc: getLocFor(start, end, ast),
|
453 | regex: {
|
454 | pattern: value.slice(1, value.lastIndexOf('/')),
|
455 | flags: value.slice(value.lastIndexOf('/') + 1),
|
456 | },
|
457 | };
|
458 | }
|
459 | else {
|
460 |
|
461 |
|
462 | return {
|
463 | type: tokenType,
|
464 | value,
|
465 | range: [start, end],
|
466 | loc: getLocFor(start, end, ast),
|
467 | };
|
468 | }
|
469 | }
|
470 | exports.convertToken = convertToken;
|
471 |
|
472 |
|
473 |
|
474 |
|
475 |
|
476 | function convertTokens(ast) {
|
477 | const result = [];
|
478 | |
479 |
|
480 |
|
481 | function walk(node) {
|
482 |
|
483 |
|
484 | if (isComment(node) || isJSDocComment(node)) {
|
485 | return;
|
486 | }
|
487 | if (isToken(node) && node.kind !== SyntaxKind.EndOfFileToken) {
|
488 | const converted = convertToken(node, ast);
|
489 | if (converted) {
|
490 | result.push(converted);
|
491 | }
|
492 | }
|
493 | else {
|
494 | node.getChildren(ast).forEach(walk);
|
495 | }
|
496 | }
|
497 | walk(ast);
|
498 | return result;
|
499 | }
|
500 | exports.convertTokens = convertTokens;
|
501 | class TSError extends Error {
|
502 | constructor(message, fileName, index, lineNumber, column) {
|
503 | super(message);
|
504 | this.fileName = fileName;
|
505 | this.index = index;
|
506 | this.lineNumber = lineNumber;
|
507 | this.column = column;
|
508 | Object.defineProperty(this, 'name', {
|
509 | value: new.target.name,
|
510 | enumerable: false,
|
511 | configurable: true,
|
512 | });
|
513 | }
|
514 | }
|
515 | exports.TSError = TSError;
|
516 |
|
517 |
|
518 |
|
519 |
|
520 |
|
521 |
|
522 | function createError(ast, start, message) {
|
523 | const loc = ast.getLineAndCharacterOfPosition(start);
|
524 | return new TSError(message, ast.fileName, start, loc.line + 1, loc.character);
|
525 | }
|
526 | exports.createError = createError;
|
527 |
|
528 |
|
529 |
|
530 |
|
531 | function nodeHasTokens(n, ast) {
|
532 |
|
533 |
|
534 | return n.kind === SyntaxKind.EndOfFileToken
|
535 | ? !!n.jsDoc
|
536 | : n.getWidth(ast) !== 0;
|
537 | }
|
538 | exports.nodeHasTokens = nodeHasTokens;
|
539 |
|
540 |
|
541 |
|
542 |
|
543 |
|
544 |
|
545 |
|
546 | function firstDefined(array, callback) {
|
547 | if (array === undefined) {
|
548 | return undefined;
|
549 | }
|
550 | for (let i = 0; i < array.length; i++) {
|
551 | const result = callback(array[i], i);
|
552 | if (result !== undefined) {
|
553 | return result;
|
554 | }
|
555 | }
|
556 | return undefined;
|
557 | }
|
558 | exports.firstDefined = firstDefined;
|
559 |
|
\ | No newline at end of file |