Skip to content

Commit

Permalink
Merge pull request #283 from stronk7/trim_js_contents
Browse files Browse the repository at this point in the history
Mustache helper now trim JS contents before passing them to ESLint
  • Loading branch information
stronk7 authored Oct 11, 2023
2 parents 615d63d + 1fb0aa6 commit 00fefa2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
5 changes: 4 additions & 1 deletion mustache_lint/js_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ public function __construct($moodleroot) {
* @param Mustache_LambdaHelper $helper Used to render nested mustache variables.
*/
public function add_js($content, Mustache_LambdaHelper $helper) {
$this->js .= $helper->render($content);
// Ensure the JS to be checked doesn't have any trailing whitespace (spaces or tabs)
// because of the position of the {{js}}...{{/js}} tags in the mustache file.
// This will avoid "no-trailing-spaces" false positives from eslint.
$this->js .= trim($helper->render($content), "\t ");
}

/**
Expand Down
2 changes: 2 additions & 0 deletions mustache_lint/mustache_lint.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,10 @@
if ($eslintproblems === false) {
// Not an error situation for now, because we might run in situations
// where npm dependencies including eslint are not installed.
echo "INFO: ESLint did not run" . PHP_EOL;
print_message('INFO', 'ESLint did not run');
} else {
echo "INFO: ESLint did run" . PHP_EOL;
// 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;
Expand Down
31 changes: 31 additions & 0 deletions tests/1-mustache_lint.bats
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ setup () {

# Assert result
assert_success
assert_output --partial "INFO: ESLint did not run"
assert_output --partial "lib/templates/js_test.mustache - INFO: ESLint did not run"
assert_output --partial "No mustache problems found"
}
Expand All @@ -191,6 +192,7 @@ setup () {

# Assert result
assert_failure
assert_output --partial "INFO: ESLint did run"
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); )"
}
Expand All @@ -210,9 +212,35 @@ setup () {

# Assert result
assert_failure
assert_output --partial "INFO: ESLint did run"
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 is immune to the {{#js}}...{{#js}} position in templates" {
create_git_branch MOODLE_403_STABLE v4.3.0

# Calculate moodleroot and localciroot.
localciroot=$PWD
moodleroot=${LOCAL_CI_TESTS_GITDIR}

# Note, we don't have to apply any fixture because there are real cases in core
# which are using {{#js}}...{{/js}} with non-trimmed whitespace around them. So
# just let's use one of them to verify that out js helper workarounds the problem.

# Install npm depends so we have eslint (sourced).
source ${localciroot}/prepare_npm_stuff/prepare_npm_stuff.sh

# Run normally (from moodleroot).
cd ${moodleroot}
ci_run_php mustache_lint/mustache_lint.php --filename="${moodleroot}/mod/data/templates/action_bar.mustache" \
--basename="${moodleroot}" --validator="${localciroot}/node_modules/vnu-jar/build/dist/vnu.jar"

# Assert result.
assert_success
assert_output --partial "INFO: ESLint did run"
refute_output --partial "ESLint error [no-trailing-spaces]"
}

@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 All @@ -232,6 +260,7 @@ setup () {
ci_run_php mustache_lint/mustache_lint.php --filename="${moodleroot}/lib/templates/inplace_editable.mustache" \
--basename="${moodleroot}" --validator="${localciroot}/node_modules/vnu-jar/build/dist/vnu.jar"
assert_success
assert_output --partial "INFO: ESLint did run"
refute_output --partial "INFO: ESLint did not run"
refute_output --partial "was not found when loaded as a Node module"

Expand All @@ -240,6 +269,7 @@ setup () {
ci_run_php mustache_lint/mustache_lint.php --filename="${moodleroot}/lib/templates/inplace_editable.mustache" \
--basename="${moodleroot}" --validator="${localciroot}/node_modules/vnu-jar/build/dist/vnu.jar"
assert_success
assert_output --partial "INFO: ESLint did run"
refute_output --partial "INFO: ESLint did not run"
refute_output --partial "was not found when loaded as a Node module"

Expand All @@ -248,6 +278,7 @@ setup () {
ci_run_php mustache_lint/mustache_lint.php --filename="${moodleroot}/lib/templates/inplace_editable.mustache" \
--basename="${moodleroot}" --validator="${localciroot}/node_modules/vnu-jar/build/dist/vnu.jar"
assert_success
assert_output --partial "INFO: ESLint did run"
refute_output --partial "INFO: ESLint did not run"
refute_output --partial "was not found when loaded as a Node module"
refute_output --partial "${LOCAL_CI_TESTS_CACHEDIR}"
Expand Down

0 comments on commit 00fefa2

Please sign in to comment.