Skip to content

Commit

Permalink
fix: escape double quotes for ts outputStringLiterals (#1097)
Browse files Browse the repository at this point in the history
* fix: escape double quotes for ts outputStringLiterals

* docs: add changeset
  • Loading branch information
andnorda committed Feb 8, 2024
1 parent b3f5d86 commit cd9f484
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/green-wolves-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'style-dictionary': patch
---

Escape double quotes for ts outputStringLiterals
4 changes: 2 additions & 2 deletions __tests__/common/formatHelpers/getTypeScriptType.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ describe('common', () => {
});

it('should handle outputStringLiterals', () => {
const stringValue = 'I am a string';
const stringValue = 'I "am" a string';
const options = { outputStringLiterals: true };
expect(getTypeScriptType(stringValue, options)).to.equal(`"${stringValue}"`);
expect(getTypeScriptType(stringValue, options)).to.equal('"I \\"am\\" a string"');
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ snapshots["formats typescript/es6-declarations with outputStringLiterals should
/** Used for errors */
export const colorRed : "#FF0000";
export const fontFamily : "\\"Source Sans Pro\\", Arial, sans-serif";
`;
/* end snapshot formats typescript/es6-declarations with outputStringLiterals should match snapshot */

6 changes: 6 additions & 0 deletions __tests__/formats/typeScriptEs6Declarations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ const tokens = {
value: '#FF0000',
},
},
font: {
family: {
name: 'fontFamily',
value: '"Source Sans Pro", Arial, sans-serif',
},
},
};

const format = formats['typescript/es6-declarations'];
Expand Down
2 changes: 1 addition & 1 deletion lib/common/formatHelpers/getTypeScriptType.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default function getTypeScriptType(value, options = {}) {

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

return 'any';
Expand Down

0 comments on commit cd9f484

Please sign in to comment.