[javascript] Simplify AvoidConsoleStatements

Now any usage of console is flagged.
This commit is contained in:
Andreas Dangel 2024-07-25 12:01:28 +02:00
parent 8b1da5d651
commit dbe5624a9e
No known key found for this signature in database
GPG Key ID: 93450DF2DF9A3FA3
3 changed files with 37 additions and 37 deletions

View File

@ -17,8 +17,8 @@ This is a {{ site.pmd.release_type }} release.
### 🌟 New and changed rules
#### New Rules
* The new JavaScript rule {%rule ecmascript/performance/AvoidConsoleStatements %} finds usages of `console.log` and
similar function calls. Using these in production code might negatively impact performance.
* The new JavaScript rule {%rule ecmascript/performance/AvoidConsoleStatements %} finds any function calls
on the Console API (e.g. `console.log`). Using these in production code might negatively impact performance.
### 🐛 Fixed Issues
* javascript-performance

View File

@ -20,13 +20,12 @@ In addition, logging could expose sensitive data.
</description>
<priority>3</priority>
<properties>
<property name="methods" type="List[String]" value="log,error,info,warn,debug,trace" description="The methods of the console object that should be flagged."/>
<property name="xpath">
<value>
<![CDATA[
//FunctionCall[PropertyGet
[Name[1][@Identifier = 'console']]
[Name[2][@Identifier = $methods]]
[Name[2][@Identifier]]
]
|
//FunctionCall[PropertyGet
@ -34,12 +33,17 @@ In addition, logging could expose sensitive data.
[Name[1][@Identifier = 'window']]
[Name[2][@Identifier = 'console']]
]
[Name[1][@Identifier = $methods]]
[Name[1][@Identifier]]
]
]]>
</value>
</property>
</properties>
<example><![CDATA[
var myObj = getData();
console.log(myObj); // bad
console.debug("myObj:", myObj); // bad
]]></example>
</rule>
</ruleset>

View File

@ -5,47 +5,37 @@
xsi:schemaLocation="http://pmd.sourceforge.net/rule-tests https://pmd.github.io/schema/rule-tests_1_0_0.xsd">
<test-code>
<description>Default console methods should be flagged</description>
<expected-problems>6</expected-problems>
<description>Any console method should be flagged</description>
<expected-problems>9</expected-problems>
<code><![CDATA[
console.log('foo'); // bad
console.error('foo'); // bad
console.info('foo'); // bad
console.warn('foo'); // bad
console.debug('foo'); // bad
console.trace('foo'); // bad
console.log('foo');
console.error('foo');
console.info('foo');
console.warn('foo');
console.debug('foo');
console.trace('foo');
console.assert(false, 'foo');
console.dir(myObj);
console.group('label');
]]></code>
</test-code>
<test-code>
<description>Default console methods via window.console should be flagged</description>
<expected-problems>6</expected-problems>
<description>Any console method via window.console should be flagged</description>
<expected-problems>9</expected-problems>
<code><![CDATA[
window.console.log('foo'); // bad
window.console.error('foo'); // bad
window.console.info('foo'); // bad
window.console.warn('foo'); // bad
window.console.debug('foo'); // bad
window.console.trace('foo'); // bad
window.console.log('foo');
window.console.error('foo');
window.console.info('foo');
window.console.warn('foo');
window.console.debug('foo');
window.console.trace('foo');
window.console.assert(false, 'foo');
window.console.dir(myObj);
window.console.group('label');
]]></code>
</test-code>
<test-code>
<description>Some console methods should be flagged</description>
<rule-property name="methods">log,info,debug,trace</rule-property>
<expected-problems>4</expected-problems>
<expected-linenumbers>1,3,5,6</expected-linenumbers>
<code><![CDATA[
console.log('foo'); // bad
console.error('foo'); // ok per configuration
console.info('foo'); // bad
console.warn('foo'); // ok per configuration
console.debug('foo'); // bad
console.trace('foo'); // bad
]]></code>
</test-code>
<test-code>
<description>Other similar methods shouldn't be flagged</description>
<expected-problems>0</expected-problems>
@ -53,10 +43,16 @@ console.trace('foo'); // bad
var MyFoo = {
debug: function(a) {
// ...
},
console: {
debug: function(a) {
// ...
}
}
};
MyFoo.debug('bar'); // ok, it is not console.debug
MyFoo.console.debug('bar'); // ok, it is not console.debug
]]></code>
</test-code>
</test-data>