Skip to content

Commit

Permalink
Modify rule S6747 (no-unknown-property ): add jsx-a11y/aria-props (
Browse files Browse the repository at this point in the history
  • Loading branch information
ilia-kebets-sonarsource authored Oct 11, 2023
1 parent 4785439 commit 215332f
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@
40,
41
],
"vuetify:packages/vuetify/src/components/VDialog/VDialog.tsx": [
120
],
"vuetify:packages/vuetify/src/components/VDivider/VDivider.tsx": [
47
],
Expand Down
2 changes: 1 addition & 1 deletion packages/jsts/src/rules/S6747/cb.fixture.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
// ^^^^^^^^^^^
// fix@qf1 {{Replace with 'className'}}
// edit@qf1 [[sc=0;ec=24]] {{<div className="foo"></div>;}}
<div aria-foo="bar"></div>; // Noncompliant
<div aria-foo="bar"></div>; // Noncompliant {{aria-foo: This attribute is an invalid ARIA attribute.}}
<img src="foo.png" />
5 changes: 1 addition & 4 deletions packages/jsts/src/rules/S6747/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,4 @@
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { rules } from 'eslint-plugin-react';
import { decorate } from './decorator';

export const rule = decorate(rules['no-unknown-property']);
export { rule } from './rule';
79 changes: 79 additions & 0 deletions packages/jsts/src/rules/S6747/rule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* 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/S6747/javascript

import { Rule } from 'eslint';
import { rules as reactRules } from 'eslint-plugin-react';
import { rules as jsxA11yRules } from 'eslint-plugin-jsx-a11y';
import { interceptReport, mergeRules } from '../helpers';
import { decorate } from './decorator';
import { TSESTree } from '@typescript-eslint/experimental-utils';

const noUnkownProp = reactRules['no-unknown-property'];
const decoratedNoUnkownProp = decorate(noUnkownProp);

/**
* We keep a single occurence of issues raised by both rules, keeping the ones raised by 'aria-props'
* in case of duplicate.
* The current logic relies on the fact that the listener of 'aria-props' runs first because
* it is alphabetically "smaller", which is how we set them up in mergeRules.
*/

/**
* start offsets of nodes that raised issues in eslint-plugin-jsx-a11y's aria-props
*/
const flaggedNodeStarts = new Map();

const ariaPropsRule = jsxA11yRules['aria-props'];
const decoratedAriaPropsRule = interceptReport(ariaPropsRule, (context, descriptor) => {
if ('node' in descriptor) {
const start = (descriptor.node as TSESTree.Node).range[0];
if (!flaggedNodeStarts.get(start)) {
flaggedNodeStarts.set(start, true);
context.report(descriptor);
}
}
});

const twiceDecoratedNoUnkownProp = interceptReport(decoratedNoUnkownProp, (context, descriptor) => {
if ('node' in descriptor) {
const start = (descriptor.node as TSESTree.Node).range[0];
if (!flaggedNodeStarts.get(start)) {
context.report(descriptor);
}
}
});

export const rule: Rule.RuleModule = {
meta: {
hasSuggestions: true,
messages: {
...decoratedAriaPropsRule.meta!.messages,
...twiceDecoratedNoUnkownProp.meta!.messages,
},
},

create(context: Rule.RuleContext) {
const ariaPropsListener: Rule.RuleListener = decoratedAriaPropsRule.create(context);
const noUnkownPropListener: Rule.RuleListener = twiceDecoratedNoUnkownProp.create(context);

return mergeRules(ariaPropsListener, noUnkownPropListener);
},
};

0 comments on commit 215332f

Please sign in to comment.