Skip to content

Commit cd9f484

Browse files
authored
fix: escape double quotes for ts outputStringLiterals (#1097)
* fix: escape double quotes for ts outputStringLiterals * docs: add changeset
1 parent b3f5d86 commit cd9f484

File tree

5 files changed

+15
-3
lines changed

5 files changed

+15
-3
lines changed

.changeset/green-wolves-heal.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'style-dictionary': patch
3+
---
4+
5+
Escape double quotes for ts outputStringLiterals

__tests__/common/formatHelpers/getTypeScriptType.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ describe('common', () => {
6262
});
6363

6464
it('should handle outputStringLiterals', () => {
65-
const stringValue = 'I am a string';
65+
const stringValue = 'I "am" a string';
6666
const options = { outputStringLiterals: true };
67-
expect(getTypeScriptType(stringValue, options)).to.equal(`"${stringValue}"`);
67+
expect(getTypeScriptType(stringValue, options)).to.equal('"I \\"am\\" a string"');
6868
});
6969
});
7070
});

__tests__/formats/__snapshots__/typeScriptEs6Declarations.test.snap.js

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ snapshots["formats typescript/es6-declarations with outputStringLiterals should
99
1010
/** Used for errors */
1111
export const colorRed : "#FF0000";
12+
export const fontFamily : "\\"Source Sans Pro\\", Arial, sans-serif";
1213
`;
1314
/* end snapshot formats typescript/es6-declarations with outputStringLiterals should match snapshot */
1415

__tests__/formats/typeScriptEs6Declarations.test.js

+6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ const tokens = {
2828
value: '#FF0000',
2929
},
3030
},
31+
font: {
32+
family: {
33+
name: 'fontFamily',
34+
value: '"Source Sans Pro", Arial, sans-serif',
35+
},
36+
},
3137
};
3238

3339
const format = formats['typescript/es6-declarations'];

lib/common/formatHelpers/getTypeScriptType.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export default function getTypeScriptType(value, options = {}) {
4545

4646
if (Array.isArray(value)) return getArrayType(value);
4747
if (typeof value === 'object') return getObjectType(value);
48-
if (outputStringLiterals && typeof value === 'string') return `"${value}"`;
48+
if (outputStringLiterals && typeof value === 'string') return `"${value.replace(/"/g, '\\"')}"`;
4949
if (['string', 'number', 'boolean'].includes(typeof value)) return typeof value;
5050

5151
return 'any';

0 commit comments

Comments
 (0)