UNPKG

1.79 kBJavaScriptView Raw
1/**
2 * disallow the use of inline templates
3 *
4 * Instead of using inline HTML templates, it is better to load the HTML from an external file.
5 * Simple HTML templates are accepted by default.
6 * ('no-inline-template': [0, {allowSimple: true}])
7 *
8 * @version 0.12.0
9 * @category bestPractice
10 * @sinceAngularVersion 1.x
11 */
12'use strict';
13
14module.exports = {
15 meta: {
16 schema: [{
17 allowSimple: {
18 type: 'boolean'
19 }
20 }]
21 },
22 create: function(context) {
23 // Extracts any HTML tags.
24 var regularTagPattern = /<(.+?)>/g;
25 // Extracts self closing HTML tags.
26 var selfClosingTagPattern = /<(.+?)\/>/g;
27
28 var allowSimple = (context.options[0] && context.options[0].allowSimple) !== false;
29
30 function reportComplex(node) {
31 context.report(node, 'Inline template is too complex. Use an external template instead');
32 }
33
34 return {
35 Property: function(node) {
36 if (node.key.name !== 'template' || node.value.type !== 'Literal') {
37 return;
38 }
39 if (!allowSimple) {
40 context.report(node, 'Inline templates are not allowed. Use an external template instead');
41 }
42 if ((node.value.value && node.value.value.match(regularTagPattern) || []).length > 2) {
43 return reportComplex(node);
44 }
45 if ((node.value.value && node.value.value.match(selfClosingTagPattern) || []).length > 1) {
46 return reportComplex(node);
47 }
48 if (node.value && node.value.raw.indexOf('\\') !== -1) {
49 reportComplex(node);
50 }
51 }
52 };
53 }
54};