From 306c217729933439cf2fb77e248f21a1cb75624a Mon Sep 17 00:00:00 2001 From: Dante Date: Wed, 23 Sep 2026 01:37:51 +0800 Subject: [PATCH] fix: scan piped foreach grouping blocks --- scripts/lib/powershell-destructive-command.js | 1 + tests/lib/powershell-destructive-command.test.js | 3 +++ 2 files changed, 4 insertions(+) diff --git a/scripts/lib/powershell-destructive-command.js b/scripts/lib/powershell-destructive-command.js index 8fad906a5..c5b90d048 100644 --- a/scripts/lib/powershell-destructive-command.js +++ b/scripts/lib/powershell-destructive-command.js @@ -428,6 +428,7 @@ function invokesContainerResult(prefix, options = {}) { const pipelineStart = clause.lastIndexOf('|'); const pipelineCommand = clause.slice(pipelineStart + 1).trim(); const isForeachLoopHeader = Boolean(options.groupingExpression) && + pipelineStart === -1 && /^foreach$/i.test(pipelineCommand); return /(?:^|\s)(?:&|\.)\s*$/.test(clause) || /\.\s*(?:foreach|where)\s*$/i.test(clause) || diff --git a/tests/lib/powershell-destructive-command.test.js b/tests/lib/powershell-destructive-command.test.js index cd023a3c1..e4f7210ba 100644 --- a/tests/lib/powershell-destructive-command.test.js +++ b/tests/lib/powershell-destructive-command.test.js @@ -488,6 +488,9 @@ test('distinguishes foreach statements from the pipeline alias', () => { expectRules('1 | foreach { Remove-Item -Force C:/tmp/demo }', [ RULES.REMOVE_FORCE, ]); + expectRules('1 | foreach ({ Remove-Item -Force C:/tmp/demo })', [ + RULES.REMOVE_FORCE, + ]); }); test('classifies invoked static script-block variables but leaves assignments inert', () => {