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..777738f2e 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:L0 +" +`; + 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..ce9d832fd --- /dev/null +++ b/test-utils/fixtures/diff/readme-examples @@ -0,0 +1,10 @@ +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 +@@ -0,0 +1,3 @@ ++const flag1 = getFeatureFlag('my-variable', defaultValue) ++const flag2 = isFeatureEnabled('enable-feature') +-const oldFlag = getFeatureFlag('removed-variable', defaultValue) +