-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathutil.js
More file actions
81 lines (76 loc) · 1.57 KB
/
util.js
File metadata and controls
81 lines (76 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
function isValidIdentifier(str) {
return !isKeyword(str) && Boolean(str.match(/^[A-Za-z_][A-Za-z_0-9]*$/));
}
function isKeyword(str) {
switch (str) {
case "do":
case "if":
case "in":
case "or":
case "and":
case "end":
case "for":
case "nil":
case "not":
case "else":
case "then":
case "true":
case "goto":
case "break":
case "false":
case "local":
case "until":
case "while":
case "elseif":
case "repeat":
case "return":
case "function":
return true;
default:
return false;
}
}
function isExpression(node) {
switch (node.type) {
case "Identifier":
case "CallExpression":
case "TableCallExpression":
case "StringCallExpression":
case "BooleanLiteral":
case "NilLiteral":
case "NumericLiteral":
case "StringLiteral":
case "VarargLiteral":
case "IndexExpression":
case "MemberExpression":
case "UnaryExpression":
case "TableConstructorExpression": {
return true;
}
case "FunctionDeclaration": {
return node.identifier == null;
}
case "BinaryExpression":
case "LogicalExpression": {
return node.inParens;
}
}
}
function lineShouldEndWithSemicolon(path) {
const node = path.getValue();
const parentNode = path.getParentNode();
if (!parentNode) {
return false;
}
return [
"ExpressionStatement",
"CallStatement",
"LocalStatement",
"AssignmentStatement",
].includes(node.type);
}
module.exports = {
isValidIdentifier,
isExpression,
lineShouldEndWithSemicolon,
};