Skip to content

Commit

Permalink
feat: allow overriding formatting, add commentStyle long-above, short…
Browse files Browse the repository at this point in the history
…-above
  • Loading branch information
jorenbroekema committed Oct 23, 2023
1 parent b5d4cc2 commit b627ff1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 127 deletions.
80 changes: 0 additions & 80 deletions __tests__/common/formatHelpers/createPropertyFormatter.js

This file was deleted.

50 changes: 25 additions & 25 deletions __tests__/common/formatHelpers/createPropertyFormatter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,30 @@
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
const createPropertyFormatter = require('../../../lib/common/formatHelpers/createPropertyFormatter');
const createDictionary = require('../../../lib/utils/createDictionary');
const createPropertyFormatter = require("../../../lib/common/formatHelpers/createPropertyFormatter");
const createDictionary = require("../../../lib/utils/createDictionary");

const dictionary = createDictionary({
properties: {
tokens: {
foo: {
original: {
value: '5px',
type: 'spacing'
value: "5px",
type: "spacing",
},
attributes: {
category: 'tokens',
type: 'foo'
category: "tokens",
type: "foo",
},
name: 'tokens-foo',
path: ['tokens', 'foo'],
value: '5px',
type: 'spacing'
name: "tokens-foo",
path: ["tokens", "foo"],
value: "5px",
type: "spacing",
},
ref: {
original: {
value: '{tokens.foo}',
type: 'spacing'
value: "{tokens.foo}",
type: "spacing",
},
attributes: {
category: 'tokens',
Expand All @@ -53,22 +53,22 @@ const transformedDictionary = createDictionary({
tokens: {
foo: {
original: {
value: '5px',
type: 'spacing'
value: "5px",
type: "spacing",
},
attributes: {
category: 'tokens',
type: 'foo'
category: "tokens",
type: "foo",
},
name: 'tokens-foo',
path: ['tokens', 'foo'],
value: '5px',
type: 'spacing'
name: "tokens-foo",
path: ["tokens", "foo"],
value: "5px",
type: "spacing",
},
ref: {
original: {
value: '{tokens.foo}',
type: 'spacing'
value: "{tokens.foo}",
type: "spacing",
},
attributes: {
category: 'tokens',
Expand All @@ -79,8 +79,8 @@ const transformedDictionary = createDictionary({
value: 'changed by transitive transform',
type: 'spacing'
},
}
}
},
},
});

const numberDictionary = createDictionary({
Expand Down Expand Up @@ -245,4 +245,4 @@ describe('common', () => {
})
})
})
})
})
48 changes: 26 additions & 22 deletions lib/common/formatHelpers/createPropertyFormatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ const defaultFormatting = {
function addComment(to_ret_prop, options) {
const { comment, style, indentation } = options;

const [commentStyle,placeAbove] = style.split('-above');
const commentsByNewLine = comment.split("\n");
const hasNewLines = commentsByNewLine.length > 1;

const commentCase = `${style}${hasNewLines ? '-new-lines' :''}`;
const commentCase = `${commentStyle}${hasNewLines ? '-new-lines' :''}`;
let processed;

switch(commentCase) {
Expand All @@ -49,19 +50,21 @@ function addComment(to_ret_prop, options) {
(acc, curr) => `${acc}${indentation}// ${curr}\n`,
''
);
// remove trailing newline
processed = processed.replace(/\n$/g, '');
break;
case 'long-new-lines':
processed = commentsByNewLine.reduce(
(acc, curr) => `${acc}${indentation} * ${curr}\n`,
`${indentation}/**\n`
);
processed += `${indentation} */\n`;
processed += `${indentation} */`;
break;
}

if (hasNewLines) {
// put the comment above the prop if it's multi-line
to_ret_prop = `${processed}${to_ret_prop}`;
if (hasNewLines || placeAbove !== undefined) {
// put the comment above the prop if it's multi-line or if commentStyle ended with -above
to_ret_prop = `${processed}\n${to_ret_prop}`;
} else {
to_ret_prop = `${to_ret_prop} ${processed}`;
}
Expand Down Expand Up @@ -107,33 +110,34 @@ function createPropertyFormatter({
formatting = {},
themeable = false
}) {
let {prefix, commentStyle, indentation, separator, suffix} = Object.assign({}, defaultFormatting, formatting);

const formatDefaults = {};
switch(format) {
case 'css':
prefix = '--';
indentation = ' ';
separator = ':';
formatDefaults.prefix = '--';
formatDefaults.indentation = ' ';
formatDefaults.separator = ':';
break;
case 'sass':
prefix = '$';
commentStyle = 'short';
indentation = '';
separator = ':';
formatDefaults.prefix = '$';
formatDefaults.commentStyle = 'short';
formatDefaults.indentation = '';
formatDefaults.separator = ':';
break;
case 'less':
prefix = '@';
commentStyle = 'short';
indentation = '';
separator = ':';
formatDefaults.prefix = '@';
formatDefaults.commentStyle = 'short';
formatDefaults.indentation = '';
formatDefaults.separator = ':';
break;
case 'stylus':
prefix = '$';
commentStyle = 'short';
indentation = '';
separator = '=';
formatDefaults.prefix = '$';
formatDefaults.commentStyle = 'short';
formatDefaults.indentation = '';
formatDefaults.separator = '=';
break;
}
let {prefix, commentStyle, indentation, separator, suffix} = Object.assign({}, defaultFormatting, formatDefaults, formatting);


return function(prop) {
let to_ret_prop = `${indentation}${prefix}${prop.name}${separator} `;
Expand Down

0 comments on commit b627ff1

Please sign in to comment.