From 5474f1301f1817fe2f7238e97ccd336f2d8fc50f Mon Sep 17 00:00:00 2001 From: NenadBojkovski Date: Fri, 18 Mar 2022 12:42:26 +0100 Subject: [PATCH] Make excludes working (#653) - Make excludes working by making inputData's fileName actually refer to file name of the file where code being formatted is coming from - CodeOrigin SourceFile contains the full file path instead of just the name of the file. - CodeOrigin added to Tokens Co-authored-by: Nenad Bojkovski --- src/formatter/Cli.hx | 7 +++--- src/formatter/Formatter.hx | 25 +++++++++++++------ test/GoldBaseTest.hx | 9 ++++--- test/SelfTest.hx | 2 +- test/TestCaseMacro.hx | 2 +- test/formatter/EmptyLinesTest.hx | 2 +- test/testcases/FormatRangeTestCases.hx | 3 ++- test/testcases/other/disabled_excluded.hxtest | 2 +- 8 files changed, 32 insertions(+), 20 deletions(-) diff --git a/src/formatter/Cli.hx b/src/formatter/Cli.hx index 306b308d..e839149c 100644 --- a/src/formatter/Cli.hx +++ b/src/formatter/Cli.hx @@ -175,8 +175,7 @@ class Cli { Sys.exit(3); } var config = Formatter.loadConfig(paths[0]); - - var result:Result = Formatter.format(Code(content.toString()), config); + var result:Result = Formatter.format(Code(content.toString(), SourceFile(paths[0])), config); switch (result) { case Success(formattedCode): Sys.println(formattedCode); @@ -205,7 +204,7 @@ class Cli { verboseLogFile(path, config); } var content:String = File.getContent(path); - var result:Result = Formatter.format(Code(content), config); + var result:Result = Formatter.format(Code(content, SourceFile(path)), config); switch (result) { case Success(formattedCode): FormatStats.incSuccess(); @@ -218,7 +217,7 @@ class Cli { exitCode = 1; } case CheckStability: - var secondResult = Formatter.format(Code(formattedCode), config); + var secondResult = Formatter.format(Code(formattedCode, SourceFile(path)), config); function unstable() { Sys.println('Unstable formatting in $path'); exitCode = 1; diff --git a/src/formatter/Formatter.hx b/src/formatter/Formatter.hx index 15ed0b4e..f6e5e09d 100644 --- a/src/formatter/Formatter.hx +++ b/src/formatter/Formatter.hx @@ -52,10 +52,13 @@ class Formatter { }; return formatInputData(inputData); #end - case Code(code): + case Code(code, origin): var content:Bytes = Bytes.ofString(code); inputData = { - fileName: "code snippet", + fileName: switch (origin) { + case SourceFile(fileName): fileName; + case Snippet: "code snippet"; + }, content: content, config: config, lineSeparator: lineSeparator, @@ -63,9 +66,12 @@ class Formatter { range: range }; return formatInputData(inputData); - case Tokens(tokenList, tokenTree, code): + case Tokens(tokenList, tokenTree, code, origin): inputData = { - fileName: "", + fileName: switch (origin) { + case SourceFile(fileName): fileName; + case Snippet: "code snippet"; + }, content: code, tokenList: tokenList, tokenTree: tokenTree, @@ -165,7 +171,7 @@ class Formatter { #if (js && !nodejs) public static function main() { - var result:Result = Formatter.format(Code(" trace ( 'foo' ) ; "), new Config(), ExpressionLevel); + var result:Result = Formatter.format(Code(" trace ( 'foo' ) ; ", Snippet), new Config(), ExpressionLevel); switch (result) { case Success(formattedCode): js.Browser.console.log("Success: " + formattedCode); @@ -182,6 +188,11 @@ enum FormatterInput { #if (sys || nodejs) FileInput(fileName:String); #end - Code(code:String); - Tokens(tokenList:Array, tokenTree:TokenTree, code:Bytes); + Code(code:String, origin:CodeOrigin); + Tokens(tokenList:Array, tokenTree:TokenTree, code:Bytes, origin:CodeOrigin); +} + +enum CodeOrigin { + SourceFile(fileName:String); + Snippet; } diff --git a/test/GoldBaseTest.hx b/test/GoldBaseTest.hx index e3d7345a..f0974dee 100644 --- a/test/GoldBaseTest.hx +++ b/test/GoldBaseTest.hx @@ -12,13 +12,13 @@ class GoldBaseTest { function goldCheck(fileName:String, unformatted:String, goldCode:String, lineSeparator:String, ?configString:String, ?pos:PosInfos) { var config = new Config(); config.readConfigFromString(configString, "goldhxformat.json"); - var result:Result = Formatter.format(Code(unformatted), config, lineSeparator, entryPoint); + var result:Result = Formatter.format(Code(unformatted, SourceFile(fileName)), config, lineSeparator, entryPoint); handleResult(fileName, result, goldCode, pos); // second run to make sure result is stable switch (result) { case Success(formattedCode): - result = Formatter.format(Code(formattedCode), config, lineSeparator, entryPoint); + result = Formatter.format(Code(formattedCode, SourceFile(fileName)), config, lineSeparator, entryPoint); handleResult(fileName, result, goldCode, pos); case Failure(errorMessage): case Disabled: @@ -26,8 +26,9 @@ class GoldBaseTest { } function handleResult(fileName:String, result:Result, goldCode:String, ?pos:PosInfos) { - var isDisabled:Bool = fileName.startsWith("disabled_"); - var isFailing:Bool = fileName.startsWith("failing_"); + var file = new haxe.io.Path(fileName).file; + var isDisabled:Bool = file.startsWith("disabled_"); + var isFailing:Bool = file.startsWith("failing_"); switch (result) { case Success(formattedCode): diff --git a/test/SelfTest.hx b/test/SelfTest.hx index c5d1f357..c0a1f9af 100644 --- a/test/SelfTest.hx +++ b/test/SelfTest.hx @@ -34,7 +34,7 @@ class SelfTest { function checkFile(fileName:String, ?pos:PosInfos) { var code:String = File.getContent(fileName); - var result = Formatter.format(Code(code), Formatter.loadConfig(fileName)); + var result = Formatter.format(Code(code, SourceFile(fileName)), Formatter.loadConfig(fileName)); switch (result) { case Success(formattedCode): if (code != formattedCode) { diff --git a/test/TestCaseMacro.hx b/test/TestCaseMacro.hx index fd288b2c..a249a3a7 100644 --- a/test/TestCaseMacro.hx +++ b/test/TestCaseMacro.hx @@ -40,7 +40,7 @@ class TestCaseMacro { return (macro class { @Test public function $fieldName() { - goldCheck($v{fieldName}, $v{unformatted}, $v{gold}, $v{lineSeparator}, $v{config}); + goldCheck($v{fileName}, $v{unformatted}, $v{gold}, $v{lineSeparator}, $v{config}); }; }).fields[0]; } diff --git a/test/formatter/EmptyLinesTest.hx b/test/formatter/EmptyLinesTest.hx index ba89ca85..9a838b67 100644 --- a/test/formatter/EmptyLinesTest.hx +++ b/test/formatter/EmptyLinesTest.hx @@ -90,7 +90,7 @@ class EmptyLinesTest { function format(unformatted:String, ?pos:PosInfos):String { var config = new Config(); config.readConfigFromString("{}", "goldhxformat.json"); - var result:Result = Formatter.format(Code(unformatted), config, null, TypeLevel); + var result:Result = Formatter.format(Code(unformatted, Snippet), config, null, TypeLevel); switch (result) { case Success(formattedCode): return formattedCode; diff --git a/test/testcases/FormatRangeTestCases.hx b/test/testcases/FormatRangeTestCases.hx index 7d37e3b0..5b1c626e 100644 --- a/test/testcases/FormatRangeTestCases.hx +++ b/test/testcases/FormatRangeTestCases.hx @@ -22,7 +22,8 @@ class FormatRangeTestCases extends GoldBaseTest { } unformatted = unformatted.replace("]<", ""); - var result:Result = Formatter.format(Code(unformatted), config, lineSeparator, entryPoint, {startPos: startIndex, endPos: endIndex}); + var result:Result = Formatter.format(Code(unformatted, SourceFile(fileName)), config, lineSeparator, entryPoint, + {startPos: startIndex, endPos: endIndex}); handleResult(fileName, result, goldCode, pos); } } diff --git a/test/testcases/other/disabled_excluded.hxtest b/test/testcases/other/disabled_excluded.hxtest index 2a1f6310..9f316f47 100644 --- a/test/testcases/other/disabled_excluded.hxtest +++ b/test/testcases/other/disabled_excluded.hxtest @@ -1,5 +1,5 @@ { - "excludes": [".*"] + "excludes": ["test\/testcases\/other\/disabled_excluded.hxtest"] } ---