[javascript] New rule AvoidConsoleStatements

Fixes #5105
This commit is contained in:
Andreas Dangel
2024-07-19 15:33:19 +02:00
parent 39750cf158
commit 838705c8da
5 changed files with 117 additions and 3 deletions

View File

@ -14,7 +14,15 @@ This is a {{ site.pmd.release_type }} release.
### 🚀 New and noteworthy
### 🌟 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.
### 🐛 Fixed Issues
* javascript-performance
* [#5105](https://github.com/pmd/pmd/issues/5105): \[javascript] Prohibit any console methods
### 🚨 API Changes

View File

@ -5,7 +5,8 @@
rulesets.filenames=\
category/ecmascript/bestpractices.xml,\
category/ecmascript/codestyle.xml,\
category/ecmascript/errorprone.xml
category/ecmascript/errorprone.xml,\
category/ecmascript/performance.xml
#
#empty categories:
@ -13,5 +14,4 @@ rulesets.filenames=\
#category/ecmascript/design.xml,
#category/ecmascript/documentation.xml,
#category/ecmascript/multithreading.xml,
#category/ecmascript/performance.xml,
#category/ecmascript/security.xml,

View File

@ -1,5 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="Performance"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@ -9,4 +8,38 @@
Rules that flag suboptimal code.
</description>
<rule name="AvoidConsoleStatements"
language="ecmascript"
since="7.4.0"
message="Avoid console statements since they negatively impact performance"
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_ecmascript_performance.html#avoidconsolestatements">
<description>
Using the console for logging in production might negatively impact performance.
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]]
]
|
//FunctionCall[PropertyGet
[PropertyGet[1]
[Name[1][@Identifier = 'window']]
[Name[2][@Identifier = 'console']]
]
[Name[1][@Identifier = $methods]]
]
]]>
</value>
</property>
</properties>
</rule>
</ruleset>

View File

@ -0,0 +1,11 @@
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.ecmascript.rule.performance;
import net.sourceforge.pmd.test.PmdRuleTst;
class AvoidConsoleStatementsTest extends PmdRuleTst {
// no additional unit tests
}

View File

@ -0,0 +1,62 @@
<?xml version="1.0" encoding="UTF-8"?>
<test-data
xmlns="http://pmd.sourceforge.net/rule-tests"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>
<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
]]></code>
</test-code>
<test-code>
<description>Default console methods via window.console should be flagged</description>
<expected-problems>6</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
]]></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>
<code><![CDATA[
var MyFoo = {
debug: function(a) {
// ...
}
};
MyFoo.debug('bar'); // ok, it is not console.debug
]]></code>
</test-code>
</test-data>