|
| 1 | +/** |
| 2 | + * @author Yosuke Ota |
| 3 | + */ |
| 4 | +'use strict' |
| 5 | + |
| 6 | +const RuleTester = require('eslint').RuleTester |
| 7 | +const rule = require('../../../lib/rules/no-restricted-syntax') |
| 8 | + |
| 9 | +const tester = new RuleTester({ |
| 10 | + parser: 'vue-eslint-parser', |
| 11 | + parserOptions: { ecmaVersion: 2015 } |
| 12 | +}) |
| 13 | + |
| 14 | +tester.run('no-restricted-syntax', rule, { |
| 15 | + valid: [ |
| 16 | + { |
| 17 | + code: ` |
| 18 | + <template> |
| 19 | + <input :value="value"> |
| 20 | + </template>`, |
| 21 | + options: [ |
| 22 | + { |
| 23 | + 'selector': 'CallExpression', |
| 24 | + 'message': 'Call expressions are not allowed.' |
| 25 | + } |
| 26 | + ] |
| 27 | + } |
| 28 | + ], |
| 29 | + invalid: [ |
| 30 | + { |
| 31 | + code: ` |
| 32 | + <template> |
| 33 | + <input :value="value()"> |
| 34 | + </template>`, |
| 35 | + options: [ |
| 36 | + { |
| 37 | + 'selector': 'CallExpression', |
| 38 | + 'message': 'Call expressions are not allowed.' |
| 39 | + } |
| 40 | + ], |
| 41 | + errors: [ |
| 42 | + { |
| 43 | + message: 'Call expressions are not allowed.', |
| 44 | + line: 3, |
| 45 | + column: 26, |
| 46 | + endLine: 3, |
| 47 | + endColumn: 33 |
| 48 | + } |
| 49 | + ] |
| 50 | + }, |
| 51 | + |
| 52 | + // Forbids call expressions inside mustache interpolation. |
| 53 | + { |
| 54 | + code: ` |
| 55 | + <template> |
| 56 | + <div> {{ foo() }} </div> |
| 57 | + <div> {{ foo.bar() }} </div> |
| 58 | + <div> {{ foo().bar }} </div> |
| 59 | + </template>`, |
| 60 | + options: [ |
| 61 | + { |
| 62 | + 'selector': 'VElement > VExpressionContainer CallExpression', |
| 63 | + 'message': 'Call expressions are not allowed inside mustache interpolation.' |
| 64 | + } |
| 65 | + ], |
| 66 | + errors: [ |
| 67 | + { |
| 68 | + message: 'Call expressions are not allowed inside mustache interpolation.', |
| 69 | + line: 3, |
| 70 | + column: 20, |
| 71 | + endLine: 3, |
| 72 | + endColumn: 25 |
| 73 | + }, |
| 74 | + { |
| 75 | + message: 'Call expressions are not allowed inside mustache interpolation.', |
| 76 | + line: 4, |
| 77 | + column: 20, |
| 78 | + endLine: 4, |
| 79 | + endColumn: 29 |
| 80 | + }, |
| 81 | + { |
| 82 | + message: 'Call expressions are not allowed inside mustache interpolation.', |
| 83 | + line: 5, |
| 84 | + column: 20, |
| 85 | + endLine: 5, |
| 86 | + endColumn: 25 |
| 87 | + } |
| 88 | + ] |
| 89 | + }, |
| 90 | + |
| 91 | + // Sample source code on issue 689 |
| 92 | + { |
| 93 | + code: ` |
| 94 | + <template> |
| 95 | + <div :foo="$gettext(\`bar\`)">{{$gettext(\`bar\`)}}</div> |
| 96 | + </template>`, |
| 97 | + options: [ |
| 98 | + "CallExpression[callee.type='Identifier'][callee.name='$gettext'] TemplateLiteral" |
| 99 | + ], |
| 100 | + errors: [ |
| 101 | + { |
| 102 | + message: 'Using \'CallExpression[callee.type=\'Identifier\'][callee.name=\'$gettext\'] TemplateLiteral\' is not allowed.', |
| 103 | + line: 3, |
| 104 | + column: 29, |
| 105 | + endLine: 3, |
| 106 | + endColumn: 34 |
| 107 | + }, |
| 108 | + { |
| 109 | + message: 'Using \'CallExpression[callee.type=\'Identifier\'][callee.name=\'$gettext\'] TemplateLiteral\' is not allowed.', |
| 110 | + line: 3, |
| 111 | + column: 48, |
| 112 | + endLine: 3, |
| 113 | + endColumn: 53 |
| 114 | + } |
| 115 | + ] |
| 116 | + } |
| 117 | + ] |
| 118 | +}) |
0 commit comments