Skip to content

Commit 2805322

Browse files
sonar fixes
1 parent 30836b4 commit 2805322

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

src/generators/typescript/renderers/ClassRenderer.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,15 @@ ${this.indent(this.renderBlock(content, 2))}
3939
const constName = prop.propertyName
4040
.replaceAll(/([a-z])([A-Z])/g, '$1_$2')
4141
.toUpperCase();
42-
const safeValue = typeof constValue === 'string' ? constValue : String(constValue);
42+
// Handle different types: strings stay as-is, objects use JSON.stringify, primitives use String()
43+
let safeValue: string;
44+
if (typeof constValue === 'string') {
45+
safeValue = constValue;
46+
} else if (typeof constValue === 'object' && constValue !== null) {
47+
safeValue = JSON.stringify(constValue);
48+
} else {
49+
safeValue = String(constValue);
50+
}
4351
return `export const ${constName} = ${safeValue};`;
4452
})
4553
.filter((val): val is string => val !== null);
@@ -106,10 +114,18 @@ ${renderer.indent(renderer.renderBlock(assignments))}
106114
},
107115
getter({ property }): string {
108116
const constVal = property.property.options.const?.value;
109-
const returnType = constVal !== undefined
110-
? (typeof constVal === 'string' ? constVal : String(constVal))
111-
: property.property.type;
112-
return `get ${property.propertyName}(): ${returnType}${property.required === false ? ' | undefined' : ''} { return this._${property.propertyName}; }`;
117+
let returnType: string;
118+
if (constVal === undefined) {
119+
returnType = property.property.type;
120+
} else if (typeof constVal === 'string') {
121+
returnType = constVal;
122+
} else if (typeof constVal === 'object' && constVal !== null) {
123+
returnType = JSON.stringify(constVal);
124+
} else {
125+
returnType = String(constVal);
126+
}
127+
const optionalSuffix = property.required === false ? ' | undefined' : '';
128+
return `get ${property.propertyName}(): ${returnType}${optionalSuffix} { return this._${property.propertyName}; }`;
113129
},
114130
setter({ property }): string {
115131
// if const value exists we should not render a setter

0 commit comments

Comments
 (0)