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
21 changes: 21 additions & 0 deletions src/commands/diff/diff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,27 @@ describe('diff', () => {
.it('identifies aliased variables specified in config file', (ctx) => {
expect(ctx.stdout).toMatchSnapshot()
})

test.stdout()
.command([
'diff',
'--file',
'test-utils/fixtures/diff/auto-alias-transform',
'--no-api',
'--repo-config-path',
'./test-utils/fixtures/configs/autoAliasConfig.yml',
])
.it(
'resolves aliases from generated types file using key transformation',
(ctx) => {
// Should NOT show "could not be identified" warnings
expect(ctx.stdout).not.toContain('could not be identified')
// Should identify the variables
expect(ctx.stdout).toContain('my-test-variable')
expect(ctx.stdout).toContain('enable-feature')
expect(ctx.stdout).toContain('removed-variable')
},
)
test.stdout()
.command([
'diff',
Expand Down
7 changes: 6 additions & 1 deletion src/commands/diff/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import VarAliasFlag, { getVariableAliases } from '../../flags/var-alias'
import ShowRegexFlag, { showRegex } from '../../flags/show-regex'
import { Variable } from '../../api/schemas'
import { FileFilters } from '../../utils/FileFilters'
import { keyToConstant } from '../../utils/keyToConstant'

const EMOJI = {
add: '🟢',
Expand Down Expand Up @@ -162,7 +163,11 @@ export default class Diff extends Base {
Object.values(matchesBySdk).forEach((matches) => {
matches.forEach((m) => {
const match = { ...m }
const aliasedName = aliasMap[match.name]
// Try to find alias by direct match first, then by transformed constant name
// This handles cases where the generated types file creates constants like MY_VARIABLE
// for a variable key like 'my-variable'
const aliasedName =
aliasMap[match.name] || aliasMap[keyToConstant(match.name)]
if (match.isUnknown && aliasedName) {
match.alias = match.name
match.name = aliasedName
Expand Down
4 changes: 2 additions & 2 deletions src/commands/generate/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { fetchAllVariables } from '../../api/variables'
import { Flags } from '@oclif/core'
import { Feature, Project, Variable, CustomProperty } from '../../api/schemas'
import { OrganizationMember, fetchOrganizationMembers } from '../../api/members'
import { upperCase } from 'lodash'
import { createHash } from 'crypto'
import path from 'path'
import { keyToConstant } from '../../utils/keyToConstant'
import {
fetchAllCompletedOrArchivedFeatures,
fetchFeatures,
Expand Down Expand Up @@ -353,7 +353,7 @@ export const ${constantName} = '${hashedKey}' as const`
}

getVariableGeneratedName(variable: Variable) {
let constantName = upperCase(variable.key).replace(/\s/g, '_')
let constantName = keyToConstant(variable.key)

if (this.methodNames[constantName]?.length) {
constantName = `${constantName}_${this.methodNames[constantName].length}`
Expand Down
10 changes: 10 additions & 0 deletions src/utils/keyToConstant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { upperCase } from 'lodash'

/**
* Converts a variable key to its generated constant name format.
*
* Example: 'my-variable' -> 'MY_VARIABLE'
*/
export function keyToConstant(variableKey: string): string {
return upperCase(variableKey).replace(/\s/g, '_')
}
13 changes: 13 additions & 0 deletions test-utils/fixtures/configs/autoAliasConfig.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"

typeGenerator:
outputPath: "./test-utils/fixtures/configs/mockTypes.ts"

codeInsights:
matchPatterns:
ts:
- ":\\s*([a-z][a-z0-9-]*)"

5 changes: 5 additions & 0 deletions test-utils/fixtures/configs/mockTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Mock generated types file for testing auto-alias resolution
export const MY_TEST_VARIABLE = 'my-test-variable' as const
export const ENABLE_FEATURE = 'enable-feature' as const
export const REMOVED_VARIABLE = 'removed-variable' as const

17 changes: 17 additions & 0 deletions test-utils/fixtures/diff/auto-alias-transform
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
diff --git a/test-utils/fixtures/diff/sampleDiff b/test-utils/fixtures/diff/sampleDiff
new file mode 100644
index 00000000..e69de29b
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 config = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might want to call the useVariableValue here for a more accurate test instead of a string match?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we use useVariableValue then the bug doesn't get caught 🙃

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the bug is related to the use of the variableKey and the VariableAlias, should get caught either way

+ feature: my-test-variable,
+ beta: enable-feature
+}
-const old = {
- feature: removed-variable
-}