Skip to content

Commit

Permalink
MDLSITE-6748 mustachelint: Add information about eslint failures
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Sep 27, 2022
1 parent 74f95b0 commit c79c408
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
15 changes: 13 additions & 2 deletions mustache_lint/mustache_lint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}
}
}

Expand Down
11 changes: 9 additions & 2 deletions tests/1-mustache_lint.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -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" {
Expand Down

0 comments on commit c79c408

Please sign in to comment.