From c79c408122dc75bad36817b8d5a372f66804cbfb Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Thu, 4 Aug 2022 10:29:56 +0800 Subject: [PATCH] MDLSITE-6748 mustachelint: Add information about eslint failures --- mustache_lint/mustache_lint.php | 15 +++++++++++++-- tests/1-mustache_lint.bats | 11 +++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/mustache_lint/mustache_lint.php b/mustache_lint/mustache_lint.php index 4a8a06fb..612547a8 100644 --- a/mustache_lint/mustache_lint.php +++ b/mustache_lint/mustache_lint.php @@ -140,6 +140,7 @@ // When we have no example context, parse errors are common because // there are missing variables in the js, thus we ignore them. $ignoreparseerrors = empty($example) ? true : false; + print_message('INFO', 'ESLint reported JavaScript errors'); print_eslint_problems($eslintproblems, $ignoreparseerrors); } @@ -260,8 +261,18 @@ function print_eslint_problems($problems, $ignoreparseerrors) { } else { $severity = 'warning'; } - $message = "ESLint {$severity} [{$problem->ruleId}]: {$problem->message} ( {$problem->source} ), Line: {$problem->line} Column: {$problem->column}"; - print_problem('WARNING', $message); + if ($problem->linesource) { + $message = "ESLint {$severity} [{$problem->ruleId}]: {$problem->message}, Line: {$problem->line} Column: {$problem->column}"; + print_problem('WARNING', $message); + print_problem('WARNING', rtrim($problem->linesource)); + + if ($problem->column !== null) { + print_problem('WARNING', str_pad(' ', $problem->column - 1) . '^'); + } + } else { + $message = "ESLint {$severity} [{$problem->ruleId}]: {$problem->message} ( {$problem->source} ), Line: {$problem->line} Column: {$problem->column}"; + print_problem('WARNING', $message); + } } } diff --git a/tests/1-mustache_lint.bats b/tests/1-mustache_lint.bats index 47babc9d..7f8e4926 100755 --- a/tests/1-mustache_lint.bats +++ b/tests/1-mustache_lint.bats @@ -177,7 +177,11 @@ setup () { # Assert result assert_failure assert_output --partial "lib/templates/js_test.mustache - WARNING: ESLint warning [camelcase]: Identifier 'my_message' is not in camel case" - assert_output --partial "lib/templates/js_test.mustache - WARNING: ESLint warning [no-alert]: Unexpected alert. ( alert(my_message); )" + assert_output --partial "lib/templates/js_test.mustache - WARNING: var my_message = 'Hello World!';" + assert_output --partial "lib/templates/js_test.mustache - WARNING: ^" + assert_output --partial "lib/templates/js_test.mustache - WARNING: ESLint warning [no-alert]: Unexpected alert." + assert_output --partial "lib/templates/js_test.mustache - WARNING: alert(my_message);" + assert_output --partial "lib/templates/js_test.mustache - WARNING: ^" } @test "mustache_lint: Test eslint handles parsing failures safely" { @@ -195,7 +199,10 @@ setup () { # Assert result assert_failure - assert_output --partial "lib/templates/js_token_test.mustache - WARNING: ESLint error []: Parsing error: Unexpected token bar ( var foo bar baz = 'bum'; )" + assert_output --partial "lib/templates/js_token_test.mustache - INFO: ESLint reported JavaScript errors" + assert_output --partial "lib/templates/js_token_test.mustache - WARNING: ESLint error []: Parsing error: Unexpected token bar, Line: 2 Column: 13" + assert_output --partial "lib/templates/js_token_test.mustache - WARNING: var foo bar baz = 'bum';" + assert_output --partial "lib/templates/js_token_test.mustache - WARNING: ^" } @test "mustache_lint: Test eslint runs ok when invoked from any directory" {