Skip to content

Commit 13167ed

Browse files
ItMagaota-meshi
andauthored
Add vue/no-console rule (#2194)
Co-authored-by: Yosuke Ota <[email protected]>
1 parent 4dfb4d7 commit 13167ed

File tree

5 files changed

+201
-0
lines changed

5 files changed

+201
-0
lines changed

docs/rules/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ The following rules extend the rules provided by ESLint itself and apply them to
299299
| [vue/keyword-spacing](./keyword-spacing.md) | Enforce consistent spacing before and after keywords in `<template>` | :wrench: | :lipstick: |
300300
| [vue/max-len](./max-len.md) | enforce a maximum line length in `.vue` files | | :lipstick: |
301301
| [vue/multiline-ternary](./multiline-ternary.md) | Enforce newlines between operands of ternary expressions in `<template>` | :wrench: | :lipstick: |
302+
| [vue/no-console](./no-console.md) | Disallow the use of `console` in `<template>` | | :hammer: |
302303
| [vue/no-constant-condition](./no-constant-condition.md) | Disallow constant expressions in conditions in `<template>` | | :warning: |
303304
| [vue/no-empty-pattern](./no-empty-pattern.md) | Disallow empty destructuring patterns in `<template>` | | :warning: |
304305
| [vue/no-extra-parens](./no-extra-parens.md) | Disallow unnecessary parentheses in `<template>` | :wrench: | :lipstick: |

docs/rules/no-console.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
pageClass: rule-details
3+
sidebarDepth: 0
4+
title: vue/no-console
5+
description: Disallow the use of `console` in `<template>`
6+
---
7+
# vue/no-console
8+
9+
> Disallow the use of `console` in `<template>`
10+
11+
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>
12+
13+
## :book: Rule Details
14+
15+
This rule is the same rule as core [no-console] rule but it applies to the expressions in `<template>`.
16+
17+
## :books: Further Reading
18+
19+
- [no-console]
20+
21+
[no-console]: https://eslint.org/docs/latest/rules/no-console
22+
23+
## :mag: Implementation
24+
25+
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-console.js)
26+
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-console.js)
27+
28+
<sup>Taken with ❤️ [from ESLint core](https://eslint.org/docs/rules/no-console)</sup>

lib/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ module.exports = {
6666
'no-boolean-default': require('./rules/no-boolean-default'),
6767
'no-child-content': require('./rules/no-child-content'),
6868
'no-computed-properties-in-data': require('./rules/no-computed-properties-in-data'),
69+
'no-console': require('./rules/no-console'),
6970
'no-constant-condition': require('./rules/no-constant-condition'),
7071
'no-custom-modifiers-on-v-model': require('./rules/no-custom-modifiers-on-v-model'),
7172
'no-deprecated-data-object-declaration': require('./rules/no-deprecated-data-object-declaration'),

lib/rules/no-console.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* @author ItMaga <https://github.com/ItMaga>
3+
* See LICENSE file in root directory for full license.
4+
*/
5+
'use strict'
6+
7+
const utils = require('../utils')
8+
9+
// eslint-disable-next-line no-invalid-meta, no-invalid-meta-docs-categories
10+
module.exports = utils.wrapCoreRule('no-console', {
11+
create(context) {
12+
const options = context.options[0] || {}
13+
const allowed = options.allow || []
14+
15+
/**
16+
* Copied from the core rule `no-console`.
17+
* Checks whether the property name of the given MemberExpression node
18+
* is allowed by options or not.
19+
* @param {MemberExpression} node The MemberExpression node to check.
20+
* @returns {boolean} `true` if the property name of the node is allowed.
21+
*/
22+
function isAllowed(node) {
23+
const propertyName = utils.getStaticPropertyName(node)
24+
25+
return propertyName && allowed.includes(propertyName)
26+
}
27+
28+
return {
29+
MemberExpression(node) {
30+
if (
31+
node.object.type === 'Identifier' &&
32+
node.object.name === 'console' &&
33+
!isAllowed(node)
34+
) {
35+
context.report({
36+
node: node.object,
37+
loc: node.object.loc,
38+
messageId: 'unexpected'
39+
})
40+
}
41+
}
42+
}
43+
}
44+
})

tests/lib/rules/no-console.js

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/**
2+
* @author ItMaga <https://github.com/ItMaga>
3+
* See LICENSE file in root directory for full license.
4+
*/
5+
'use strict'
6+
7+
const RuleTester = require('eslint').RuleTester
8+
const rule = require('../../../lib/rules/no-console')
9+
10+
const tester = new RuleTester({
11+
parser: require.resolve('vue-eslint-parser'),
12+
parserOptions: {
13+
ecmaVersion: 2020,
14+
sourceType: 'module'
15+
}
16+
})
17+
18+
tester.run('no-console', rule, {
19+
valid: [
20+
{
21+
filename: 'test.vue',
22+
code: `
23+
<template>
24+
<button @click="Console.log">button</button>
25+
</template>
26+
`
27+
},
28+
{
29+
filename: 'test.vue',
30+
code: `
31+
<template>
32+
<button>{{ console.warn('warn') }}</button>
33+
</template>
34+
`,
35+
options: [{ allow: ['warn'] }]
36+
},
37+
{
38+
filename: 'test.vue',
39+
code: `
40+
<template>
41+
<button :foo="console.error">button</button>
42+
</template>
43+
`,
44+
options: [{ allow: ['error'] }]
45+
},
46+
{
47+
filename: 'test.vue',
48+
code: `
49+
<template>
50+
<button :foo="console.log">{{ console.log('test') }}</button>
51+
{{ console.warn('test') }}
52+
{{ console.info('test') }}
53+
</template>
54+
`,
55+
options: [{ allow: ['log', 'warn', 'info'] }]
56+
}
57+
],
58+
invalid: [
59+
{
60+
filename: 'test.vue',
61+
code: `
62+
<template>
63+
<button @click="console.log">button</button>
64+
</template>
65+
`,
66+
errors: [
67+
{
68+
message: 'Unexpected console statement.',
69+
line: 3,
70+
column: 25
71+
}
72+
]
73+
},
74+
{
75+
filename: 'test.vue',
76+
code: `
77+
<template>
78+
<button :foo="console.log">{{ console.log('test') }}</button>
79+
</template>
80+
`,
81+
errors: [
82+
{
83+
message: 'Unexpected console statement.',
84+
line: 3,
85+
column: 23
86+
},
87+
{
88+
message: 'Unexpected console statement.',
89+
line: 3,
90+
column: 39
91+
}
92+
]
93+
},
94+
{
95+
filename: 'test.vue',
96+
code: `
97+
<template>
98+
<button @click="() => console.log">button</button>
99+
</template>
100+
`,
101+
errors: [
102+
{
103+
message: 'Unexpected console statement.',
104+
line: 3,
105+
column: 31
106+
}
107+
]
108+
},
109+
{
110+
filename: 'test.vue',
111+
code: `
112+
<template>
113+
<button @click="console.log">button</button>
114+
{{ console.error('test') }}
115+
</template>
116+
`,
117+
options: [{ allow: ['error'] }],
118+
errors: [
119+
{
120+
message: 'Unexpected console statement.',
121+
line: 3,
122+
column: 25
123+
}
124+
]
125+
}
126+
]
127+
})

0 commit comments

Comments
 (0)