From d928a15321a359ddae71af98684901fc17dbc408 Mon Sep 17 00:00:00 2001 From: Gabriella Gerges Date: Thu, 6 Nov 2025 17:11:23 -0400 Subject: [PATCH 1/2] fix: update match pattern docs. Add ReadMe associated pattern test. fix: revert unnecessary readme changes --- README.md | 56 +++++++++++++++++-- .../diff/__snapshots__/diff.test.ts.snap | 21 +++++++ src/commands/diff/diff.test.ts | 16 ++++++ .../fixtures/configs/readmeExamplesConfig.yml | 13 +++++ test-utils/fixtures/diff/readme-examples | 11 ++++ 5 files changed, 112 insertions(+), 5 deletions(-) create mode 100644 test-utils/fixtures/configs/readmeExamplesConfig.yml create mode 100644 test-utils/fixtures/diff/readme-examples diff --git a/README.md b/README.md index 9805b06c8..5eda9c7da 100644 --- a/README.md +++ b/README.md @@ -239,12 +239,15 @@ codeInsights: - "dist/*" ``` -### Match Patterns +### Match Patterns and Aliases When identifying variable usages in the code, the CLI will identify DevCycle SDK methods by default. To capture -other usages you may define match patterns. Match patterns are defined by file extension, and each pattern should -contain exactly one capture group which matches the key of the variable. Make sure the captured value contains the -entire key parameter (including quotes, if applicable). +other usages you may define match patterns. Match patterns are defined by file extension. + +:::note +Each pattern must include exactly one capture group for the variable key. Capture the entire key value +(including surrounding quotes if you choose the “with quotes” pattern). +::: Match patterns can be defined in the configuration file, for example: @@ -263,10 +266,34 @@ codeInsights: - customVariableGetter\(\s*["']([^"']*)["'] ``` +#### Capturing with or without quotes + +Match patterns can capture variable keys with or without quotes, which affects whether aliases are needed: + +With quotes: + +```yml +# Pattern captures the key including surrounding quotes +- dvcClient\.variable\(\s*(["'][^"']*["'])\s*, +``` + +- Matches: `dvcClient.variable('my-variable', default)` +- Captures: `'my-variable'` (with quotes) + +Without quotes (aliases or generated constants required): + +```yml +# Pattern strips quotes, capturing only the content +- dvcClient\.variable\(\s*["']([^"']*)["'] +``` + +- Matches: `dvcClient.variable('my-variable', default)` +- Captures: `my-variable` + Match patterns can also be passed directly to relevant commands using the `--match-pattern` flag: ```bash -dvc usages --match-pattern ts="customVariableGetter\(\s*[\"']([^\"']*)[\"']" js="customVariableGetter\(\s*[\"']([^\"']*)[\"']" +dvc usages --match-pattern ts="customVariableGetter\(\s*["']([^"']*)["']" js="customVariableGetter\(\s*["']([^"']*)["']" ``` When testing your regex the `--show-regex` flag can be helpful. This will print all patterns used to find matches in your codebase. @@ -274,3 +301,22 @@ When testing your regex the `--show-regex` flag can be helpful. This will print ```bash dvc usages --show-regex ``` + +#### Custom Wrapper Functions + +If you use wrapper functions around the SDK, add patterns for them. + +**Example: Custom wrapper functions** + +```yml +codeInsights: + matchPatterns: + ts: + # Matches: getFeatureFlag('my-variable', defaultValue) + - getFeatureFlag\(\s*([^,)]*)\s*, + # Matches: isFeatureEnabled('my-variable') + - isFeatureEnabled\(\s*([^,)]*)\s*\) + tsx: + # Matches: useFeatureFlag('my-variable', defaultValue) + - useFeatureFlag\(\s*([^,)]*)\s*, +``` diff --git a/src/commands/diff/__snapshots__/diff.test.ts.snap b/src/commands/diff/__snapshots__/diff.test.ts.snap index 98ee8058d..276b74c90 100644 --- a/src/commands/diff/__snapshots__/diff.test.ts.snap +++ b/src/commands/diff/__snapshots__/diff.test.ts.snap @@ -1,5 +1,26 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html +exports[`diff > correctly matches using custom patterns from README examples 1`] = ` +" +DevCycle Variable Changes: + +🟢 2 Variables Added +🔴 1 Variable Removed + +🟢 Added + + 1. my-variable + Location: test-utils/fixtures/diff/sampleDiff.ts:L1 + 2. enable-feature + Location: test-utils/fixtures/diff/sampleDiff.ts:L2 + +🔴 Removed + + 1. removed-variable + Location: test-utils/fixtures/diff/sampleDiff.ts:L1 +" +`; + exports[`diff > enriches output with API data 1`] = ` " DevCycle Variable Changes: diff --git a/src/commands/diff/diff.test.ts b/src/commands/diff/diff.test.ts index b2343aff2..6238a28b1 100644 --- a/src/commands/diff/diff.test.ts +++ b/src/commands/diff/diff.test.ts @@ -292,6 +292,22 @@ describe('diff', () => { .it('identifies aliased variables specified in config file', (ctx) => { expect(ctx.stdout).toMatchSnapshot() }) + + test.stdout() + .command([ + 'diff', + '--file', + 'test-utils/fixtures/diff/readme-examples', + '--no-api', + '--repo-config-path', + './test-utils/fixtures/configs/readmeExamplesConfig.yml', + ]) + .it( + 'correctly matches using custom patterns from README examples', + (ctx) => { + expect(ctx.stdout).toMatchSnapshot() + }, + ) test.stdout() .command([ 'diff', diff --git a/test-utils/fixtures/configs/readmeExamplesConfig.yml b/test-utils/fixtures/configs/readmeExamplesConfig.yml new file mode 100644 index 000000000..ef85d52e2 --- /dev/null +++ b/test-utils/fixtures/configs/readmeExamplesConfig.yml @@ -0,0 +1,13 @@ +project: "test-project" +org: + id: "org_test" + name: "test-org" + +codeInsights: + matchPatterns: + ts: + # Matches: getFeatureFlag('my-variable', defaultValue) + - getFeatureFlag\(\s*([^,)]*)\s*, + # Matches: isFeatureEnabled('my-variable') + - isFeatureEnabled\(\s*([^,)]*)\s*\) + diff --git a/test-utils/fixtures/diff/readme-examples b/test-utils/fixtures/diff/readme-examples new file mode 100644 index 000000000..14d99a285 --- /dev/null +++ b/test-utils/fixtures/diff/readme-examples @@ -0,0 +1,11 @@ +diff --git a/test-utils/fixtures/diff/sampleDiff.ts b/test-utils/fixtures/diff/sampleDiff.ts +new file mode 100644 +index 00000000..ed8ee4ab +--- /dev/null ++++ b/test-utils/fixtures/diff/sampleDiff.ts + +@@ -1,1 +1,21 @@ ++const flag1 = getFeatureFlag('my-variable', defaultValue) ++const flag2 = isFeatureEnabled('enable-feature') +-const oldFlag = getFeatureFlag('removed-variable', defaultValue) + From 03de8f240731b132b8d5bf407ed94eca4defccd1 Mon Sep 17 00:00:00 2001 From: Gabriella Gerges Date: Thu, 6 Nov 2025 17:24:42 -0400 Subject: [PATCH 2/2] fix: update diff output --- src/commands/diff/__snapshots__/diff.test.ts.snap | 2 +- test-utils/fixtures/diff/readme-examples | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/commands/diff/__snapshots__/diff.test.ts.snap b/src/commands/diff/__snapshots__/diff.test.ts.snap index 276b74c90..777738f2e 100644 --- a/src/commands/diff/__snapshots__/diff.test.ts.snap +++ b/src/commands/diff/__snapshots__/diff.test.ts.snap @@ -17,7 +17,7 @@ DevCycle Variable Changes: 🔴 Removed 1. removed-variable - Location: test-utils/fixtures/diff/sampleDiff.ts:L1 + Location: test-utils/fixtures/diff/sampleDiff.ts:L0 " `; diff --git a/test-utils/fixtures/diff/readme-examples b/test-utils/fixtures/diff/readme-examples index 14d99a285..ce9d832fd 100644 --- a/test-utils/fixtures/diff/readme-examples +++ b/test-utils/fixtures/diff/readme-examples @@ -3,8 +3,7 @@ new file mode 100644 index 00000000..ed8ee4ab --- /dev/null +++ b/test-utils/fixtures/diff/sampleDiff.ts - -@@ -1,1 +1,21 @@ +@@ -0,0 +1,3 @@ +const flag1 = getFeatureFlag('my-variable', defaultValue) +const flag2 = isFeatureEnabled('enable-feature') -const oldFlag = getFeatureFlag('removed-variable', defaultValue)