Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP "short echo tag" not supported #4

Open
fredericgboutin-yapla opened this issue Dec 3, 2021 · 4 comments
Open

PHP "short echo tag" not supported #4

fredericgboutin-yapla opened this issue Dec 3, 2021 · 4 comments

Comments

@fredericgboutin-yapla
Copy link

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
Copy link
Author

fredericgboutin-yapla commented Dec 3, 2021

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
Copy link

kkmuffme commented Dec 4, 2021

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
Copy link
Author

fredericgboutin-yapla commented Dec 6, 2021

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
Copy link

kkmuffme commented Dec 7, 2021

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants