This repository has been archived by the owner on Aug 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
broccoli-template-linter.js
183 lines (144 loc) · 5.26 KB
/
broccoli-template-linter.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
'use strict';
/* eslint-env node */
const Filter = require('broccoli-persistent-filter');
const md5Hex = require('md5-hex');
const stringify = require('json-stable-stringify');
const chalk = require('chalk');
const Linter = require('ember-template-lint');
const debug = require('debug')('template-lint:broccoli');
const projectLocalizationAddon = require('./lib/utils/project-localization-framework');
const testGenerators = require('aot-test-generators');
const testGeneratorNames = Object.keys(testGenerators);
const concat = require('broccoli-concat');
const stripAnsi = require('strip-ansi');
function TemplateLinter(inputNode, _options) {
if (!(this instanceof TemplateLinter)) { return new TemplateLinter(inputNode, _options); }
let options = _options || {};
if (!Object.prototype.hasOwnProperty.call(options, 'persist')) {
options.persist = true;
}
Filter.call(this, inputNode, {
annotation: options.annotation,
persist: options.persist
});
this.options = options;
this._console = this.options.console || console;
this._templatercConfig = undefined;
if (this.options.testGenerator) {
let testGenerator = testGenerators[this.options.testGenerator];
if (!testGenerator) {
throw new Error(`No test generator found for "testGenerator: ${this.options.testGenerator}"`);
}
this._testGenerator = testGenerator;
}
this.linter = new Linter(options);
debug('Linter config: %s', JSON.stringify(this.linter.config));
this.issueLocalizationWarningIfNeeded();
}
TemplateLinter.prototype = Object.create(Filter.prototype);
TemplateLinter.prototype.constructor = TemplateLinter;
TemplateLinter.prototype.extensions = ['hbs', 'handlebars'];
TemplateLinter.prototype.targetExtension = 'template.lint-test.js';
TemplateLinter.prototype.baseDir = function() {
return __dirname;
};
TemplateLinter.prototype.cacheKeyProcessString = function(string, relativePath) {
return md5Hex([
stringify(this.linter.config),
this.options.testGenerator || '',
this.options.groupName || '',
string,
relativePath
]);
};
TemplateLinter.prototype.build = function () {
let self = this;
self._errors = [];
return Filter.prototype.build.apply(this, arguments)
.finally(function() {
if (self._errors.length > 0) {
let label = ' Template Linting Error' + (self._errors.length > 1 ? 's' : '');
self._console.log('\n' + self._errors.join('\n'));
self._console.log(chalk.yellow('===== ' + self._errors.length + label + '\n'));
}
});
};
TemplateLinter.prototype.processString = function(contents, relativePath) {
let errors = this.linter.verify({
source: contents,
moduleId: relativePath.slice(0, -4)
});
errors = errors.filter(function(error) {
return error.severity > 1;
});
let passed = errors.length === 0;
let consoleOutput = Linter.errorsToMessages(relativePath, errors);
let testOutput = stripAnsi(consoleOutput);
let output = '';
if (this._testGenerator) {
if (this.options.groupName) {
output = this._testGenerator.test(relativePath, passed,
`${relativePath} should pass TemplateLint.\n\n${testOutput}`);
} else {
output = [
this._testGenerator.suiteHeader(`TemplateLint | ${relativePath}`),
this._testGenerator.test('should pass TemplateLint', passed,
`${relativePath} should pass TemplateLint.\n\n${testOutput}`),
this._testGenerator.suiteFooter()
].join('');
}
}
debug('Found %s errors for %s with \ncontents: \n%s\nerrors: \n%s', errors.length, relativePath, contents, consoleOutput);
return {
errors,
consoleOutput,
output
};
};
TemplateLinter.prototype.postProcess = function(results) {
if (results.consoleOutput) {
this._errors.push(results.consoleOutput);
}
return results;
};
TemplateLinter.prototype.issueLocalizationWarningIfNeeded = function() {
if ('no-bare-strings' in this.linter.config.rules) {
return;
}
let project = this.options.project;
if (!project) {
return;
}
let addon = projectLocalizationAddon(project);
if (addon) {
this._console.log(chalk.yellow(
'The `no-bare-strings` rule must be configured when using a localization framework (`' + addon.name + '`). To prevent this warning, add the following to your `.template-lintrc.js`:\n\n rules: {\n \'no-bare-strings\': true\n }'
));
}
};
TemplateLinter.create = function(inputNode, options) {
options = options || {};
if (!options.groupName) {
return new TemplateLinter(inputNode, options);
}
if (testGeneratorNames.indexOf(options.testGenerator) === -1) {
throw new Error(`The "groupName" options can only be used with a "testGenerator" option of: ${testGeneratorNames}`);
}
let testGenerator = testGenerators[options.testGenerator];
let headerName = 'TemplateLint';
if (options.groupName !== 'templates') {
headerName += ` | ${options.groupName}`;
}
let header = testGenerator.suiteHeader(headerName);
let footer = testGenerator.suiteFooter();
let lint = new TemplateLinter(inputNode, options);
return concat(lint, {
outputFile: `/${options.groupName}.template.lint-test.js`,
header,
inputFiles: ['**/*.template.lint-test.js'],
footer,
sourceMapConfig: { enabled: false },
allowNone: true
});
};
module.exports = TemplateLinter;