Skip to content

Commit

Permalink
Update strict math (madskristensen#427)
Browse files Browse the repository at this point in the history
* Less uses the new strict math now.

* Included additional tests and updated Json schema.
  • Loading branch information
jws305 authored and madskristensen committed Jul 15, 2019
1 parent 6f49c0e commit e2ac3d7
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/WebCompiler/Compile/LessCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,10 @@ private static string ConstructArguments(Config config)
if (options.SourceMap || config.SourceMap)
arguments += " --source-map-map-inline";

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

if (options.IECompat)
arguments += " --ie-compat";
Expand Down
10 changes: 10 additions & 0 deletions src/WebCompiler/Compile/LessOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ protected override void LoadSettings(Config config)
if (ieCompat != null)
IECompat = ieCompat.ToLowerInvariant() == trueStr;

var math = GetValue(config, "math");
if (math != null)
Math = math;

var strictMath = GetValue(config, "strictMath");
if (strictMath != null)
StrictMath = strictMath.ToLowerInvariant() == trueStr;
Expand Down Expand Up @@ -85,6 +89,12 @@ protected override string CompilerFileName
[JsonProperty("ieCompat")]
public bool IECompat { get; set; } = true;

/// <summary>
/// New option for math that replaces 'strictMath' option.
/// </summary>
[JsonProperty("math")]
public string Math { get; set; } = null;

/// <summary>
/// Without this option on Less will try and process all maths in your CSS.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/WebCompilerTest/Compile/LessOptionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void StrictMath()
{
var configs = ConfigHandler.GetConfigs("../../artifacts/lessconfig.json");
var result = LessOptions.FromConfig(configs.First());
Assert.AreEqual(true, result.StrictMath);
Assert.AreEqual("strict", result.Math);
}
}
}
12 changes: 12 additions & 0 deletions src/WebCompilerTest/Compile/LessTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public void Cleanup()
public void CompileLess()
{
var result = _processor.Process("../../artifacts/lessconfig.json");
Assert.IsTrue(result.All(r => !r.HasErrors));
Assert.IsTrue(File.Exists("../../artifacts/less/test.css"));
Assert.IsTrue(File.Exists("../../artifacts/less/test.min.css"));
Assert.IsTrue(result.ElementAt(1).CompiledContent.Contains("url(foo.png)"));
Expand All @@ -51,6 +52,7 @@ public void CompileLess()
public void CompileLessWithError()
{
var result = _processor.Process("../../artifacts/lessconfigerror.json");
Assert.IsTrue(result.Any(r => r.HasErrors));
Assert.IsTrue(result.Count() == 1);
Assert.IsTrue(result.ElementAt(0).HasErrors);
}
Expand All @@ -59,6 +61,7 @@ public void CompileLessWithError()
public void CompileLessWithParsingExceptionError()
{
var result = _processor.Process("../../artifacts/lessconfigParseerror.json");
Assert.IsTrue(result.Any(r => r.HasErrors));
Assert.IsTrue(result.Count() == 1);
Assert.IsTrue(result.ElementAt(0).HasErrors);
Assert.AreNotEqual(0, result.ElementAt(0).Errors.ElementAt(0).LineNumber, "LineNumber is set when engine.TransformToCss generate a ParsingException");
Expand All @@ -76,14 +79,23 @@ public void CompileLessWithOptions()
public void AssociateExtensionSourceFileChangedTest()
{
var result = _processor.SourceFileChanged("../../artifacts/lessconfig.json", "less/test.less", null);
Assert.IsTrue(result.All(r => !r.HasErrors));
Assert.AreEqual(2, result.Count<CompilerResult>());
}

[TestMethod, TestCategory("LESS")]
public void OtherExtensionTypeSourceFileChangedTest()
{
var result = _processor.SourceFileChanged("../../artifacts/lessconfig.json", "scss/test.scss", null);
Assert.IsTrue(result.All(r => !r.HasErrors));
Assert.AreEqual(0, result.Count<CompilerResult>());
}

[TestMethod, TestCategory("LESS")]
public void CompileLessLegacyStrictMath()
{
var result = _processor.Process("../../artifacts/lessconfigLegacyStrictMath.json");
Assert.IsTrue(result.All(r => !r.HasErrors || r.Errors.All(e => e.IsWarning)));
}
}
}
1 change: 1 addition & 0 deletions src/WebCompilerTest/WebCompilerTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
<None Include="artifacts\handlebars\test.hbs" />
<None Include="artifacts\handlebars\error.hbs" />
<None Include="artifacts\handlebars\_partial.hbs" />
<None Include="artifacts\lessconfigLegacyStrictMath.json" />
<None Include="artifacts\scss\sub\foo.scss" />
<None Include="artifacts\stylusconfig.json" />
<None Include="artifacts\less\sub\relative.less" />
Expand Down
2 changes: 1 addition & 1 deletion src/WebCompilerTest/artifacts/lessconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"includeInProject": true,
"options": {
"relativeUrls": true,
"strictMath": true
"math": "strict"
}
},
{
Expand Down
23 changes: 23 additions & 0 deletions src/WebCompilerTest/artifacts/lessconfigLegacyStrictMath.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[
{
"outputFile": "less/test.css",
"inputFile": "less/test.less",
"includeInProject": true,
"options": {
"relativeUrls": true,
"math": "strict-legacy"
}
},
{
"outputFile": "less/relative.css",
"inputFile": "less/sub/relative.less",
"minify": {
"enabled": true,
"adjustRelativePaths": true
},
"options": {
"autoPrefix": "last 2 versions, > 5%",
"sourceMap": true
}
}
]
7 changes: 6 additions & 1 deletion src/WebCompilerVsix/JSON/compilerdefaults-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,18 @@
"type": "boolean",
"default": true
},
"math": {
"description": "LESS only. Specifies which mode Less will use to process the math in your CSS.",
"default": "none",
"enum": ["always", "parens-division", "parens", "strict", "strict-legacy"]
},
"sourceMap": {
"description": "Generates a base64 encoded source map at the bottom of the output.",
"type": "boolean",
"default": false
},
"strictMath": {
"description": "LESS only. Without this option on Less will try and process all maths in your CSS.",
"description": "(DEPRECATED: Use 'math' instead) LESS only. Without this option on Less will try and process all maths in your CSS.",
"type": "boolean",
"default": false
},
Expand Down

0 comments on commit e2ac3d7

Please sign in to comment.