Skip to content

Commit

Permalink
[Backport 1.x] Trim trailing slashes before checking no-restricted-pa…
Browse files Browse the repository at this point in the history
…th rule (#3052)

* 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 <amoo_miki@yahoo.com>

Signed-off-by: Miki <amoo_miki@yahoo.com>
(cherry picked from commit 8732b1c)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

# Conflicts:
#	CHANGELOG.md

* Update tests in backport PR 3020 with correct paths (#3315)

Signed-off-by: Miki <miki@amazon.com>

Signed-off-by: Miki <miki@amazon.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Anan Zhuang <ananzh@amazon.com>
Co-authored-by: Miki <miki@amazon.com>
  • Loading branch information
4 people authored Jan 25, 2023
1 parent cb0e99b commit 54ffc93
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,22 @@ ruleTester.run('@osd/eslint/no-restricted-paths', rule, {
},
],
},
{
code: 'import b from "files/no_restricted_paths/server/deep/deeper/e.js"',
filename: path.join(__dirname, 'files/no_restricted_paths/server/deep/d.js'),
options: [
{
basePath: __dirname,
zones: [
{
target: 'testfiles/**/server/**/*',
from: 'testfiles/**/server/**/*',
allowSameFolder: true,
},
],
},
],
},

// irrelevant function calls
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,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) {
Expand Down

0 comments on commit 54ffc93

Please sign in to comment.