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: account for multiline comments in js/es6 format #1439

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions __tests__/formats/__snapshots__/es6Constants.test.snap.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,15 @@ export const red = "#EF5350"; // comment
`;
/* end snapshot formats javascript/es6 should handle DTCG token format, be a valid JS file and matches snapshot */

snapshots["formats javascript/es6 should handle multiline comments"] =
`/**
* Do not edit directly, this file was auto-generated.
*/

export const red = "#EF5350"; // comment
// multiline
// comment
export const blue = "#4FEDF0";
`;
/* end snapshot formats javascript/es6 should handle multiline comments */

39 changes: 39 additions & 0 deletions __tests__/formats/es6Constants.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,31 @@ const DTCGTokens = {
},
};

const commentTokens = {
color: {
red: {
comment: 'comment',
name: 'red',
original: {
value: '#EF5350',
},
path: ['color', 'red'],
type: 'color',
value: '#EF5350',
},
blue: {
comment: 'multiline\ncomment',
name: 'blue',
original: {
value: '#4FEDF0',
},
path: ['color', 'blue'],
type: 'color',
value: '#4FEDF0',
},
},
};

const format = formats[javascriptEs6];

describe('formats', () => {
Expand Down Expand Up @@ -85,5 +110,19 @@ describe('formats', () => {

await expect(output).to.matchSnapshot();
});

it('should handle multiline comments', async () => {
const output = await format(
createFormatArgs({
dictionary: {
tokens: commentTokens,
allTokens: convertTokenData(commentTokens, { output: 'array' }),
},
file,
platform: {},
}),
);
await expect(output).to.matchSnapshot();
});
});
});
9 changes: 8 additions & 1 deletion lib/common/formats.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import {
formats as fileFormats,
propertyFormatNames,
} from '../enums/index.js';
import { addComment } from './formatHelpers/createPropertyFormatter.js';

/**
* @typedef {import('../../types/Format.d.ts').Format} Format
Expand Down Expand Up @@ -640,7 +641,13 @@ const formats = {
const comment = options.usesDtcg ? token.$description : token.comment;
const to_ret = `export const ${token.name} = ${value};`;

return comment ? to_ret.concat(`// ${comment}`) : to_ret;
const format = {
commentStyle: short,
indentation: '',
...formatting,
};

return comment ? addComment(to_ret, comment, format) : to_ret;
}),
]
.flat()
Expand Down