-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create rule CSS:S125: Sections of code should not be commented out
- Loading branch information
1 parent
8c2a4c3
commit ae070ad
Showing
15 changed files
with
278 additions
and
19 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
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,5 @@ | ||
{ | ||
"project:custom/S125.css": [ | ||
3 | ||
] | ||
} |
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,4 @@ | ||
p { | ||
color: red; | ||
/* font-size: large; */ | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,20 @@ | ||
/* | ||
* SonarQube JavaScript Plugin | ||
* Copyright (C) 2011-2023 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
export { rule } from './rule'; |
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,69 @@ | ||
/* | ||
* SonarQube JavaScript Plugin | ||
* Copyright (C) 2011-2023 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
// https://sonarsource.github.io/rspec/#/rspec/S125/css | ||
|
||
import * as stylelint from 'stylelint'; | ||
import { parse } from 'postcss'; | ||
|
||
const ruleName = 'no-commented-code'; | ||
const messages = { commentedCode: 'Remove this commented out code.' }; | ||
|
||
const ruleImpl: stylelint.RuleBase = () => { | ||
return (root: any, result: any) => { | ||
root.walkComments((comment: any) => { | ||
const { text } = comment; | ||
if (isLikelyCss(text)) { | ||
try { | ||
parse(text); | ||
stylelint.utils.report({ | ||
ruleName, | ||
result, | ||
message: messages.commentedCode, | ||
node: comment, | ||
}); | ||
} catch { | ||
/* syntax error */ | ||
} | ||
} | ||
}); | ||
}; | ||
|
||
function isLikelyCss(text: string) { | ||
// Regular expression to match CSS selectors, properties, and values | ||
const ruleRegex = /([a-z0-9\s,.\-#:_]+)\{([^}]*)\}/i; | ||
|
||
// Regular expression to match CSS declarations | ||
const declRegex = /([a-z-]+)\s*:\s*([^;]+);/i; | ||
|
||
// Regular expression to match CSS at-rules | ||
const atRuleRegex = /@([a-z-]*)\s*([^;{]*)(;|(\{([^}]*)\}))/i; | ||
|
||
// Test the text against the regular expressions | ||
return ruleRegex.test(text) || declRegex.test(text) || atRuleRegex.test(text); | ||
} | ||
}; | ||
|
||
export const rule = stylelint.createPlugin( | ||
ruleName, | ||
Object.assign(ruleImpl, { | ||
messages, | ||
ruleName, | ||
}), | ||
) as { ruleName: string; rule: stylelint.Rule }; |
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,103 @@ | ||
/* | ||
* SonarQube JavaScript Plugin | ||
* Copyright (C) 2011-2023 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
import { StylelintRuleTester } from '../../../tests/tools/tester'; | ||
import { rule } from './rule'; | ||
|
||
const ruleTester = new StylelintRuleTester(rule); | ||
ruleTester.run('no-commented-code', { | ||
valid: [ | ||
{ | ||
description: 'no comment', | ||
code: 'p {}', | ||
}, | ||
{ | ||
description: 'no commented code', | ||
code: '/* hello, world! */', | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
description: 'selector', | ||
code: '/* p {} */', | ||
errors: [{ text: 'Remove this commented out code.', line: 1, column: 1 }], | ||
}, | ||
{ | ||
description: 'multiple selectors', | ||
code: '/* p, div {} */', | ||
errors: [{ text: 'Remove this commented out code.', line: 1, column: 1 }], | ||
}, | ||
{ | ||
description: 'declaration', | ||
code: '/* color: blue; */', | ||
errors: [{ text: 'Remove this commented out code.', line: 1, column: 1 }], | ||
}, | ||
{ | ||
description: 'selector declaration', | ||
code: '/* p { color: blue; } */', | ||
errors: [{ text: 'Remove this commented out code.', line: 1, column: 1 }], | ||
}, | ||
{ | ||
description: 'multiple declarations', | ||
code: '/* div { font-size: 20px; color: red; } */', | ||
errors: [{ text: 'Remove this commented out code.', line: 1, column: 1 }], | ||
}, | ||
{ | ||
description: 'class selector', | ||
code: '/* .class { background-color: red; } */', | ||
errors: [{ text: 'Remove this commented out code.', line: 1, column: 1 }], | ||
}, | ||
{ | ||
description: 'id selector', | ||
code: '/* #id:hover { border: 1px solid black; } */', | ||
errors: [{ text: 'Remove this commented out code.', line: 1, column: 1 }], | ||
}, | ||
{ | ||
description: 'attribute selector', | ||
code: '/* a[href] { color: purple; } */', | ||
errors: [{ text: 'Remove this commented out code.', line: 1, column: 1 }], | ||
}, | ||
{ | ||
description: 'media query', | ||
code: '/* @media (max-width: 600px) { .class { font-size: 18px; } } */', | ||
errors: [{ text: 'Remove this commented out code.', line: 1, column: 1 }], | ||
}, | ||
{ | ||
description: '@keyframes', | ||
code: '/* @keyframes mymove { 0% { top: 0px; } 100% { top: 200px; } } */', | ||
errors: [{ text: 'Remove this commented out code.', line: 1, column: 1 }], | ||
}, | ||
{ | ||
description: 'import', | ||
code: '/* @import url("styles.css"); */', | ||
errors: [{ text: 'Remove this commented out code.', line: 1, column: 1 }], | ||
}, | ||
{ | ||
description: 'multline', | ||
code: ` | ||
/* | ||
p { | ||
color: blue; | ||
} | ||
*/ | ||
`, | ||
errors: [{ text: 'Remove this commented out code.', line: 2, column: 1 }], | ||
}, | ||
], | ||
}); |
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
31 changes: 31 additions & 0 deletions
31
sonar-plugin/css/src/main/java/org/sonar/css/rules/NoCommentedCode.java
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,31 @@ | ||
/* | ||
* SonarQube JavaScript Plugin | ||
* Copyright (C) 2011-2023 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package org.sonar.css.rules; | ||
|
||
import org.sonar.check.Rule; | ||
|
||
@Rule(key = "S125") | ||
public class NoCommentedCode implements CssRule { | ||
|
||
@Override | ||
public String stylelintKey() { | ||
return "no-commented-code"; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
sonar-plugin/css/src/main/resources/org/sonar/l10n/css/rules/css/S125.html
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,5 @@ | ||
<h2>Why is this an issue?</h2> | ||
<p>Commented-out code distracts the focus from the actual executed code. It creates a noise that increases maintenance code. And because it is never | ||
executed, it quickly becomes out of date and invalid.</p> | ||
<p>Commented-out code should be deleted and can be retrieved from source control history if required.</p> | ||
|
23 changes: 23 additions & 0 deletions
23
sonar-plugin/css/src/main/resources/org/sonar/l10n/css/rules/css/S125.json
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,23 @@ | ||
{ | ||
"title": "Sections of code should not be commented out", | ||
"type": "CODE_SMELL", | ||
"code": { | ||
"impacts": { | ||
"MAINTAINABILITY": "MEDIUM" | ||
}, | ||
"attribute": "CLEAR" | ||
}, | ||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant\/Issue", | ||
"constantCost": "5min" | ||
}, | ||
"tags": [ | ||
"unused" | ||
], | ||
"defaultSeverity": "Major", | ||
"ruleSpecification": "RSPEC-125", | ||
"sqKey": "S125", | ||
"scope": "All", | ||
"quickfix": "unknown" | ||
} |
1 change: 1 addition & 0 deletions
1
sonar-plugin/css/src/main/resources/org/sonar/l10n/css/rules/css/Sonar_way_profile.json
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
{ | ||
"name": "Sonar way", | ||
"ruleKeys": [ | ||
"S125", | ||
"S1116", | ||
"S1128", | ||
"S4647", | ||
|