From d3ed3eb8de9cb96bb22e519bc1dd92cf28453f1c Mon Sep 17 00:00:00 2001 From: Miki Date: Thu, 8 Dec 2022 14:21:37 -0800 Subject: [PATCH] Fixes `no-restricted-path` false-positives when `allowSameFolder` is true (#3020) `no-restricted-paths` compares source files and import statements, and their membership in restricted zones. However, when `allowSameFolder` is true, it failed to remove a trailing slash before validation which results in a false-positive. Signed-off-by: Miki Signed-off-by: Miki Signed-off-by: David Sinclair --- CHANGELOG.md | 1 + .../rules/no_restricted_paths.js | 2 +- .../rules/no_restricted_paths.test.js | 16 ++++++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 454816230c89..8ff128235ef1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -107,6 +107,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - [BWC Tests] Add BWC tests for 2.5.0 ([#2890](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2890)) - Fix incorrect validation of time values in JUnit Reporter ([#2965](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2965)) - Make tests covering plugin installation on cluster snapshots work across platforms ([#2994](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2994)) +- Correct the linting logic for `no-restricted-path` to ignore trailing slashes ([#3020](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3020)) ## [2.x] diff --git a/packages/osd-eslint-plugin-eslint/rules/no_restricted_paths.js b/packages/osd-eslint-plugin-eslint/rules/no_restricted_paths.js index ecd14f7317aa..52b1b27ba360 100644 --- a/packages/osd-eslint-plugin-eslint/rules/no_restricted_paths.js +++ b/packages/osd-eslint-plugin-eslint/rules/no_restricted_paths.js @@ -58,7 +58,7 @@ function traverseToTopFolder(src, pattern) { const srcIdx = src.lastIndexOf(path.sep); src = src.slice(0, srcIdx); } - return src.replace(/\\/g, '/'); + return src.replace(/\\/g, '/').replace(/\/$/, ''); } function isSameFolderOrDescendent(src, imported, pattern) { diff --git a/packages/osd-eslint-plugin-eslint/rules/no_restricted_paths.test.js b/packages/osd-eslint-plugin-eslint/rules/no_restricted_paths.test.js index 06e7a9c39414..96251e8fe55c 100644 --- a/packages/osd-eslint-plugin-eslint/rules/no_restricted_paths.test.js +++ b/packages/osd-eslint-plugin-eslint/rules/no_restricted_paths.test.js @@ -97,6 +97,22 @@ ruleTester.run('@osd/eslint/no-restricted-paths', rule, { }, ], }, + { + code: 'import b from "testfiles/no_restricted_paths/server/deep/deeper/e.js"', + filename: path.join(__dirname, 'testfiles/no_restricted_paths/server/deep/d.js'), + options: [ + { + basePath: __dirname, + zones: [ + { + target: 'testfiles/**/server/**/*', + from: 'testfiles/**/server/**/*', + allowSameFolder: true, + }, + ], + }, + ], + }, // irrelevant function calls {