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

Feature request: use this with HTML in Javascript template literals #196

Closed
RobertAKARobin opened this issue Jun 20, 2024 · 5 comments
Closed
Assignees
Labels
enhancement New feature or request

Comments

@RobertAKARobin
Copy link
Contributor

RobertAKARobin commented Jun 20, 2024

The /*html*//html template literal syntax is used by several libraries, like Lit:

const myTemplate = /*html*/`
<div>
  <h1>Hello, world!</h1>
</div>
`;

document.body.innerHTML = myTemplate;

It would be nice if html-eslint could be run on Javascript template literals like this.

See also https://github.com/pushqrdx/vscode-inline-html

@yeonjuan yeonjuan added the enhancement New feature or request label Jun 21, 2024
@yeonjuan
Copy link
Owner

@RobertAKARobin Agree! Thanks for the suggestion. I can't think of a way to implement this off the top of my head, but it seems like a useful feature.

@jpradelle
Copy link

It would be awesome for Lit webcomponents linting. I didn't find any project doing this for eslint.

@yeonjuan
Copy link
Owner

yeonjuan commented Nov 6, 2024

I'm working on this 👍

@yeonjuan
Copy link
Owner

yeonjuan commented Dec 1, 2024

Hi @RobertAKARobin, @jpradelle I'm happy to announce that this feature has been implemented and deployed in 0.29.0.
How to use

  1. Updates both @html-eslint/parser and @html-eslint/eslint-plugin version to 0.29.0.
npm install -D @html-eslint/eslint-plugin@0.29.0 @html-eslint/parser@0.29.0
  1. Edit your eslint configuration file
import eslintHTML from "@html-eslint/eslint-plugin";
import eslintHTMLParser from "@html-eslint/parser";

export default [
  {
    "plugins": {
      "@html-eslint": eslintHTML
    },
    "rules": {
      // Specifies the @html-eslint rules to apply to Template Literal.
      "@html-eslint/no-inline-styles": 1,
      "@html-eslint/indent": 1,
     }
  },
  {
    "files": ["**/*.html"],
    "languageOptions": {
      parser: eslintHTMLParser,
    },
    "plugins": {
      "@html-eslint": eslintHTML,
    },
    "rules": {
      // Specifies the @html-eslint rule to apply to HTML files.
     "@html-eslint/no-inline-styles": 1
    }
},
]
  1. Now you can see that this plugin linting the html syntax inside the template literal.
// TaggedTemplateExpression
html`
<div style="${style}">
    ^^^^^^^^^^^^^^^// Unexpected usage of inline style

</div>`;

// TemplateLiteral
const code = /* html */`
<div style="${style}">
    ^^^^^^^^^^^^^^^// Unexpected usage of inline style

</div>`
  1. If you want to specify that linting should be done with keywords other than html, you can utilize the settings option.
 {
    "plugins": {
      "@html-eslint": eslintHTML
    },
    settings: {
        html: {
          templateLiterals: {
               // default options
               tags: ["^html$"],
               comments: ["^\\s*html\\s*$"],
          }
        }
    },
}

@yeonjuan yeonjuan closed this as completed Dec 1, 2024
@jpradelle
Copy link

Awesome, thanks a lot ! Works great for a lot of rules.

But I have an issue with indent rule: #241

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

No branches or pull requests

3 participants