-
-
Notifications
You must be signed in to change notification settings - Fork 669
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Yosuke Ota <[email protected]>
- Loading branch information
Showing
5 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,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> |
This file contains 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 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 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 | ||
} | ||
] | ||
} | ||
] | ||
}) |