Skip to content

Commit

Permalink
Merge branch 'master' into 2736-default-tooltip-dap-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
joshblack authored Sep 24, 2019
2 parents 2f57749 + 369b59d commit 7274a0a
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 15 deletions.
23 changes: 23 additions & 0 deletions packages/scss-generator/__tests__/scss-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,17 @@ describe('@carbon/scss', () => {
$test: 1;
};`,
],
[
'function in assignment',
t.Assignment(
t.Identifier('value'),
t.SassFunctionCall(t.Identifier('map-get'), [
t.Identifier('map'),
t.SassString('key'),
])
),
`$value: map-get($map, 'key');`,
],
];

test.each(calls)('%s', (_, ast, expected) => {
Expand Down Expand Up @@ -515,4 +526,16 @@ describe('@carbon/scss', () => {
expect(code.trim()).toEqual(expected.trim());
});
});

describe('formatting', () => {
test('newline', () => {
const { code } = generate(
t.StyleSheet([t.Comment('start'), t.Newline(), t.Comment('end')])
);
expect(code).toBe(`//start
//end
`);
});
});
});
106 changes: 91 additions & 15 deletions packages/scss-generator/src/types/definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,63 @@ const SassString = defineType('SassString', {
},
});

// Allow ability to shortcircuit AST builder limitations and embed raw values
// into the Sass source code
const SassValue = defineType('SassValue', {
fields: {
value: {
validate: assertAny,
},
},
generate(printer, node) {
printer.token(node.value);
},
});

//-------------------------------------------------------------------------------
// Calls
//-------------------------------------------------------------------------------
const SassFunctionCall = defineType('SassFunctionCall', {
fields: {
id: {
validate: assertType(Identifier),
},
params: {
optional: true,
validate: () =>
arrayOf(
assertOneOf([
assertType(Identifier),
assertType(SassBoolean),
assertType(SassList),
assertType(SassMap),
assertType(SassNumber),
assertType(SassString),
])
),
},
},
generate(printer, node) {
printer.space();
printer.print(node.id);
printer.token('(');
if (Array.isArray(node.params)) {
for (let i = 0; i < node.params.length; i++) {
const param = node.params[i];
if (param.type === Identifier.type) {
printer.token('$');
}
printer.print(param, node);
if (i !== node.params.length - 1) {
printer.token(',');
printer.space();
}
}
}
printer.token(')');
},
});

const SassMixinCall = defineType('SassMixinCall', {
fields: {
id: {
Expand Down Expand Up @@ -317,8 +371,13 @@ const SassMixinCall = defineType('SassMixinCall', {
printer.token('(');
if (Array.isArray(node.params)) {
for (let i = 0; i < node.params.length; i++) {
printer.token('$');
printer.print(node.params[i], node);
const param = node.params[i];

if (param.type === Identifier.type) {
printer.token('$');
}

printer.print(param, node);
if (i !== node.params.length - 1) {
printer.token(',');
printer.space();
Expand Down Expand Up @@ -465,6 +524,7 @@ const Assignment = defineType('Assignment', {
assertType(SassMap),
assertType(SassNumber),
assertType(SassString),
assertType(SassFunctionCall),
]),
},
default: {
Expand Down Expand Up @@ -663,19 +723,21 @@ const CallExpression = defineType('CallExpression', {
const StyleSheet = defineType('StyleSheet', {
fields: {
children: {
validate: arrayOf(
assertOneOf([
assertType(Assignment),
assertType(AtRule),
assertType(Comment),
assertType(IfStatement),
assertType(Rule),
assertType(SassFunction),
assertType(SassImport),
assertType(SassMixin),
assertType(SassMixinCall),
])
),
validate: () =>
arrayOf(
assertOneOf([
assertType(Assignment),
assertType(AtRule),
assertType(Comment),
assertType(IfStatement),
assertType(Rule),
assertType(SassFunction),
assertType(SassImport),
assertType(SassMixin),
assertType(SassMixinCall),
assertType(Newline),
])
),
},
},
generate(printer, node) {
Expand All @@ -689,6 +751,15 @@ const StyleSheet = defineType('StyleSheet', {
},
});

//-------------------------------------------------------------------------------
// Formatting
//-------------------------------------------------------------------------------
const Newline = defineType('Newline', {
generate(printer) {
printer.newline();
},
});

module.exports = {
Assignment,
AssignmentPattern,
Expand All @@ -707,13 +778,18 @@ module.exports = {
SassBoolean,
SassColor,
SassFunction,
SassFunctionCall,
SassImport,
SassNumber,
SassString,
SassList,
SassMap,
SassMapProperty,
SassValue,
SassMixin,
SassMixinCall,
StyleSheet,

// Formatting
Newline,
};

0 comments on commit 7274a0a

Please sign in to comment.