Skip to content

Commit

Permalink
Make excludes working (#653)
Browse files Browse the repository at this point in the history
- 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 <[email protected]>
  • Loading branch information
NenadBojkovski authored Mar 18, 2022
1 parent 0a28fce commit 5474f13
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 20 deletions.
7 changes: 3 additions & 4 deletions src/formatter/Cli.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand All @@ -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;
Expand Down
25 changes: 18 additions & 7 deletions src/formatter/Formatter.hx
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,26 @@ 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,
entryPoint: entryPoint,
range: range
};
return formatInputData(inputData);
case Tokens(tokenList, tokenTree, code):
case Tokens(tokenList, tokenTree, code, origin):
inputData = {
fileName: "<unknown.hx>",
fileName: switch (origin) {
case SourceFile(fileName): fileName;
case Snippet: "code snippet";
},
content: code,
tokenList: tokenList,
tokenTree: tokenTree,
Expand Down Expand Up @@ -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);
Expand All @@ -182,6 +188,11 @@ enum FormatterInput {
#if (sys || nodejs)
FileInput(fileName:String);
#end
Code(code:String);
Tokens(tokenList:Array<Token>, tokenTree:TokenTree, code:Bytes);
Code(code:String, origin:CodeOrigin);
Tokens(tokenList:Array<Token>, tokenTree:TokenTree, code:Bytes, origin:CodeOrigin);
}

enum CodeOrigin {
SourceFile(fileName:String);
Snippet;
}
9 changes: 5 additions & 4 deletions test/GoldBaseTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,23 @@ 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:
}
}

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):
Expand Down
2 changes: 1 addition & 1 deletion test/SelfTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion test/TestCaseMacro.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down
2 changes: 1 addition & 1 deletion test/formatter/EmptyLinesTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion test/testcases/FormatRangeTestCases.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion test/testcases/other/disabled_excluded.hxtest
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"excludes": [".*"]
"excludes": ["test\/testcases\/other\/disabled_excluded.hxtest"]
}

---
Expand Down

0 comments on commit 5474f13

Please sign in to comment.