all files / eslint-plugin-angular/rules/ no-inline-template.js

100% Statements 18/18
100% Branches 22/22
100% Functions 3/3
100% Lines 18/18
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                          17×   17×   17×       17×                              
/**
 * disallow the use of inline templates
 *
 * Instead of using inline HTML templates, it is better to load the HTML from an external file.
 * Simple HTML templates are accepted by default.
 * ('no-inline-template': [0, {allowSimple: true}])
 *
 * @version 0.12.0
 * @category bestPractice
 */
'use strict';
 
module.exports = function(context) {
    // Extracts any HTML tags.
    var regularTagPattern = /<(.+?)>/g;
    // Extracts self closing HTML tags.
    var selfClosingTagPattern = /<(.+?)\/>/g;
 
    var allowSimple = (context.options[0] && context.options[0].allowSimple) !== false;
 
    function reportComplex(node) {
        context.report(node, 'Inline template is too complex. Use an external template instead');
    }
 
    return {
        Property: function(node) {
            if (node.key.name !== 'template' || node.value.type !== 'Literal') {
                return;
            }
            if (!allowSimple) {
                context.report(node, 'Inline templates are not allowed. Use an external template instead');
            }
            if ((node.value.value && node.value.value.match(regularTagPattern) || []).length > 2) {
                return reportComplex(node);
            }
            if ((node.value.value && node.value.value.match(selfClosingTagPattern) || []).length > 1) {
                return reportComplex(node);
            }
            if (node.value && node.value.raw.indexOf('\\') !== -1) {
                reportComplex(node);
            }
        }
    };
};
 
module.exports.schema = [{
    allowSimple: {
        type: 'boolean'
    }
}];