From 16ab6560f64706bbfeffbdb7c2c71075f5fdc7eb Mon Sep 17 00:00:00 2001 From: yassin-kammoun-sonarsource Date: Mon, 23 Sep 2024 11:58:32 +0200 Subject: [PATCH] Update S6747 (`no-unknown-property`): Enable the rule only on React projects --- .../jsts/file-for-rules/javascript-S6747.json | 8 - .../jsts/it-tools/typescript-S6747.json | 12 - .../jsts/vuetify/typescript-S6747.json | 532 ------------------ .../fixtures/non-react-project/package.json | 3 + .../fixtures/non-react-project/unit.test.ts | 35 ++ .../react-project}/cb.fixture.jsx | 0 .../{ => fixtures/react-project}/cb.test.ts | 11 +- .../S6747/fixtures/react-project/package.json | 5 + packages/jsts/src/rules/S6747/rule.ts | 7 +- 9 files changed, 58 insertions(+), 555 deletions(-) delete mode 100644 its/ruling/src/test/expected/jsts/file-for-rules/javascript-S6747.json delete mode 100644 its/ruling/src/test/expected/jsts/it-tools/typescript-S6747.json delete mode 100644 its/ruling/src/test/expected/jsts/vuetify/typescript-S6747.json create mode 100644 packages/jsts/src/rules/S6747/fixtures/non-react-project/package.json create mode 100644 packages/jsts/src/rules/S6747/fixtures/non-react-project/unit.test.ts rename packages/jsts/src/rules/S6747/{ => fixtures/react-project}/cb.fixture.jsx (100%) rename packages/jsts/src/rules/S6747/{ => fixtures/react-project}/cb.test.ts (78%) create mode 100644 packages/jsts/src/rules/S6747/fixtures/react-project/package.json diff --git a/its/ruling/src/test/expected/jsts/file-for-rules/javascript-S6747.json b/its/ruling/src/test/expected/jsts/file-for-rules/javascript-S6747.json deleted file mode 100644 index 7f17219c5eb..00000000000 --- a/its/ruling/src/test/expected/jsts/file-for-rules/javascript-S6747.json +++ /dev/null @@ -1,8 +0,0 @@ -{ -"file-for-rules:S6840.js": [ -2 -], -"file-for-rules:S6846.js": [ -2 -] -} diff --git a/its/ruling/src/test/expected/jsts/it-tools/typescript-S6747.json b/its/ruling/src/test/expected/jsts/it-tools/typescript-S6747.json deleted file mode 100644 index 19faf5acd88..00000000000 --- a/its/ruling/src/test/expected/jsts/it-tools/typescript-S6747.json +++ /dev/null @@ -1,12 +0,0 @@ -{ -"it-tools:src/tools/json-diff/diff-viewer/diff-viewer.models.tsx": [ -7, -37, -40, -55, -58, -85, -88, -115 -] -} diff --git a/its/ruling/src/test/expected/jsts/vuetify/typescript-S6747.json b/its/ruling/src/test/expected/jsts/vuetify/typescript-S6747.json deleted file mode 100644 index d91141067f1..00000000000 --- a/its/ruling/src/test/expected/jsts/vuetify/typescript-S6747.json +++ /dev/null @@ -1,532 +0,0 @@ -{ -"vuetify:packages/vuetify/cypress/templates/CenteredGrid.tsx": [ -7 -], -"vuetify:packages/vuetify/cypress/templates/generateStories.tsx": [ -27, -33, -113, -117 -], -"vuetify:packages/vuetify/cypress/templates/gridOn.tsx": [ -5, -9 -], -"vuetify:packages/vuetify/src/components/VAlert/VAlert.tsx": [ -170, -189, -198, -213, -229 -], -"vuetify:packages/vuetify/src/components/VApp/VApp.tsx": [ -28, -36 -], -"vuetify:packages/vuetify/src/components/VAutocomplete/VAutocomplete.tsx": [ -42, -43, -44, -365, -385, -388 -], -"vuetify:packages/vuetify/src/components/VBadge/VBadge.tsx": [ -108, -113, -114 -], -"vuetify:packages/vuetify/src/components/VBanner/VBanner.tsx": [ -117, -126 -], -"vuetify:packages/vuetify/src/components/VBanner/VBannerActions.tsx": [ -25 -], -"vuetify:packages/vuetify/src/components/VBottomNavigation/VBottomNavigation.tsx": [ -124 -], -"vuetify:packages/vuetify/src/components/VBreadcrumbs/VBreadcrumbs.tsx": [ -105 -], -"vuetify:packages/vuetify/src/components/VBreadcrumbs/VBreadcrumbsDivider.tsx": [ -13 -], -"vuetify:packages/vuetify/src/components/VBtn/VBtn.tsx": [ -201, -207, -233, -240 -], -"vuetify:packages/vuetify/src/components/VCard/VCard.tsx": [ -161 -], -"vuetify:packages/vuetify/src/components/VCard/VCardActions.tsx": [ -21 -], -"vuetify:packages/vuetify/src/components/VCard/VCardItem.tsx": [ -47, -63, -69, -100 -], -"vuetify:packages/vuetify/src/components/VCarousel/VCarousel.tsx": [ -107 -], -"vuetify:packages/vuetify/src/components/VChip/VChip.tsx": [ -201, -202, -224, -258, -280 -], -"vuetify:packages/vuetify/src/components/VColorPicker/VColorPicker.tsx": [ -146 -], -"vuetify:packages/vuetify/src/components/VColorPicker/VColorPickerCanvas.tsx": [ -182, -184, -185, -194 -], -"vuetify:packages/vuetify/src/components/VColorPicker/VColorPickerEdit.tsx": [ -19, -81 -], -"vuetify:packages/vuetify/src/components/VColorPicker/VColorPickerPreview.tsx": [ -33, -40, -44 -], -"vuetify:packages/vuetify/src/components/VColorPicker/VColorPickerSwatches.tsx": [ -58, -65, -71 -], -"vuetify:packages/vuetify/src/components/VCombobox/VCombobox.tsx": [ -44, -45, -46, -447, -475, -478 -], -"vuetify:packages/vuetify/src/components/VCounter/VCounter.tsx": [ -40, -41 -], -"vuetify:packages/vuetify/src/components/VDialog/VDialog.tsx": [ -120 -], -"vuetify:packages/vuetify/src/components/VDivider/VDivider.tsx": [ -47 -], -"vuetify:packages/vuetify/src/components/VExpansionPanel/VExpansionPanel.tsx": [ -91 -], -"vuetify:packages/vuetify/src/components/VExpansionPanel/VExpansionPanelText.tsx": [ -29, -30, -33 -], -"vuetify:packages/vuetify/src/components/VExpansionPanel/VExpansionPanelTitle.tsx": [ -73, -82, -86, -88, -93 -], -"vuetify:packages/vuetify/src/components/VExpansionPanel/__tests__/VExpansionPanels.spec.cy.tsx": [ -143 -], -"vuetify:packages/vuetify/src/components/VField/VField.tsx": [ -213, -241, -251, -260, -292, -293, -304, -314, -321, -324, -331 -], -"vuetify:packages/vuetify/src/components/VFileInput/VFileInput.tsx": [ -210, -231 -], -"vuetify:packages/vuetify/src/components/VForm/VForm.tsx": [ -57, -58 -], -"vuetify:packages/vuetify/src/components/VHover/__tests__/VHover.spec.cy.tsx": [ -12, -29, -47 -], -"vuetify:packages/vuetify/src/components/VIcon/__tests__/VIcon.spec.cy.tsx": [ -87 -], -"vuetify:packages/vuetify/src/components/VImg/VImg.tsx": [ -207, -209, -225, -238, -252, -264, -273 -], -"vuetify:packages/vuetify/src/components/VInput/VInput.tsx": [ -124, -132, -145, -151, -164 -], -"vuetify:packages/vuetify/src/components/VInput/__tests__/VInput.spec.cy.tsx": [ -17, -19 -], -"vuetify:packages/vuetify/src/components/VLabel/VLabel.tsx": [ -23 -], -"vuetify:packages/vuetify/src/components/VLayout/VLayout.tsx": [ -19 -], -"vuetify:packages/vuetify/src/components/VLayout/VLayoutItem.tsx": [ -43 -], -"vuetify:packages/vuetify/src/components/VList/VListGroup.tsx": [ -112, -112 -], -"vuetify:packages/vuetify/src/components/VList/VListItem.tsx": [ -215, -247, -264 -], -"vuetify:packages/vuetify/src/components/VList/VListSubheader.tsx": [ -40 -], -"vuetify:packages/vuetify/src/components/VList/__tests__/VList.spec.cy.tsx": [ -24, -46 -], -"vuetify:packages/vuetify/src/components/VList/__tests__/VListGroup.spec.cy.tsx": [ -16, -32 -], -"vuetify:packages/vuetify/src/components/VList/__tests__/VListItem.spec.cy.tsx": [ -15 -], -"vuetify:packages/vuetify/src/components/VLocaleProvider/VLocaleProvider.tsx": [ -28 -], -"vuetify:packages/vuetify/src/components/VMain/VMain.tsx": [ -38 -], -"vuetify:packages/vuetify/src/components/VMessages/VMessages.tsx": [ -65 -], -"vuetify:packages/vuetify/src/components/VNavigationDrawer/VNavigationDrawer.tsx": [ -238, -247, -252, -257, -266 -], -"vuetify:packages/vuetify/src/components/VOverlay/VOverlay.tsx": [ -64, -253, -282, -283, -284 -], -"vuetify:packages/vuetify/src/components/VPagination/VPagination.tsx": [ -319, -321, -328, -337, -353, -364 -], -"vuetify:packages/vuetify/src/components/VProgressCircular/VProgressCircular.tsx": [ -107, -116, -117, -118, -122, -127, -128, -129, -134 -], -"vuetify:packages/vuetify/src/components/VProgressLinear/VProgressLinear.tsx": [ -132, -149, -165, -175, -179, -192 -], -"vuetify:packages/vuetify/src/components/VRangeSlider/VRangeSlider.tsx": [ -149, -150, -151, -157, -158, -166, -167 -], -"vuetify:packages/vuetify/src/components/VRating/VRating.tsx": [ -182, -183, -187, -188, -189, -190, -192, -208, -218, -250, -257 -], -"vuetify:packages/vuetify/src/components/VRating/__tests__/VRating.spec.cy.tsx": [ -101, -115 -], -"vuetify:packages/vuetify/src/components/VResponsive/VResponsive.tsx": [ -43, -44, -49 -], -"vuetify:packages/vuetify/src/components/VSelect/VSelect.tsx": [ -319, -339, -342 -], -"vuetify:packages/vuetify/src/components/VSelectionControl/VSelectionControl.tsx": [ -200, -215, -224, -227 -], -"vuetify:packages/vuetify/src/components/VSelectionControlGroup/VSelectionControlGroup.tsx": [ -112 -], -"vuetify:packages/vuetify/src/components/VSlideGroup/VSlideGroup.tsx": [ -357, -374, -379, -381, -382, -383, -384, -385, -386, -395 -], -"vuetify:packages/vuetify/src/components/VSlider/VSlider.tsx": [ -121, -122, -123, -129, -130 -], -"vuetify:packages/vuetify/src/components/VSlider/VSliderThumb.tsx": [ -124, -136, -142, -145, -155, -160, -164, -165, -168 -], -"vuetify:packages/vuetify/src/components/VSlider/VSliderTrack.tsx": [ -96, -108, -121, -132, -145, -157 -], -"vuetify:packages/vuetify/src/components/VSnackbar/VSnackbar.tsx": [ -131, -148 -], -"vuetify:packages/vuetify/src/components/VSwitch/VSwitch.tsx": [ -118, -121 -], -"vuetify:packages/vuetify/src/components/VTable/VTable.tsx": [ -57 -], -"vuetify:packages/vuetify/src/components/VTabs/VTab.tsx": [ -153 -], -"vuetify:packages/vuetify/src/components/VTabs/__tests__/VTabs.spec.cy.tsx": [ -134 -], -"vuetify:packages/vuetify/src/components/VTextField/VTextField.tsx": [ -208, -211, -212, -228, -235, -244 -], -"vuetify:packages/vuetify/src/components/VTextField/__tests__/VTextField.spec.cy.tsx": [ -17, -19 -], -"vuetify:packages/vuetify/src/components/VTextarea/VTextarea.tsx": [ -251, -258, -261, -264, -265, -278, -282, -284, -290 -], -"vuetify:packages/vuetify/src/components/VTimeline/VTimelineDivider.tsx": [ -52, -60, -70, -79, -92 -], -"vuetify:packages/vuetify/src/components/VTimeline/VTimelineItem.tsx": [ -63, -75, -95 -], -"vuetify:packages/vuetify/src/components/VTimeline/__tests__/VTimeline.spec.cy.tsx": [ -30, -52, -89, -90, -100, -101, -126, -127, -137, -138, -161, -162, -172, -173, -200, -222, -259, -260, -270, -271, -296, -297, -307, -308 -], -"vuetify:packages/vuetify/src/components/VToolbar/VToolbar.tsx": [ -134, -156, -160, -174, -191 -], -"vuetify:packages/vuetify/src/components/VToolbar/VToolbarItems.tsx": [ -24 -], -"vuetify:packages/vuetify/src/components/VToolbar/VToolbarTitle.tsx": [ -33 -], -"vuetify:packages/vuetify/src/components/VWindow/VWindow.tsx": [ -227, -235 -], -"vuetify:packages/vuetify/src/components/VWindow/VWindowItem.tsx": [ -127, -131 -], -"vuetify:packages/vuetify/src/components/VWindow/__tests__/VWindow.spec.cy.tsx": [ -10, -15, -29, -34, -39, -63, -68, -98, -103, -133, -138, -143, -169, -174, -202, -207, -223, -228, -233, -251, -256 -], -"vuetify:packages/vuetify/src/composables/__tests__/forwardRefs.spec.tsx": [ -13, -28, -45, -62 -], -"vuetify:packages/vuetify/src/composables/icons.tsx": [ -117 -], -"vuetify:packages/vuetify/src/composables/loader.tsx": [ -48 -], -"vuetify:packages/vuetify/src/composables/variant.tsx": [ -31, -33 -], -"vuetify:packages/vuetify/src/labs/VDataTable/VDataTableFooter.tsx": [ -90, -93, -104, -109, -125 -], -"vuetify:packages/vuetify/src/labs/VDataTable/VDataTableGroupHeaderRow.tsx": [ -45 -], -"vuetify:packages/vuetify/src/labs/VDataTable/VDataTableHeaders.tsx": [ -121, -133, -161, -162 -], -"vuetify:packages/vuetify/src/labs/VDataTable/VDataTableRow.tsx": [ -32 -], -"vuetify:packages/vuetify/src/labs/VDataTable/VDataTableServer.tsx": [ -121, -132 -], -"vuetify:packages/vuetify/src/labs/VDataTable/VDataTableVirtual.tsx": [ -129, -144, -154 -], -"vuetify:packages/vuetify/src/labs/VVirtualScroll/VVirtualScroll.tsx": [ -164, -169 -], -"vuetify:packages/vuetify/src/labs/VVirtualScroll/VVirtualScrollItem.tsx": [ -40 -], -"vuetify:templates/component.tsx": [ -27 -] -} diff --git a/packages/jsts/src/rules/S6747/fixtures/non-react-project/package.json b/packages/jsts/src/rules/S6747/fixtures/non-react-project/package.json new file mode 100644 index 00000000000..18a1e415e56 --- /dev/null +++ b/packages/jsts/src/rules/S6747/fixtures/non-react-project/package.json @@ -0,0 +1,3 @@ +{ + "dependencies": {} +} diff --git a/packages/jsts/src/rules/S6747/fixtures/non-react-project/unit.test.ts b/packages/jsts/src/rules/S6747/fixtures/non-react-project/unit.test.ts new file mode 100644 index 00000000000..e8c7198e726 --- /dev/null +++ b/packages/jsts/src/rules/S6747/fixtures/non-react-project/unit.test.ts @@ -0,0 +1,35 @@ +/* + * SonarQube JavaScript Plugin + * Copyright (C) 2011-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. + */ +import { RuleTester } from 'eslint'; +import { rule } from '../../'; +import { clearPackageJsons, loadPackageJsons } from '../../../helpers'; + +clearPackageJsons(); +loadPackageJsons(__dirname, []); + +const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2018, sourceType: 'module', ecmaFeatures: { jsx: true } } }); +ruleTester.run('S6747 turns into a noop on non-React projects', rule, { + valid: [ + { + code: '
;', + }, + ], + invalid: [], +}); diff --git a/packages/jsts/src/rules/S6747/cb.fixture.jsx b/packages/jsts/src/rules/S6747/fixtures/react-project/cb.fixture.jsx similarity index 100% rename from packages/jsts/src/rules/S6747/cb.fixture.jsx rename to packages/jsts/src/rules/S6747/fixtures/react-project/cb.fixture.jsx diff --git a/packages/jsts/src/rules/S6747/cb.test.ts b/packages/jsts/src/rules/S6747/fixtures/react-project/cb.test.ts similarity index 78% rename from packages/jsts/src/rules/S6747/cb.test.ts rename to packages/jsts/src/rules/S6747/fixtures/react-project/cb.test.ts index c040a10c2aa..a2c879be94a 100644 --- a/packages/jsts/src/rules/S6747/cb.test.ts +++ b/packages/jsts/src/rules/S6747/fixtures/react-project/cb.test.ts @@ -17,12 +17,19 @@ * 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 '../../../tests/tools'; -import { rule } from './'; +import { check } from '../../../../../tests/tools'; +import { clearPackageJsons, loadPackageJsons } from '../../../helpers'; +import { rule } from '../../'; import path from 'path'; const sonarId = path.basename(__dirname); describe('Rule S6747', () => { + beforeEach(() => { + loadPackageJsons(__dirname, []); + }); + afterAll(() => { + clearPackageJsons(); + }); check(sonarId, rule, __dirname); }); diff --git a/packages/jsts/src/rules/S6747/fixtures/react-project/package.json b/packages/jsts/src/rules/S6747/fixtures/react-project/package.json new file mode 100644 index 00000000000..ffbff46a0f6 --- /dev/null +++ b/packages/jsts/src/rules/S6747/fixtures/react-project/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "react": "1.2.3" + } +} diff --git a/packages/jsts/src/rules/S6747/rule.ts b/packages/jsts/src/rules/S6747/rule.ts index 759a7b3fba4..b82b754e3cf 100644 --- a/packages/jsts/src/rules/S6747/rule.ts +++ b/packages/jsts/src/rules/S6747/rule.ts @@ -22,7 +22,7 @@ import { Rule } from 'eslint'; import { rules as reactRules } from 'eslint-plugin-react'; import { rules as jsxA11yRules } from 'eslint-plugin-jsx-a11y'; -import { generateMeta, interceptReport, mergeRules } from '../helpers'; +import { generateMeta, getDependencies, interceptReport, mergeRules } from '../helpers'; import { decorate } from './decorator'; import { TSESTree } from '@typescript-eslint/utils'; import { meta } from './meta'; @@ -88,6 +88,11 @@ export const rule: Rule.RuleModule = { }), create(context: Rule.RuleContext) { + const dependencies = getDependencies(context.filename); + if (!dependencies.has('react')) { + return {}; + } + const ariaPropsListener: Rule.RuleListener = decoratedAriaPropsRule.create(context); const noUnknownPropListener: Rule.RuleListener = twiceDecoratedNoUnknownProp.create(context);