From 9b925e5931796a04867eb9c524e67fae9da2671a Mon Sep 17 00:00:00 2001 From: Grégoire Aubert Date: Tue, 20 Feb 2024 09:34:09 +0100 Subject: SONAR-19032 MIUI cleanup part 1 (#10627) Co-authored-by: David Cho-Lerat Co-authored-by: Jeremy Davis --- ...onditional-rendering-of-deferredspinner-test.js | 72 ---------------------- .../no-conditional-rendering-of-spinner-test.js | 68 ++++++++++++++++++++ server/sonar-web/eslint-local-rules/index.js | 2 +- .../no-conditional-rendering-of-deferredspinner.js | 59 ------------------ .../no-conditional-rendering-of-spinner.js | 59 ++++++++++++++++++ 5 files changed, 128 insertions(+), 132 deletions(-) delete mode 100644 server/sonar-web/eslint-local-rules/__tests__/no-conditional-rendering-of-deferredspinner-test.js create mode 100644 server/sonar-web/eslint-local-rules/__tests__/no-conditional-rendering-of-spinner-test.js delete mode 100644 server/sonar-web/eslint-local-rules/no-conditional-rendering-of-deferredspinner.js create mode 100644 server/sonar-web/eslint-local-rules/no-conditional-rendering-of-spinner.js (limited to 'server/sonar-web/eslint-local-rules') diff --git a/server/sonar-web/eslint-local-rules/__tests__/no-conditional-rendering-of-deferredspinner-test.js b/server/sonar-web/eslint-local-rules/__tests__/no-conditional-rendering-of-deferredspinner-test.js deleted file mode 100644 index 99a34407909..00000000000 --- a/server/sonar-web/eslint-local-rules/__tests__/no-conditional-rendering-of-deferredspinner-test.js +++ /dev/null @@ -1,72 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2024 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. - */ -const { RuleTester } = require('eslint'); -const noConditionalRenderingOfDeferredSpinner = require('../no-conditional-rendering-of-deferredspinner'); - -const ruleTester = new RuleTester({ - parser: require.resolve('@typescript-eslint/parser'), - parserOptions: { - ecmaFeatures: { - jsx: true, - }, - }, -}); - -ruleTester.run( - 'no-conditional-rendering-of-deferredspinner', - noConditionalRenderingOfDeferredSpinner, - { - valid: [ - { - code: `function MyCompontent({ loading }) { - return <> - - -}`, - }, - ], - invalid: [ - { - code: `function MyCompontent({ loading }) { - return <> - {loading && } - -}`, - errors: [{ messageId: 'noConditionalRenderingOfDeferredSpinner' }], - }, - { - code: `function MyComponent({ loading }) { - return <> - {loading ? :
} - -}`, - errors: [{ messageId: 'noConditionalRenderingOfDeferredSpinner' }], - }, - { - code: `function MyCompontent({ loaded }) { - return <> - {loaded ?
: } - -}`, - errors: [{ messageId: 'noConditionalRenderingOfDeferredSpinner' }], - }, - ], - } -); diff --git a/server/sonar-web/eslint-local-rules/__tests__/no-conditional-rendering-of-spinner-test.js b/server/sonar-web/eslint-local-rules/__tests__/no-conditional-rendering-of-spinner-test.js new file mode 100644 index 00000000000..bcd456e9f98 --- /dev/null +++ b/server/sonar-web/eslint-local-rules/__tests__/no-conditional-rendering-of-spinner-test.js @@ -0,0 +1,68 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 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. + */ +const { RuleTester } = require('eslint'); +const noConditionalRenderingOfSpinner = require('../no-conditional-rendering-of-spinner'); + +const ruleTester = new RuleTester({ + parser: require.resolve('@typescript-eslint/parser'), + parserOptions: { + ecmaFeatures: { + jsx: true, + }, + }, +}); + +ruleTester.run('no-conditional-rendering-of-spinner', noConditionalRenderingOfSpinner, { + valid: [ + { + code: `function MyCompontent({ loading }) { + return <> + + +}`, + }, + ], + invalid: [ + { + code: `function MyCompontent({ loading }) { + return <> + {loading && } + +}`, + errors: [{ messageId: 'noConditionalRenderingOfSpinner' }], + }, + { + code: `function MyComponent({ loading }) { + return <> + {loading ? :
} + +}`, + errors: [{ messageId: 'noConditionalRenderingOfSpinner' }], + }, + { + code: `function MyCompontent({ loaded }) { + return <> + {loaded ?
: } + +}`, + errors: [{ messageId: 'noConditionalRenderingOfSpinner' }], + }, + ], +}); diff --git a/server/sonar-web/eslint-local-rules/index.js b/server/sonar-web/eslint-local-rules/index.js index 7d9345d3021..e80671fb5a5 100644 --- a/server/sonar-web/eslint-local-rules/index.js +++ b/server/sonar-web/eslint-local-rules/index.js @@ -20,7 +20,7 @@ module.exports = { 'use-jest-mocked': require('./use-jest-mocked'), 'convert-class-to-function-component': require('./convert-class-to-function-component'), - 'no-conditional-rendering-of-deferredspinner': require('./no-conditional-rendering-of-deferredspinner'), + 'no-conditional-rendering-of-spinner': require('./no-conditional-rendering-of-spinner'), 'use-visibility-enum': require('./use-visibility-enum'), 'use-componentqualifier-enum': require('./use-componentqualifier-enum'), 'use-metrickey-enum': require('./use-metrickey-enum'), diff --git a/server/sonar-web/eslint-local-rules/no-conditional-rendering-of-deferredspinner.js b/server/sonar-web/eslint-local-rules/no-conditional-rendering-of-deferredspinner.js deleted file mode 100644 index 89540719d75..00000000000 --- a/server/sonar-web/eslint-local-rules/no-conditional-rendering-of-deferredspinner.js +++ /dev/null @@ -1,59 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2024 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. - */ -module.exports = { - meta: { - messages: { - noConditionalRenderingOfDeferredSpinner: - 'For accessibility reasons, you should not conditionally render a . Always render it, and pass a loading prop instead.', - }, - }, - create(context) { - return { - JSXExpressionContainer(node) { - switch (node.expression.type) { - case 'LogicalExpression': - const { right } = node.expression; - if (isDeferredSpinnerComponent(right)) { - context.report({ node, messageId: 'noConditionalRenderingOfDeferredSpinner' }); - } - break; - - case 'ConditionalExpression': - const { consequent, alternate } = node.expression; - if (isDeferredSpinnerComponent(consequent)) { - context.report({ node, messageId: 'noConditionalRenderingOfDeferredSpinner' }); - } - if (isDeferredSpinnerComponent(alternate)) { - context.report({ node, messageId: 'noConditionalRenderingOfDeferredSpinner' }); - } - break; - } - }, - }; - }, -}; - -function isDeferredSpinnerComponent(element) { - return ( - element.type === 'JSXElement' && - element.openingElement && - element.openingElement.name.name === 'Spinner' - ); -} diff --git a/server/sonar-web/eslint-local-rules/no-conditional-rendering-of-spinner.js b/server/sonar-web/eslint-local-rules/no-conditional-rendering-of-spinner.js new file mode 100644 index 00000000000..06ed0a9fa55 --- /dev/null +++ b/server/sonar-web/eslint-local-rules/no-conditional-rendering-of-spinner.js @@ -0,0 +1,59 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 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. + */ +module.exports = { + meta: { + messages: { + noConditionalRenderingOfSpinner: + 'For accessibility reasons, you should not conditionally render a . Always render it, and pass a loading prop instead.', + }, + }, + create(context) { + return { + JSXExpressionContainer(node) { + switch (node.expression.type) { + case 'LogicalExpression': + const { right } = node.expression; + if (isSpinnerComponent(right)) { + context.report({ node, messageId: 'noConditionalRenderingOfSpinner' }); + } + break; + + case 'ConditionalExpression': + const { consequent, alternate } = node.expression; + if (isSpinnerComponent(consequent)) { + context.report({ node, messageId: 'noConditionalRenderingOfSpinner' }); + } + if (isSpinnerComponent(alternate)) { + context.report({ node, messageId: 'noConditionalRenderingOfSpinner' }); + } + break; + } + }, + }; + }, +}; + +function isSpinnerComponent(element) { + return ( + element.type === 'JSXElement' && + element.openingElement && + element.openingElement.name.name === 'Spinner' + ); +} -- cgit v1.2.3