Skip to content

Commit e2ac3d7

Browse files
jws305madskristensen
authored andcommitted
Update strict math (#427)
* Less uses the new strict math now. * Included additional tests and updated Json schema.
1 parent 6f49c0e commit e2ac3d7

File tree

8 files changed

+58
-5
lines changed

8 files changed

+58
-5
lines changed

src/WebCompiler/Compile/LessCompiler.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,10 @@ private static string ConstructArguments(Config config)
115115
if (options.SourceMap || config.SourceMap)
116116
arguments += " --source-map-map-inline";
117117

118-
if (options.StrictMath)
119-
arguments += " --strict-math=on";
118+
if (options.Math != null)
119+
arguments += $" --math={options.Math}";
120+
else if (options.StrictMath)
121+
arguments += " --math=strict-legacy";
120122

121123
if (options.IECompat)
122124
arguments += " --ie-compat";

src/WebCompiler/Compile/LessOptions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ protected override void LoadSettings(Config config)
3232
if (ieCompat != null)
3333
IECompat = ieCompat.ToLowerInvariant() == trueStr;
3434

35+
var math = GetValue(config, "math");
36+
if (math != null)
37+
Math = math;
38+
3539
var strictMath = GetValue(config, "strictMath");
3640
if (strictMath != null)
3741
StrictMath = strictMath.ToLowerInvariant() == trueStr;
@@ -85,6 +89,12 @@ protected override string CompilerFileName
8589
[JsonProperty("ieCompat")]
8690
public bool IECompat { get; set; } = true;
8791

92+
/// <summary>
93+
/// New option for math that replaces 'strictMath' option.
94+
/// </summary>
95+
[JsonProperty("math")]
96+
public string Math { get; set; } = null;
97+
8898
/// <summary>
8999
/// Without this option on Less will try and process all maths in your CSS.
90100
/// </summary>

src/WebCompilerTest/Compile/LessOptionsTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public void StrictMath()
2828
{
2929
var configs = ConfigHandler.GetConfigs("../../artifacts/lessconfig.json");
3030
var result = LessOptions.FromConfig(configs.First());
31-
Assert.AreEqual(true, result.StrictMath);
31+
Assert.AreEqual("strict", result.Math);
3232
}
3333
}
3434
}

src/WebCompilerTest/Compile/LessTest.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public void Cleanup()
3131
public void CompileLess()
3232
{
3333
var result = _processor.Process("../../artifacts/lessconfig.json");
34+
Assert.IsTrue(result.All(r => !r.HasErrors));
3435
Assert.IsTrue(File.Exists("../../artifacts/less/test.css"));
3536
Assert.IsTrue(File.Exists("../../artifacts/less/test.min.css"));
3637
Assert.IsTrue(result.ElementAt(1).CompiledContent.Contains("url(foo.png)"));
@@ -51,6 +52,7 @@ public void CompileLess()
5152
public void CompileLessWithError()
5253
{
5354
var result = _processor.Process("../../artifacts/lessconfigerror.json");
55+
Assert.IsTrue(result.Any(r => r.HasErrors));
5456
Assert.IsTrue(result.Count() == 1);
5557
Assert.IsTrue(result.ElementAt(0).HasErrors);
5658
}
@@ -59,6 +61,7 @@ public void CompileLessWithError()
5961
public void CompileLessWithParsingExceptionError()
6062
{
6163
var result = _processor.Process("../../artifacts/lessconfigParseerror.json");
64+
Assert.IsTrue(result.Any(r => r.HasErrors));
6265
Assert.IsTrue(result.Count() == 1);
6366
Assert.IsTrue(result.ElementAt(0).HasErrors);
6467
Assert.AreNotEqual(0, result.ElementAt(0).Errors.ElementAt(0).LineNumber, "LineNumber is set when engine.TransformToCss generate a ParsingException");
@@ -76,14 +79,23 @@ public void CompileLessWithOptions()
7679
public void AssociateExtensionSourceFileChangedTest()
7780
{
7881
var result = _processor.SourceFileChanged("../../artifacts/lessconfig.json", "less/test.less", null);
82+
Assert.IsTrue(result.All(r => !r.HasErrors));
7983
Assert.AreEqual(2, result.Count<CompilerResult>());
8084
}
8185

8286
[TestMethod, TestCategory("LESS")]
8387
public void OtherExtensionTypeSourceFileChangedTest()
8488
{
8589
var result = _processor.SourceFileChanged("../../artifacts/lessconfig.json", "scss/test.scss", null);
90+
Assert.IsTrue(result.All(r => !r.HasErrors));
8691
Assert.AreEqual(0, result.Count<CompilerResult>());
8792
}
93+
94+
[TestMethod, TestCategory("LESS")]
95+
public void CompileLessLegacyStrictMath()
96+
{
97+
var result = _processor.Process("../../artifacts/lessconfigLegacyStrictMath.json");
98+
Assert.IsTrue(result.All(r => !r.HasErrors || r.Errors.All(e => e.IsWarning)));
99+
}
88100
}
89101
}

src/WebCompilerTest/WebCompilerTest.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
<None Include="artifacts\handlebars\test.hbs" />
8787
<None Include="artifacts\handlebars\error.hbs" />
8888
<None Include="artifacts\handlebars\_partial.hbs" />
89+
<None Include="artifacts\lessconfigLegacyStrictMath.json" />
8990
<None Include="artifacts\scss\sub\foo.scss" />
9091
<None Include="artifacts\stylusconfig.json" />
9192
<None Include="artifacts\less\sub\relative.less" />

src/WebCompilerTest/artifacts/lessconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"includeInProject": true,
66
"options": {
77
"relativeUrls": true,
8-
"strictMath": true
8+
"math": "strict"
99
}
1010
},
1111
{
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[
2+
{
3+
"outputFile": "less/test.css",
4+
"inputFile": "less/test.less",
5+
"includeInProject": true,
6+
"options": {
7+
"relativeUrls": true,
8+
"math": "strict-legacy"
9+
}
10+
},
11+
{
12+
"outputFile": "less/relative.css",
13+
"inputFile": "less/sub/relative.less",
14+
"minify": {
15+
"enabled": true,
16+
"adjustRelativePaths": true
17+
},
18+
"options": {
19+
"autoPrefix": "last 2 versions, > 5%",
20+
"sourceMap": true
21+
}
22+
}
23+
]

src/WebCompilerVsix/JSON/compilerdefaults-schema.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,18 @@
3333
"type": "boolean",
3434
"default": true
3535
},
36+
"math": {
37+
"description": "LESS only. Specifies which mode Less will use to process the math in your CSS.",
38+
"default": "none",
39+
"enum": ["always", "parens-division", "parens", "strict", "strict-legacy"]
40+
},
3641
"sourceMap": {
3742
"description": "Generates a base64 encoded source map at the bottom of the output.",
3843
"type": "boolean",
3944
"default": false
4045
},
4146
"strictMath": {
42-
"description": "LESS only. Without this option on Less will try and process all maths in your CSS.",
47+
"description": "(DEPRECATED: Use 'math' instead) LESS only. Without this option on Less will try and process all maths in your CSS.",
4348
"type": "boolean",
4449
"default": false
4550
},

0 commit comments

Comments
 (0)