From b4e593de09eae7928bd1088af01f65609f2afa88 Mon Sep 17 00:00:00 2001 From: Armano Date: Sat, 12 Aug 2017 16:52:11 +0200 Subject: [PATCH 1/2] Add `no-this-in-template` rule. fixes #148 --- docs/rules/no-this-in-template.md | 25 ++++++++ lib/rules/no-this-in-template.js | 47 +++++++++++++++ tests/lib/rules/no-this-in-template.js | 79 ++++++++++++++++++++++++++ 3 files changed, 151 insertions(+) create mode 100644 docs/rules/no-this-in-template.md create mode 100644 lib/rules/no-this-in-template.js create mode 100644 tests/lib/rules/no-this-in-template.js diff --git a/docs/rules/no-this-in-template.md b/docs/rules/no-this-in-template.md new file mode 100644 index 000000000..f91f383d5 --- /dev/null +++ b/docs/rules/no-this-in-template.md @@ -0,0 +1,25 @@ +# Disallow usage of `this` in the template. (no-this-in-template) + +This rule reports expresions witch contains `this`. + +## :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..ced9f5577 --- /dev/null +++ b/lib/rules/no-this-in-template.js @@ -0,0 +1,47 @@ +/** + * @fileoverview Disallow usage of `this` in the template. + * @author Armano + */ +'use strict' + +// ------------------------------------------------------------------------------ +// Requirements +// ------------------------------------------------------------------------------ + +const utils = require('../utils') + +// ------------------------------------------------------------------------------ +// Rule Definition +// ------------------------------------------------------------------------------ + +module.exports = { + meta: { + docs: { + description: 'Disallow usage of `this` in the 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..5cac7460f --- /dev/null +++ b/tests/lib/rules/no-this-in-template.js @@ -0,0 +1,79 @@ +/** + * @fileoverview Disallow usage of `this` in the 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' + }] + } + ] +}) From 9e5ceeb446f1b43272614bc3bf01dcd2e8c46256 Mon Sep 17 00:00:00 2001 From: Armano Date: Tue, 15 Aug 2017 14:06:01 +0200 Subject: [PATCH 2/2] Update documentation --- docs/rules/no-this-in-template.md | 4 ++-- lib/rules/no-this-in-template.js | 4 ++-- tests/lib/rules/no-this-in-template.js | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/rules/no-this-in-template.md b/docs/rules/no-this-in-template.md index f91f383d5..0b877094f 100644 --- a/docs/rules/no-this-in-template.md +++ b/docs/rules/no-this-in-template.md @@ -1,6 +1,6 @@ -# Disallow usage of `this` in the template. (no-this-in-template) +# Disallow usage of `this` in template. (no-this-in-template) -This rule reports expresions witch contains `this`. +This rule reports expresions that contain `this` keyword in expressions ## :book: Rule Details diff --git a/lib/rules/no-this-in-template.js b/lib/rules/no-this-in-template.js index ced9f5577..bec97d76a 100644 --- a/lib/rules/no-this-in-template.js +++ b/lib/rules/no-this-in-template.js @@ -1,5 +1,5 @@ /** - * @fileoverview Disallow usage of `this` in the template. + * @fileoverview Disallow usage of `this` in template. * @author Armano */ 'use strict' @@ -17,7 +17,7 @@ const utils = require('../utils') module.exports = { meta: { docs: { - description: 'Disallow usage of `this` in the template.', + description: 'Disallow usage of `this` in template.', category: 'Best Practices', recommended: false }, diff --git a/tests/lib/rules/no-this-in-template.js b/tests/lib/rules/no-this-in-template.js index 5cac7460f..991ef74ce 100644 --- a/tests/lib/rules/no-this-in-template.js +++ b/tests/lib/rules/no-this-in-template.js @@ -1,5 +1,5 @@ /** - * @fileoverview Disallow usage of `this` in the template. + * @fileoverview Disallow usage of `this` in template. * @author Armano */ 'use strict'