Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: escape double quotes for ts outputStringLiterals #1097

Merged
merged 3 commits into from
Feb 8, 2024
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
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
Loading