From a9d6d32c36077b90f4ba043a0821e430ceaf028f Mon Sep 17 00:00:00 2001 From: Jonas Krogh Date: Fri, 24 Jan 2025 10:36:19 +0100 Subject: [PATCH] fix: allow multiline comments in js/es6 format --- .../__snapshots__/es6Constants.test.snap.js | 12 ++++++ __tests__/formats/es6Constants.test.js | 39 +++++++++++++++++++ lib/common/formats.js | 9 ++++- 3 files changed, 59 insertions(+), 1 deletion(-) diff --git a/__tests__/formats/__snapshots__/es6Constants.test.snap.js b/__tests__/formats/__snapshots__/es6Constants.test.snap.js index 7f82dd950..4df8650b5 100644 --- a/__tests__/formats/__snapshots__/es6Constants.test.snap.js +++ b/__tests__/formats/__snapshots__/es6Constants.test.snap.js @@ -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 */ + diff --git a/__tests__/formats/es6Constants.test.js b/__tests__/formats/es6Constants.test.js index b3de68342..c1f8af722 100644 --- a/__tests__/formats/es6Constants.test.js +++ b/__tests__/formats/es6Constants.test.js @@ -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', () => { @@ -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(); + }); }); }); diff --git a/lib/common/formats.js b/lib/common/formats.js index e9deb1e83..447ba5d26 100644 --- a/lib/common/formats.js +++ b/lib/common/formats.js @@ -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 @@ -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()