Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80621 views
1
/*
2
Copyright (C) 2013 Yusuke Suzuki <[email protected]>
3
4
Redistribution and use in source and binary forms, with or without
5
modification, are permitted provided that the following conditions are met:
6
7
* Redistributions of source code must retain the above copyright
8
notice, this list of conditions and the following disclaimer.
9
* Redistributions in binary form must reproduce the above copyright
10
notice, this list of conditions and the following disclaimer in the
11
documentation and/or other materials provided with the distribution.
12
13
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16
ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
17
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
*/
24
25
(function () {
26
'use strict';
27
28
var code = require('./code');
29
30
function isStrictModeReservedWordES6(id) {
31
switch (id) {
32
case 'implements':
33
case 'interface':
34
case 'package':
35
case 'private':
36
case 'protected':
37
case 'public':
38
case 'static':
39
case 'let':
40
return true;
41
default:
42
return false;
43
}
44
}
45
46
function isKeywordES5(id, strict) {
47
// yield should not be treated as keyword under non-strict mode.
48
if (!strict && id === 'yield') {
49
return false;
50
}
51
return isKeywordES6(id, strict);
52
}
53
54
function isKeywordES6(id, strict) {
55
if (strict && isStrictModeReservedWordES6(id)) {
56
return true;
57
}
58
59
switch (id.length) {
60
case 2:
61
return (id === 'if') || (id === 'in') || (id === 'do');
62
case 3:
63
return (id === 'var') || (id === 'for') || (id === 'new') || (id === 'try');
64
case 4:
65
return (id === 'this') || (id === 'else') || (id === 'case') ||
66
(id === 'void') || (id === 'with') || (id === 'enum');
67
case 5:
68
return (id === 'while') || (id === 'break') || (id === 'catch') ||
69
(id === 'throw') || (id === 'const') || (id === 'yield') ||
70
(id === 'class') || (id === 'super');
71
case 6:
72
return (id === 'return') || (id === 'typeof') || (id === 'delete') ||
73
(id === 'switch') || (id === 'export') || (id === 'import');
74
case 7:
75
return (id === 'default') || (id === 'finally') || (id === 'extends');
76
case 8:
77
return (id === 'function') || (id === 'continue') || (id === 'debugger');
78
case 10:
79
return (id === 'instanceof');
80
default:
81
return false;
82
}
83
}
84
85
function isReservedWordES5(id, strict) {
86
return id === 'null' || id === 'true' || id === 'false' || isKeywordES5(id, strict);
87
}
88
89
function isReservedWordES6(id, strict) {
90
return id === 'null' || id === 'true' || id === 'false' || isKeywordES6(id, strict);
91
}
92
93
function isRestrictedWord(id) {
94
return id === 'eval' || id === 'arguments';
95
}
96
97
function isIdentifierName(id) {
98
var i, iz, ch;
99
100
if (id.length === 0) {
101
return false;
102
}
103
104
ch = id.charCodeAt(0);
105
if (!code.isIdentifierStart(ch) || ch === 92) { // \ (backslash)
106
return false;
107
}
108
109
for (i = 1, iz = id.length; i < iz; ++i) {
110
ch = id.charCodeAt(i);
111
if (!code.isIdentifierPart(ch) || ch === 92) { // \ (backslash)
112
return false;
113
}
114
}
115
return true;
116
}
117
118
function isIdentifierES5(id, strict) {
119
return isIdentifierName(id) && !isReservedWordES5(id, strict);
120
}
121
122
function isIdentifierES6(id, strict) {
123
return isIdentifierName(id) && !isReservedWordES6(id, strict);
124
}
125
126
module.exports = {
127
isKeywordES5: isKeywordES5,
128
isKeywordES6: isKeywordES6,
129
isReservedWordES5: isReservedWordES5,
130
isReservedWordES6: isReservedWordES6,
131
isRestrictedWord: isRestrictedWord,
132
isIdentifierName: isIdentifierName,
133
isIdentifierES5: isIdentifierES5,
134
isIdentifierES6: isIdentifierES6
135
};
136
}());
137
/* vim: set sw=4 ts=4 et tw=80 : */
138
139