-
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.
… elements should have a valid language property (#4387)
- Loading branch information
1 parent
2be9541
commit fdde183
Showing
13 changed files
with
222 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,5 +10,8 @@ | |
], | ||
"file-for-rules:S6583.ts": [ | ||
0 | ||
], | ||
"file-for-rules:S6849.tsx": [ | ||
0 | ||
] | ||
} |
5 changes: 5 additions & 0 deletions
5
its/ruling/src/test/expected/jsts/file-for-rules/typescript-S6849.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,5 @@ | ||
{ | ||
"file-for-rules:S6849.tsx": [ | ||
2 | ||
] | ||
} |
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,3 @@ | ||
function html() { | ||
return <html></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,2 @@ | ||
<html></html>; // Noncompliant {{<html> elements must have the lang prop.}} | ||
<html lang="yolo"></html>; // Noncompliant {{lang attribute must have a valid value.}} |
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 @@ | ||
/* | ||
* 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 { check } from '../tools'; | ||
import { rule } from './'; | ||
import path from 'path'; | ||
|
||
const sonarId = path.basename(__dirname); | ||
|
||
describe(`Rule ${sonarId}`, () => { | ||
check(sonarId, rule, __dirname); | ||
}); |
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,44 @@ | ||
/* | ||
* 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/S6849/javascript | ||
|
||
import { Rule } from 'eslint'; | ||
import { rules as jsxA11yRules } from 'eslint-plugin-jsx-a11y'; | ||
import { mergeRules } from '../helpers'; | ||
|
||
const langRule = jsxA11yRules['lang']; | ||
const htmlHasLangRule = jsxA11yRules['html-has-lang']; | ||
|
||
export const rule: Rule.RuleModule = { | ||
meta: { | ||
hasSuggestions: true, | ||
messages: { | ||
...langRule.meta!.messages, | ||
...htmlHasLangRule.meta!.messages, | ||
}, | ||
}, | ||
|
||
create(context: Rule.RuleContext) { | ||
const langListener: Rule.RuleListener = langRule.create(context); | ||
const htmlHasLangListener: Rule.RuleListener = htmlHasLangRule.create(context); | ||
|
||
return mergeRules(langListener, htmlHasLangListener); | ||
}, | ||
}; |
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
36 changes: 36 additions & 0 deletions
36
...-plugin/javascript-checks/src/main/java/org/sonar/javascript/checks/HtmlHasLangCheck.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,36 @@ | ||
/** | ||
* 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.javascript.checks; | ||
|
||
import org.sonar.check.Rule; | ||
import org.sonar.plugins.javascript.api.EslintBasedCheck; | ||
import org.sonar.plugins.javascript.api.JavaScriptRule; | ||
import org.sonar.plugins.javascript.api.TypeScriptRule; | ||
|
||
@TypeScriptRule | ||
@JavaScriptRule | ||
@Rule(key = "S6849") | ||
public class HtmlHasLangCheck implements EslintBasedCheck { | ||
|
||
@Override | ||
public String eslintKey() { | ||
return "html-has-lang"; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...avascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/S6849.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,46 @@ | ||
<p>The <code><a></code> tag in HTML is designed to create hyperlinks, which can link to different sections of the same page, different pages, or | ||
even different websites. However, sometimes developers misuse <code><a></code> tags as buttons, which can lead to accessibility issues and | ||
unexpected behavior.</p> | ||
<p>This rule checks that <code><a></code> tags are used correctly as hyperlinks and not misused as buttons. It verifies that each | ||
<code><a></code> tag has a <code>href</code> attribute, which is necessary for it to function as a hyperlink. If an <code><a></code> tag | ||
is used without a <code>href</code> attribute, it behaves like a button, which is not its intended use.</p> | ||
<p>Using the correct HTML elements for their intended purpose is crucial for accessibility and usability. It ensures that the website behaves as | ||
expected and can be used by all users, including those using assistive technologies. Misusing HTML elements can lead to a poor user experience and | ||
potential accessibility violations.</p> | ||
<p>Compliance with this rule will ensure that your HTML code is semantically correct, accessible, and behaves as expected.</p> | ||
<h2>Why is this an issue?</h2> | ||
<p>Misusing <code><a></code> tags as buttons can lead to several issues:</p> | ||
<ul> | ||
<li> Accessibility: Screen readers and other assistive technologies rely on the correct use of HTML elements to interpret and interact with the | ||
content. When <code><a></code> tags are used as buttons, it can confuse these technologies and make the website less accessible to users with | ||
disabilities. </li> | ||
<li> Usability: The behavior of <code><a></code> tags and buttons is different. For example, buttons can be triggered using the space bar, | ||
while <code><a></code> tags cannot. Misusing these elements can lead to unexpected behavior and a poor user experience. </li> | ||
<li> Semantic correctness: Each HTML element has a specific purpose and meaning. Using elements for purposes other than their intended use can make | ||
the code harder to understand and maintain. </li> | ||
<li> SEO implications: Search engines use the structure and semantics of HTML to understand and rank web pages. Misusing HTML elements can | ||
negatively impact a website’s SEO. </li> | ||
</ul> | ||
<h2>How to fix it</h2> | ||
<p>To fix this issue, you should use the appropriate HTML elements for their intended purposes. If you need to create a hyperlink, use the | ||
<code><a></code> tag with a <code>href</code> attribute. If you need to create a button, use the <code><button></code> tag.</p> | ||
<h3>Code examples</h3> | ||
<h4>Noncompliant code example</h4> | ||
<pre data-diff-id="1" data-diff-type="noncompliant"> | ||
<a href="javascript:void(0)" onClick={foo}>Perform action</a> | ||
<a href="#" onClick={foo}>Perform action</a> | ||
<a onClick={foo}>Perform action</a> | ||
</pre> | ||
<h4>Compliant solution</h4> | ||
<pre data-diff-id="1" data-diff-type="compliant"> | ||
<button onClick={foo}>Perform action</button> | ||
</pre> | ||
<h2>Resources</h2> | ||
<h3>Documentation</h3> | ||
<ul> | ||
<li> WebAIM - <a href="https://webaim.org/techniques/hypertext/">Introduction to Links and Hypertext</a> </li> | ||
<li> <a href="https://marcysutton.com/links-vs-buttons-in-modern-web-applications/">Links vs. Buttons in Modern Web Applications</a> </li> | ||
<li> <a | ||
href="https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/docs/rules/anchor-is-valid.md#jsx-a11yanchor-is-valid:~:text=Using%20ARIA%20%2D%20Notes%20on%20ARIA%20use%20in%20HTML">Using ARIA - Notes on ARIA use in HTML</a> </li> | ||
</ul> | ||
|
28 changes: 28 additions & 0 deletions
28
...avascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/S6849.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,28 @@ | ||
{ | ||
"title": "HTML elements should have a valid language attribute", | ||
"type": "CODE_SMELL", | ||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant\/Issue", | ||
"constantCost": "5min" | ||
}, | ||
"tags": [ | ||
"accessibility", | ||
"react" | ||
], | ||
"defaultSeverity": "Minor", | ||
"ruleSpecification": "RSPEC-6849", | ||
"sqKey": "S6849", | ||
"scope": "All", | ||
"quickfix": "targeted", | ||
"code": { | ||
"impacts": { | ||
"RELIABILITY": "MEDIUM" | ||
}, | ||
"attribute": "LOGICAL" | ||
}, | ||
"compatibleLanguages": [ | ||
"JAVASCRIPT", | ||
"TYPESCRIPT" | ||
] | ||
} |
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 |
---|---|---|
|
@@ -313,6 +313,7 @@ | |
"S6843", | ||
"S6844", | ||
"S6845", | ||
"S6846" | ||
"S6846", | ||
"S6849" | ||
] | ||
} |