Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion mustache_lint/js_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,23 @@ public function run_eslint() {
}

// We should only have problem messages for the one file passed.
return $problems[0]->messages;
$messages = $problems[0]->messages;

foreach ($messages as $problem) {
$problem->linesource = '';
if ($problem->line) {
$lines = explode("\n", $this->js, $problem->line);
if ($lines && count($lines) === $problem->line) {
$problem->linesource = array_pop($lines);
}
}
if (!property_exists($problem, 'source')) {
// Fatal errors do not include a source.
// Fill the entire source line.
$problem->source = $problem->linesource;
}
}

return $messages;
}
}
18 changes: 18 additions & 0 deletions tests/1-mustache_lint.bats
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,24 @@ setup () {
assert_output --partial "lib/templates/js_test.mustache - WARNING: ESLint warning [no-alert]: Unexpected alert. ( alert(my_message); )"
}

@test "mustache_lint: Test eslint handles parsing failures safely" {

# a8c64d6 has eslint in package.json (switch to v3.1.3 when releaseD)
create_git_branch MOODLE_31_STABLE a8c64d6267fd0a2f12435ea75af88eb4de980d6f
git_apply_fixture 31-mustache_lint-js_token_test.patch
export GIT_PREVIOUS_COMMIT=$FIXTURE_HASH_BEFORE
export GIT_COMMIT=$FIXTURE_HASH_AFTER

# Install npm depends so we have eslint
ci_run prepare_npm_stuff/prepare_npm_stuff.sh
# Run with eslint.
ci_run mustache_lint/mustache_lint.sh

# 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'; )"
}

@test "mustache_lint: Test eslint runs ok when invoked from any directory" {

# We need to use recent version here, the default 3.1.3 used for tests (that already had eslint)
Expand Down
34 changes: 34 additions & 0 deletions tests/fixtures/31-mustache_lint-js_token_test.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
From 7c4ad58c7f7a462e39577959a6c4e05a868cc610 Mon Sep 17 00:00:00 2001
From: Andrew Nicols <[email protected]>
Date: Mon, 8 Aug 2022 15:07:36 +0800
Subject: [PATCH 1/1] MDL-12345 mustache: fixture for js parsing failure
detection

---
lib/templates/js_token_test.mustache | 14 ++++++++++++++
1 file changed, 14 insertions(+)
create mode 100644 lib/templates/js_token_test.mustache

diff --git a/lib/templates/js_token_test.mustache b/lib/templates/js_token_test.mustache
new file mode 100644
index 0000000000..022896ea27
--- /dev/null
+++ b/lib/templates/js_token_test.mustache
@@ -0,0 +1,14 @@
+{{!
+ @template core/js_token_test
+
+ Fixture for testing eslint parsing errors.
+
+ Example context (json):
+ {
+ "text": "World!"
+ }
+}}
+<p>Hello {{text}}</p>
+{{#js}}
+ var foo bar baz = 'bum';
+{{/js}}
--
2.37.0