Skip to content

Commit 177c25e

Browse files
authored
fix: account for multiline comments in js/es6 format (#1439)
1 parent 61ed767 commit 177c25e

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

.changeset/spotty-clocks-drop.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'style-dictionary': patch
3+
---
4+
5+
Account for multiline comments in javascript/es6 format

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

+12
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,15 @@ export const red = "#EF5350"; // comment
1919
`;
2020
/* end snapshot formats javascript/es6 should handle DTCG token format, be a valid JS file and matches snapshot */
2121

22+
snapshots["formats javascript/es6 should handle multiline comments"] =
23+
`/**
24+
* Do not edit directly, this file was auto-generated.
25+
*/
26+
27+
export const red = "#EF5350"; // comment
28+
// multiline
29+
// comment
30+
export const blue = "#4FEDF0";
31+
`;
32+
/* end snapshot formats javascript/es6 should handle multiline comments */
33+

__tests__/formats/es6Constants.test.js

+39
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,31 @@ const DTCGTokens = {
5656
},
5757
};
5858

59+
const commentTokens = {
60+
color: {
61+
red: {
62+
comment: 'comment',
63+
name: 'red',
64+
original: {
65+
value: '#EF5350',
66+
},
67+
path: ['color', 'red'],
68+
type: 'color',
69+
value: '#EF5350',
70+
},
71+
blue: {
72+
comment: 'multiline\ncomment',
73+
name: 'blue',
74+
original: {
75+
value: '#4FEDF0',
76+
},
77+
path: ['color', 'blue'],
78+
type: 'color',
79+
value: '#4FEDF0',
80+
},
81+
},
82+
};
83+
5984
const format = formats[javascriptEs6];
6085

6186
describe('formats', () => {
@@ -85,5 +110,19 @@ describe('formats', () => {
85110

86111
await expect(output).to.matchSnapshot();
87112
});
113+
114+
it('should handle multiline comments', async () => {
115+
const output = await format(
116+
createFormatArgs({
117+
dictionary: {
118+
tokens: commentTokens,
119+
allTokens: convertTokenData(commentTokens, { output: 'array' }),
120+
},
121+
file,
122+
platform: {},
123+
}),
124+
);
125+
await expect(output).to.matchSnapshot();
126+
});
88127
});
89128
});

lib/common/formats.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import {
5858
formats as fileFormats,
5959
propertyFormatNames,
6060
} from '../enums/index.js';
61+
import { addComment } from './formatHelpers/createPropertyFormatter.js';
6162

6263
/**
6364
* @typedef {import('../../types/Format.d.ts').Format} Format
@@ -644,7 +645,13 @@ const formats = {
644645
const comment = options.usesDtcg ? token.$description : token.comment;
645646
const to_ret = `export const ${token.name} = ${value};`;
646647

647-
return comment ? to_ret.concat(`// ${comment}`) : to_ret;
648+
const format = {
649+
commentStyle: short,
650+
indentation: '',
651+
...formatting,
652+
};
653+
654+
return comment ? addComment(to_ret, comment, format) : to_ret;
648655
}),
649656
]
650657
.flat()

0 commit comments

Comments
 (0)