-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
150 lines (126 loc) · 3.77 KB
/
index.js
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*!
* handlebars-lint <https://github.com/jonschlinkert/handlebars-lint>
*
* Copyright (c) 2016-2018, Jon Schlinkert.
* Released under the MIT License.
*/
'use strict';
const union = require('arr-union');
/**
* Pass a string with handlebars templates and an optional context to lint for
* missing variables, helpers, block helpers or partials.
*
* ```js
* const lint = require('handlebars-lint');
* lint(string[, options]);
* ```
* @param {String} `str` The string to lint.
* @param {Object} `options` Pass a context on `options.context` or your own instance of handlebars on `options.hbs`.
* @return {Object}
* @api public
*/
function lint(str, options) {
const opts = Object.assign({ hbs: require('handlebars'), context: {} }, options);
const ast = opts.hbs.parse(str);
const obj = filter(ast.body);
const report = createReport(obj, opts.context);
if (report.variables) report.variables.sort();
if (report.blockHelpers) report.blockHelpers.sort();
if (report.partials) report.partials.sort();
if (report.helpers) report.helpers.sort();
return report;
}
function filter(body, tokens = {}) {
for (const node of body) {
if (node.type === 'ContentStatement') {
continue;
}
if (node.type === 'PartialBlockStatement') {
set(tokens, 'partialBlocks', node.name.parts[0]);
}
if (node.type === 'PartialStatement') {
if (node.name.parts) {
set(tokens, 'partials', node.name.parts[0]);
} else if (node.name && node.name.type === 'SubExpression') {
set(tokens, 'helpers', node.name.path.parts[0]);
}
}
if (node.type === 'DecoratorBlock') {
set(tokens, 'decoratorBlocks', node.path.parts[0]);
}
if (node.type === 'BlockStatement') {
set(tokens, 'blockHelpers', node.path.parts[0]);
}
if (node.type === 'SubExpression' || node.type === 'MustacheStatement') {
set(tokens, node.params.length || node.hash ? 'helpers' : 'variables', node.path.parts[0]);
}
if (node.type === 'PathExpression') {
set(tokens, 'variables', node.parts[0]);
}
if (node.type === 'HashPair') {
filter([node.value], tokens);
}
if (node.hasOwnProperty('program')) {
filter(node.program.body, tokens);
}
if (node.name && node.name.hash) {
filter(node.name.hash.pairs, tokens);
}
if (node.hash) {
filter(node.hash.pairs, tokens);
}
if (node.params) {
filter(node.params, tokens);
}
}
return tokens;
}
function visit(node, fn) {
fn(node);
return node.nodes ? mapVisit(node, fn) : node;
}
function mapVisit(node, fn) {
node.nodes.forEach(child => fn(child));
return node;
}
function set(tokens, key, val) {
tokens[key] = union(tokens[key] || [], val);
}
function createReport(tokens, context) {
const report = {};
for (const key of Object.keys(tokens)) {
var actual = toArray(key, context);
var expected = tokens[key];
for (const name of expected) {
if (!actual.includes(name)) {
set(report, key, name);
}
}
}
return report;
}
function toArray(key, context) {
if (isObject(context[key])) {
context[key] = Object.keys(context[key]);
}
if (isObject(context.variables)) {
context.variables = Object.keys(context.variables);
}
if (isObject(context.helpers)) {
context.helpers = Object.keys(context.helpers);
}
if (isObject(context.asyncHelpers)) {
context.asyncHelpers = Object.keys(context.asyncHelpers);
}
if (key === 'variables' || key === 'helpers' || key === 'blockHelpers') {
return union([], context.variables, context.helpers, context.asyncHelpers);
}
if (Array.isArray(context[key])) {
return context[key];
}
return [];
}
function isObject(val) {
return val && typeof val === 'object' && !Array.isArray(val);
}
module.exports = lint;