BenoitZugmeyer / eslint-plugin-html

An ESLint plugin to extract and lint scripts from HTML files.
ISC License
430 stars 51 forks source link

Lint HTML attributes by creating a virtual <script> element #87

Closed loicbourgois closed 2 years ago

loicbourgois commented 6 years ago

This should fix https://github.com/BenoitZugmeyer/eslint-plugin-html/issues/61

Before linting, the file is parsed. If an attributes matching /^on[a-zA-Z]+$/ is found, then its value is added to a virtual <script> element at the end of the file.

A problem remains : if there is an error in an attribute, the line number reported will not be accurate.

BenoitZugmeyer commented 6 years ago

Thank you for your work, but the approach is wrong here.

The main issue comes from modifiying the input source code before the plugin can extract its content. Because of this, the plugin will not be able to associate correct line/column numbers and fix ranges to errors reported by ESLint. This is the purpose of the TransformableString object: record changes made to the input code and get correct original positions after linting the modified code.

It would be better to modify the extract module to emit the script contained in onxxx attributes, so those scripts would be handled to verifyWithSharedScopes. This way, you don't have to generate hacky script tags based on the ESLint rules, or parse the HTML twice.

I'm not sure if this feature can be implemented cleanly in the first place, but I'm open to suggestions.