Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 51 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -263,14 +266,57 @@ 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.

```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*,
```
21 changes: 21 additions & 0 deletions src/commands/diff/__snapshots__/diff.test.ts.snap
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
16 changes: 16 additions & 0 deletions src/commands/diff/diff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
13 changes: 13 additions & 0 deletions test-utils/fixtures/configs/readmeExamplesConfig.yml
Original file line number Diff line number Diff line change
@@ -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*\)

10 changes: 10 additions & 0 deletions test-utils/fixtures/diff/readme-examples
Original file line number Diff line number Diff line change
@@ -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)