Skip to content

Commit

Permalink
Add vue/no-console rule (#2194)
Browse files Browse the repository at this point in the history
Co-authored-by: Yosuke Ota <[email protected]>
  • Loading branch information
ItMaga and ota-meshi authored May 30, 2023
1 parent 4dfb4d7 commit 13167ed
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/rules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ The following rules extend the rules provided by ESLint itself and apply them to
| [vue/keyword-spacing](./keyword-spacing.md) | Enforce consistent spacing before and after keywords in `<template>` | :wrench: | :lipstick: |
| [vue/max-len](./max-len.md) | enforce a maximum line length in `.vue` files | | :lipstick: |
| [vue/multiline-ternary](./multiline-ternary.md) | Enforce newlines between operands of ternary expressions in `<template>` | :wrench: | :lipstick: |
| [vue/no-console](./no-console.md) | Disallow the use of `console` in `<template>` | | :hammer: |
| [vue/no-constant-condition](./no-constant-condition.md) | Disallow constant expressions in conditions in `<template>` | | :warning: |
| [vue/no-empty-pattern](./no-empty-pattern.md) | Disallow empty destructuring patterns in `<template>` | | :warning: |
| [vue/no-extra-parens](./no-extra-parens.md) | Disallow unnecessary parentheses in `<template>` | :wrench: | :lipstick: |
Expand Down
28 changes: 28 additions & 0 deletions docs/rules/no-console.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
pageClass: rule-details
sidebarDepth: 0
title: vue/no-console
description: Disallow the use of `console` in `<template>`
---
# vue/no-console

> Disallow the use of `console` in `<template>`
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>

## :book: Rule Details

This rule is the same rule as core [no-console] rule but it applies to the expressions in `<template>`.

## :books: Further Reading

- [no-console]

[no-console]: https://eslint.org/docs/latest/rules/no-console

## :mag: Implementation

- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-console.js)
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-console.js)

<sup>Taken with ❤️ [from ESLint core](https://eslint.org/docs/rules/no-console)</sup>
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ module.exports = {
'no-boolean-default': require('./rules/no-boolean-default'),
'no-child-content': require('./rules/no-child-content'),
'no-computed-properties-in-data': require('./rules/no-computed-properties-in-data'),
'no-console': require('./rules/no-console'),
'no-constant-condition': require('./rules/no-constant-condition'),
'no-custom-modifiers-on-v-model': require('./rules/no-custom-modifiers-on-v-model'),
'no-deprecated-data-object-declaration': require('./rules/no-deprecated-data-object-declaration'),
Expand Down
44 changes: 44 additions & 0 deletions lib/rules/no-console.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* @author ItMaga <https://github.com/ItMaga>
* See LICENSE file in root directory for full license.
*/
'use strict'

const utils = require('../utils')

// eslint-disable-next-line no-invalid-meta, no-invalid-meta-docs-categories
module.exports = utils.wrapCoreRule('no-console', {
create(context) {
const options = context.options[0] || {}
const allowed = options.allow || []

/**
* Copied from the core rule `no-console`.
* Checks whether the property name of the given MemberExpression node
* is allowed by options or not.
* @param {MemberExpression} node The MemberExpression node to check.
* @returns {boolean} `true` if the property name of the node is allowed.
*/
function isAllowed(node) {
const propertyName = utils.getStaticPropertyName(node)

return propertyName && allowed.includes(propertyName)
}

return {
MemberExpression(node) {
if (
node.object.type === 'Identifier' &&
node.object.name === 'console' &&
!isAllowed(node)
) {
context.report({
node: node.object,
loc: node.object.loc,
messageId: 'unexpected'
})
}
}
}
}
})
127 changes: 127 additions & 0 deletions tests/lib/rules/no-console.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/**
* @author ItMaga <https://github.com/ItMaga>
* See LICENSE file in root directory for full license.
*/
'use strict'

const RuleTester = require('eslint').RuleTester
const rule = require('../../../lib/rules/no-console')

const tester = new RuleTester({
parser: require.resolve('vue-eslint-parser'),
parserOptions: {
ecmaVersion: 2020,
sourceType: 'module'
}
})

tester.run('no-console', rule, {
valid: [
{
filename: 'test.vue',
code: `
<template>
<button @click="Console.log">button</button>
</template>
`
},
{
filename: 'test.vue',
code: `
<template>
<button>{{ console.warn('warn') }}</button>
</template>
`,
options: [{ allow: ['warn'] }]
},
{
filename: 'test.vue',
code: `
<template>
<button :foo="console.error">button</button>
</template>
`,
options: [{ allow: ['error'] }]
},
{
filename: 'test.vue',
code: `
<template>
<button :foo="console.log">{{ console.log('test') }}</button>
{{ console.warn('test') }}
{{ console.info('test') }}
</template>
`,
options: [{ allow: ['log', 'warn', 'info'] }]
}
],
invalid: [
{
filename: 'test.vue',
code: `
<template>
<button @click="console.log">button</button>
</template>
`,
errors: [
{
message: 'Unexpected console statement.',
line: 3,
column: 25
}
]
},
{
filename: 'test.vue',
code: `
<template>
<button :foo="console.log">{{ console.log('test') }}</button>
</template>
`,
errors: [
{
message: 'Unexpected console statement.',
line: 3,
column: 23
},
{
message: 'Unexpected console statement.',
line: 3,
column: 39
}
]
},
{
filename: 'test.vue',
code: `
<template>
<button @click="() => console.log">button</button>
</template>
`,
errors: [
{
message: 'Unexpected console statement.',
line: 3,
column: 31
}
]
},
{
filename: 'test.vue',
code: `
<template>
<button @click="console.log">button</button>
{{ console.error('test') }}
</template>
`,
options: [{ allow: ['error'] }],
errors: [
{
message: 'Unexpected console statement.',
line: 3,
column: 25
}
]
}
]
})

0 comments on commit 13167ed

Please sign in to comment.