tengattack / eslint-plugin-php-markup

A eslint plugin to process PHP markup
8 stars 2 forks source link

PHP "short echo tag" not supported #4

Open fredericgboutin-yapla opened 2 years ago

fredericgboutin-yapla commented 2 years ago

I was trying the plugin and was doing fine except I got the famous "Unexpected token <" error only to find out that my PHP code is using what is call "short echo tags" - i.e. <?= ... ?> - which is not actually supported, see https://github.com/tengattack/eslint-plugin-php-markup/blob/master/lib/index.js#L134

Reference: https://www.php.net/manual/en/language.basic-syntax.phptags.php

fredericgboutin-yapla commented 2 years ago

I think I created this issue a bit too fast. The problem is more specific than that.

The actual regex does SUPPORT the short echo tags. The problem I have specifically is that I have this tag INSIDE a <script> tag.

For example,

<script>
    <?= 'var ok = 123;' ?>
</script>

When I remove this line specifically, the whole file get properly linted even if it has numerous other "short echo tags"🤔

kkmuffme commented 2 years ago

There are some other issues too, that are out of scope for this I think. I just automatically "prepare" the .js file with .sh before running eslint on it in CI. Hacky but it works on our huge code base.

e.g.

# when inline js has a line where the PHP is not quoted and followed by a ; it will not lint the whole file for some reason, so we remove trailing ; after ?> and add a comment to replace it back later on
sed -i 's/\?>;$/?> \/\/ replace-back-trailing-semicolon/g' "$file"

# for some reason eslint completely messes up the file content & thinks the file is all js, if any js function has a js doc
# thus changing all function comments to single asterisk (normal comment) and then replacing it back after eslint
# requires disabling of jsdoc/no-bad-blocks
sed -i 's/\/\*\*$/\/\* replace-back-asterisk/g' "$file"

In a similar vein, you could just replace your short echo tags with regular php tags and replace it back after the run finished.

fredericgboutin-yapla commented 2 years ago

Oh well 😞

I think I begin to see the PROs and CONs of trying to lint javascript in PHP template files. As far as it goes, the current approach doesn't seem to be very reliable. And I mean it without blaming this plugin, eslint-plugin-html or ESLint specifically. It seems that there is so much you can do in this context without the help of the PHP community and the way the code is actually written in phtml.

Maybe there is an alternative I'm not aware of in the PHP ecosystem.

kkmuffme commented 2 years ago

Exactly, there are too many variables in code style, inline HTML/JS,... with PHP. With some pre-processing, this plugin works great though.

You just have to preprocess php/phtml files which will "fix" issues that match your project's specific code file and then you're good. The above .sh works perfectly for any WordPress files (as short echo tags are disallowed for WP)