-
-
Notifications
You must be signed in to change notification settings - Fork 679
Add vue/no-console
rule
#2194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add vue/no-console
rule
#2194
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8ed1177
Add 'vue/no-console` rule
ItMaga 7a6335b
add `eslint-disable`
ItMaga 2e93491
Merge remote-tracking branch 'origin/master' into no-console
ItMaga 6c25866
remove unnecessary options
ItMaga cdbe042
fix lint
ItMaga b02bd70
Update docs/rules/no-console.md
ota-meshi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--- | ||
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>`. | ||
|
||
</eslint-code-block> | ||
|
||
## :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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
] | ||
} | ||
] | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.