Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve S1077 (alt-text): Report on the name of the opening element #4407

Merged
merged 1 commit into from Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/jsts/src/rules/S1077/cb.fixture.tsx
@@ -0,0 +1,2 @@
<img className={styles.Image} src={snapshot.imageSource} style={{height: safeHeight, width: safeWidth}}/>; // Noncompliant
// ^^^
28 changes: 28 additions & 0 deletions packages/jsts/src/rules/S1077/cb.test.ts
@@ -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 S1077', () => {
check(sonarId, rule, __dirname);
});
33 changes: 33 additions & 0 deletions packages/jsts/src/rules/S1077/decorator.ts
@@ -0,0 +1,33 @@
/*
* 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/S1077/javascript

import { Rule } from 'eslint';
import { Node } from 'estree';
import { TSESTree } from '@typescript-eslint/experimental-utils';
import { interceptReport } from '../helpers';

export function decorate(rule: Rule.RuleModule): Rule.RuleModule {
return interceptReport(rule, (context, descriptor) => {
const { node } = descriptor as unknown as { node: TSESTree.JSXOpeningElement };
const name = node.name as unknown as Node;
context.report({ ...descriptor, node: name });
});
}
23 changes: 23 additions & 0 deletions packages/jsts/src/rules/S1077/index.ts
@@ -0,0 +1,23 @@
/*
* 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 { rules as jsxA11yRules } from 'eslint-plugin-jsx-a11y';
import { decorate } from './decorator';

export const rule = decorate(jsxA11yRules['alt-text']);
6 changes: 4 additions & 2 deletions packages/jsts/src/rules/index.ts
Expand Up @@ -20,8 +20,9 @@
import { Rule } from 'eslint';

import { rule as S2376 } from './S2376'; // accessor-pairs
import { rule as S6844 } from './S6844'; // anchor-is-valid
import { rule as S1077 } from './S1077'; // alt-text
import { rule as S6827 } from './S6827'; // anchor-has-content
import { rule as S6844 } from './S6844'; // anchor-is-valid
import { rule as S5850 } from './S5850'; // anchor-precedence
import { rule as S3782 } from './S3782'; // argument-type
import { rule as S2234 } from './S2234'; // arguments-order
Expand Down Expand Up @@ -303,8 +304,9 @@ import { rule as S4817 } from './S4817'; // xpath
const rules: { [key: string]: Rule.RuleModule } = {};

rules['accessor-pairs'] = S2376;
rules['anchor-is-valid'] = S6844;
rules['alt-text'] = S1077;
rules['anchor-has-content'] = S6827;
rules['anchor-is-valid'] = S6844;
rules['anchor-precedence'] = S5850;
rules['argument-type'] = S3782;
rules['arguments-order'] = S2234;
Expand Down