source: frontend/node_modules/sucrase/dist/util/isIdentifier.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 1.9 KB
Line 
1"use strict";Object.defineProperty(exports, "__esModule", {value: true});var _identifier = require('../parser/util/identifier');
2
3// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar
4// Hard-code a list of reserved words rather than trying to use keywords or contextual keywords
5// from the parser, since currently there are various exceptions, like `package` being reserved
6// but unused and various contextual keywords being reserved. Note that we assume that all code
7// compiled by Sucrase is in a module, so strict mode words and await are all considered reserved
8// here.
9const RESERVED_WORDS = new Set([
10 // Reserved keywords as of ECMAScript 2015
11 "break",
12 "case",
13 "catch",
14 "class",
15 "const",
16 "continue",
17 "debugger",
18 "default",
19 "delete",
20 "do",
21 "else",
22 "export",
23 "extends",
24 "finally",
25 "for",
26 "function",
27 "if",
28 "import",
29 "in",
30 "instanceof",
31 "new",
32 "return",
33 "super",
34 "switch",
35 "this",
36 "throw",
37 "try",
38 "typeof",
39 "var",
40 "void",
41 "while",
42 "with",
43 "yield",
44 // Future reserved keywords
45 "enum",
46 "implements",
47 "interface",
48 "let",
49 "package",
50 "private",
51 "protected",
52 "public",
53 "static",
54 "await",
55 // Literals that cannot be used as identifiers
56 "false",
57 "null",
58 "true",
59]);
60
61/**
62 * Determine if the given name is a legal variable name.
63 *
64 * This is needed when transforming TypeScript enums; if an enum key is a valid
65 * variable name, it might be referenced later in the enum, so we need to
66 * declare a variable.
67 */
68 function isIdentifier(name) {
69 if (name.length === 0) {
70 return false;
71 }
72 if (!_identifier.IS_IDENTIFIER_START[name.charCodeAt(0)]) {
73 return false;
74 }
75 for (let i = 1; i < name.length; i++) {
76 if (!_identifier.IS_IDENTIFIER_CHAR[name.charCodeAt(i)]) {
77 return false;
78 }
79 }
80 return !RESERVED_WORDS.has(name);
81} exports.default = isIdentifier;
Note: See TracBrowser for help on using the repository browser.