diff --git a/docs/rules/no-this-in-template.md b/docs/rules/no-this-in-template.md new file mode 100644 index 000000000..0b877094f --- /dev/null +++ b/docs/rules/no-this-in-template.md @@ -0,0 +1,25 @@ +# Disallow usage of `this` in template. (no-this-in-template) + +This rule reports expresions that contain `this` keyword in expressions + +## :book: Rule Details + +:-1: Examples of **incorrect** code for this rule: + +```html + +``` + +:+1: Examples of **correct** code for this rule: + +```html + +``` + +## :wrench: Options + +Nothing. diff --git a/lib/rules/no-this-in-template.js b/lib/rules/no-this-in-template.js new file mode 100644 index 000000000..bec97d76a --- /dev/null +++ b/lib/rules/no-this-in-template.js @@ -0,0 +1,47 @@ +/** + * @fileoverview Disallow usage of `this` in template. + * @author Armano + */ +'use strict' + +// ------------------------------------------------------------------------------ +// Requirements +// ------------------------------------------------------------------------------ + +const utils = require('../utils') + +// ------------------------------------------------------------------------------ +// Rule Definition +// ------------------------------------------------------------------------------ + +module.exports = { + meta: { + docs: { + description: 'Disallow usage of `this` in template.', + category: 'Best Practices', + recommended: false + }, + fixable: null, + schema: [] + }, + + /** + * Creates AST event handlers for no-this-in-template. + * + * @param {RuleContext} context - The rule context. + * @returns {Object} AST event handlers. + */ + create (context) { + utils.registerTemplateBodyVisitor(context, { + 'VExpressionContainer ThisExpression' (node) { + context.report({ + node, + loc: node.loc, + message: "Unexpected usage of 'this'." + }) + } + }) + + return {} + } +} diff --git a/tests/lib/rules/no-this-in-template.js b/tests/lib/rules/no-this-in-template.js new file mode 100644 index 000000000..991ef74ce --- /dev/null +++ b/tests/lib/rules/no-this-in-template.js @@ -0,0 +1,79 @@ +/** + * @fileoverview Disallow usage of `this` in template. + * @author Armano + */ +'use strict' + +// ------------------------------------------------------------------------------ +// Requirements +// ------------------------------------------------------------------------------ + +const rule = require('../../../lib/rules/no-this-in-template') + +const RuleTester = require('eslint').RuleTester + +// ------------------------------------------------------------------------------ +// Tests +// ------------------------------------------------------------------------------ + +const ruleTester = new RuleTester({ + parser: 'vue-eslint-parser', + parserOptions: { ecmaVersion: 2015 } +}) + +ruleTester.run('no-this-in-template', rule, { + valid: [ + '', + '', + '', + '', + '', + '', + '', + '' + ], + invalid: [ + { + code: '', + errors: [{ + message: "Unexpected usage of 'this'.", + type: 'ThisExpression' + }] + }, + { + code: '', + errors: [{ + message: "Unexpected usage of 'this'.", + type: 'ThisExpression' + }] + }, + { + code: '', + errors: [{ + message: "Unexpected usage of 'this'.", + type: 'ThisExpression' + }] + }, + { + code: '', + errors: [{ + message: "Unexpected usage of 'this'.", + type: 'ThisExpression' + }] + }, + { + code: '', + errors: [{ + message: "Unexpected usage of 'this'.", + type: 'ThisExpression' + }] + }, + { + code: '', + errors: [{ + message: "Unexpected usage of 'this'.", + type: 'ThisExpression' + }] + } + ] +})