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

Customise StyleLint's syntax for a given file type #162

Closed
jackfranklin opened this issue Jan 18, 2021 · 5 comments
Closed

Customise StyleLint's syntax for a given file type #162

jackfranklin opened this issue Jan 18, 2021 · 5 comments
Labels
type: enhancement a new feature that isn't related to rules

Comments

@jackfranklin
Copy link

We use TypeScript files to define web components that use LitHtml. By default Stylelint assumes that TS files use Styelint's css-in-js syntax setting, but that fails to find any of the CSS we have defined in our TypeScript files.

If we could customise the stylelint.syntax setting per file type, this would resolve the problem:

"[typescript]": {
  "stylelint.syntax": "html"
}

Is this something you would consider ? I am happy to attempt the PR myself 👍

@jeddy3
Copy link
Member

jeddy3 commented Jan 19, 2021

This sounds like a good suggestion. It would resolve the problem.

By default Stylelint assumes that TS files use Styelint's css-in-js syntax setting

Yep, that's the underlying issue. Stylelint should detect LitHTML components and set the syntax to "html" automatically. The bug is with postcss-syntax which Stylelint relies on for language detection. Unfortunately, the author of that package is no longer active on GitHub. However, there's work going on in the background to update the package (for PostCSS@8). This work is unlikely to land for a while, though, as it's complicated and not many people are interested in it.

This problem could also be solved by adding an overrides property to the configuration object, like so:

// .stylelintrc.js
{
  "syntax": "css-in-js",
  "rules": {
    "at-rule-no-unknown": true,
  },
  "overrides": [
    {
      "files": ["/lit-elements/*.ts"],
      "syntax": "html",
      "rules": {
         "at-rule-no-unknown": false,
      }
  ]
}

And having this extension respect that configuration.

No one has time to implement the overrides property yet.

Ideally the issue would be fixed in these places but it's more work. By adding your suggestion users can workaround these limitations, i.e. they can configure their CI using the CLI (npx stylelint "components/*.ts" --syntax html), and their VS Code editors using your suggested setting.

Thanks for the offer of a PR. That would be great! The two core maintainers of this package haven't had much time to dip in of late. If you know how to release VS Code extensions, I can give you the keys. Otherwise, I can look into the release process myself.

@jeddy3 jeddy3 added status: ready to implement is ready to be worked on by someone type: enhancement a new feature that isn't related to rules labels Jan 19, 2021
@jackfranklin
Copy link
Author

@jeddy3 thanks for the reply; sorry for my delay in replying.

I'd love to look into the stylelint overrides but I don't know if I have the capacity to do that at the moment. As a stop gap, would you accept a PR to this repo that lets us override the config for the VS Code extension?

@jeddy3
Copy link
Member

jeddy3 commented Feb 3, 2021

As a stop gap, would you accept a PR to this repo that lets us override the config for the VS Code extension?

Yes, absolutely.

The two core maintainers of this package haven't had much time to dip in of late. If you know how to release VS Code extensions, I can give you the keys to do that. Otherwise, I can look into the release process myself once your pull request is merged.

@adalinesimonian
Copy link
Member

adalinesimonian commented Oct 28, 2021

Extension-side, the solution for this is resource-scoped configuration, which is effectively what #271 is about (I might rename that issue). That's going to take quite a bit of refactoring to change the way we work with configuration objects in the extension, so I can't guarantee when that'll be implemented. In the meantime, using overrides is really your best option. The effective equivalent of the configuration in your initial example is:

// .stylelintrc.js or similar
module.exports = {
  // …
  overrides: [
    {
      files: ['**/*.ts', '**/*.tsx'], // what files the additional configuration should apply to
      customSyntax: 'postcss-html', // the syntax module to be used
    }
  ],
}

@adalinesimonian adalinesimonian removed the status: ready to implement is ready to be worked on by someone label Oct 28, 2021
@jeddy3
Copy link
Member

jeddy3 commented Oct 29, 2021

@jackfranklin FYI, the overrides configuration property was added in 14.0.0 of Stylelint.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement a new feature that isn't related to rules
Projects
None yet
Development

No branches or pull requests

3 participants