Merge pull request #1459 from MacKLess/less

Fix LESS variable formatting
This commit is contained in:
Liam Newman 2018-07-25 14:09:30 -07:00 committed by GitHub
commit 6e95ef83af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 65 additions and 5 deletions

View File

@ -195,6 +195,8 @@ function Beautifier(source_text, options) {
parenLevel = 0;
var insideRule = false;
// This is the value side of a property value pair (blue in the following ex)
// label { content: blue }
var insidePropertyValue = false;
var enteringConditionalGroup = false;
var insideAtExtend = false;
@ -263,6 +265,9 @@ function Beautifier(source_text, options) {
if (variableOrRule in this.CONDITIONAL_GROUP_RULE) {
enteringConditionalGroup = true;
}
// might be less variable
} else if (!insideRule && parenLevel === 0 && variableOrRule.indexOf(':') !== -1) {
insidePropertyValue = true;
}
}
} else if (ch === '#' && input.peek() === '{') {

View File

@ -466,11 +466,7 @@ function Tokenizer(input_string, opts) {
return [trim(resulting_string) + '\n', 'TK_UNKNOWN'];
}
// Spidermonkey-specific sharp variables for circular references
// https://developer.mozilla.org/En/Sharp_variables_in_JavaScript
// http://mxr.mozilla.org/mozilla-central/source/js/src/jsscan.cpp around line 1935
// Spidermonkey-specific sharp variables for circular references. Considered obsolete.
var sharp = '#';
if (input.hasNext() && input.testChar(digit)) {
do {

View File

@ -9318,6 +9318,24 @@ function run_css_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_bea
t('@page: first {}');
//============================================================
// Issue 1411 -- LESS Variable Assignment Spacing
reset_options();
t(
'@set: {\n' +
'\tone: blue;\n' +
'\ttwo: green;\n' +
'\tthree: red;\n' +
'}\n' +
'.set {\n' +
'\teach(@set, {\n' +
'\t\t@{key}-@{index}: @value;\n' +
'\t}\n' +
'\t);\n' +
'}');
t('@light-blue: @nice-blue + #111;');
//============================================================
// SASS/SCSS
reset_options();

View File

@ -284,6 +284,8 @@ class Beautifier:
printer.nestedLevel += 1
if variableOrRule in self.CONDITIONAL_GROUP_RULE:
enteringConditionalGroup = True
elif not insideRule and parenLevel == 0 and variableOrRule[-1] == ":":
insidePropertyValue = True
elif self.ch == '#' and input.peek() == '{':
printer.preserveSingleSpace(isAfterSpace)
printer.print_string(self.ch + self.eatString('}'))

View File

@ -9276,6 +9276,24 @@ class CSSBeautifierTest(unittest.TestCase):
t('@page: first {}')
#============================================================
# Issue 1411 -- LESS Variable Assignment Spacing
self.reset_options()
t(
'@set: {\n' +
'\tone: blue;\n' +
'\ttwo: green;\n' +
'\tthree: red;\n' +
'}\n' +
'.set {\n' +
'\teach(@set, {\n' +
'\t\t@{key}-@{index}: @value;\n' +
'\t}\n' +
'\t);\n' +
'}')
t('@light-blue: @nice-blue + #111;')
#============================================================
# SASS/SCSS
self.reset_options()

View File

@ -588,6 +588,27 @@ exports.test_data = {
},
{ unchanged: '@page: first {}' }
]
}, {
name: "Issue 1411 -- LESS Variable Assignment Spacing",
description: "",
tests: [{
unchanged: [
'@set: {',
'\tone: blue;',
'\ttwo: green;',
'\tthree: red;',
'}',
'.set {',
'\teach(@set, {',
'\t\t@{key}-@{index}: @value;',
'\t}',
// This is not optimal formatting, included to document current behavior.
'\t);',
'}'
]
},
{ unchanged: '@light-blue: @nice-blue + #111;' }
]
}, {
name: "SASS/SCSS",
description: "",