check-lexer-functions.test.js
2.38 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
var fs = require('fs');
var acorn = require('acorn');
var walk = require('acorn/dist/walk');
var hadErrors = false;
var lexerFunctions = {
advance: true,
append: true,
attributesBlock: true,
attrs: true,
blank: true,
block: true,
blockCode: true,
call: true,
case: true,
className: true,
code: true,
colon: true,
comment: true,
conditional: true,
default: true,
doctype: true,
dot: true,
each: true,
eos: true,
endInterpolation: true,
extends: true,
filter: true,
id: true,
include: true,
indent: true,
interpolation: true,
isExpression: true,
mixin: true,
mixinBlock: true,
path: true,
pipelessText: true,
prepend: true,
slash: true,
tag: true,
text: true,
textHtml: true,
when: true,
while: true,
yield: true,
};
function checkDirectCalls (node) {
var callee = node.callee;
if (callee.type !== 'MemberExpression') return;
if (callee.object.type !== 'ThisExpression') return;
var property = callee.property;
var func;
if (callee.computed) {
if (property.type !== 'Literal') return;
func = property.value;
} else {
func = property.name;
}
if (!lexerFunctions[func]) return;
console.log('index.js:' + node.loc.start.line + ':' + node.loc.start.column + ': Lexer function ' + func + ' called directly');
hadErrors = true;
}
function checkMissingLexerFunction (node) {
var callee = node.callee;
if (callee.type !== 'MemberExpression') return;
if (callee.object.type !== 'ThisExpression') return;
var property = callee.property;
var func;
if (callee.computed) {
if (property.type !== 'Literal') return;
func = property.value;
} else {
func = property.name;
}
if (func !== 'callLexerFunction') return;
if (!node.arguments.length) return;
if (node.arguments[0].type !== 'Literal') return;
func = node.arguments[0].value;
if (lexerFunctions[func]) return;
console.log('index.js:' + node.loc.start.line + ':' + node.loc.start.column + ': Lexer function ' + func + ' not in lexerFunctions list');
hadErrors = true;
}
test('lexer functions', () => {
var str = fs.readFileSync(__dirname + '/../index.js', 'utf8');
var ast = acorn.parse(str, {locations: true});
walk.simple(ast, {
CallExpression: function (node) {
checkDirectCalls(node);
checkMissingLexerFunction(node);
}
});
if (hadErrors) {
throw new Error('Problem with lexer functions detected');
}
});