Giter VIP home page Giter VIP logo

eslint-plugin-solid's Introduction

Solid ESLint Extension

Solid ESLint Plugin

npm version GitHub package version ESLint peer dependency CI

This package contains Solid-specific linting rules for ESLint. It can ease Solid's learning curve by finding and fixing problems around Solid's reactivity system, and can migrate some React patterns to Solid code.

It's approaching a 1.0.0 release, and it's well tested and should be helpful in Solid projects today.

Installation

Install eslint and eslint-plugin-solid locally.

npm install --save-dev eslint eslint-plugin-solid
# or
pnpm add --save-dev eslint eslint-plugin-solid
yarn add --dev eslint eslint-plugin-solid

# optional, to create an ESLint config file
npx eslint --init
# or
pnpm eslint --init
yarn eslint --init

If you're using VSCode, you'll want to install the ESLint extension. You're encouraged to enable auto-fixing problems on save by adding the following to your settings.json file.

{
  "editor.codeActionsOnSave": {
    "source.fixAll": true
  }
}

If you're using Vite, you may want to install vite-plugin-eslint.

You may also want to check out eslint-plugin-jsx-a11y, which provides useful rules for writing accessible HTML.

Configuration

Use the "plugin:solid/recommended" configuration to get reasonable defaults as shown below.

{
  "plugins": ["solid"],
  "extends": ["eslint:recommended", "plugin:solid/recommended"]
}

TypeScript

If you're using TypeScript, use the "plugin:solid/typescript" configuration instead. This disables some features that overlap with type checking.

{
  "parser": "@typescript-eslint/parser",
  "plugins": ["solid"],
  "extends": ["eslint:recommended", "plugin:solid/typescript"]
}

Manual Configuration

If you don't want to use a preset, you can configure rules individually. Add the "solid" plugin, enable JSX with the parser options (or use the equivalent options for @typescript-eslint/parser or @babel/eslint-parser), and configure the rules you would like to use. Some rules have additional options you can set.

{
  "plugins": ["solid"],
  "parserOptions": {
    "ecmaFeatures": {
      "jsx": true
    }
  },
  "rules": {
    "solid/reactivity": "warn",
    "solid/no-destructure": "warn",
    "solid/jsx-no-undef": "error"
  }
}

Flat Configuration

ESLint's new configuration system, Flat Configuration, is available starting in ESLint v8.23.0. To use it, create an eslint.config.js file at the root of your project, instead of .eslintrc.* and/or .eslintignore.

import js from "@eslint/js";
import solid from "eslint-plugin-solid/configs/recommended";

export default [
  js.configs.recommended, // replaces eslint:recommended
  solid,
];

For TypeScript:

import js from "@eslint/js";
import solid from "eslint-plugin-solid/configs/typescript";
import * as tsParser from "@typescript-eslint/parser";

export default [
  js.configs.recommended,
  {
    files: ["**/*.{ts,tsx}"],
    ...solid,
    languageOptions: {
      parser: tsParser,
      parserOptions: {
        project: "tsconfig.json",
      },
    },
  },
];

These configurations do not configure global variables in ESLint. You can do this yourself manually or with a package like globals by creating a configuration with a languageOptions.globals object. We recommend setting up global variables for Browser APIs as well as at least ES2015.

Note for the ESLint VSCode Extension: Enable the "Use Flat Config" setting for your workspace to enable Flat Config support.

Flat configs are also available as plugin.configs['flat/recommended'] and plugin.configs['flat/typescript'], after using import plugin from 'eslint-plugin-solid'.

Rules

โœ”: Enabled in the recommended configuration.

๐Ÿ”ง: Fixable with eslint --fix/IDE auto-fix.

โœ” ๐Ÿ”ง Rule Description
โœ” ๐Ÿ”ง solid/components-return-once Disallow early returns in components. Solid components only run once, and so conditionals should be inside JSX.
โœ” ๐Ÿ”ง solid/event-handlers Enforce naming DOM element event handlers consistently and prevent Solid's analysis from misunderstanding whether a prop should be an event handler.
โœ” ๐Ÿ”ง solid/imports Enforce consistent imports from "solid-js", "solid-js/web", and "solid-js/store".
โœ” solid/jsx-no-duplicate-props Disallow passing the same prop twice in JSX.
โœ” solid/jsx-no-script-url Disallow javascript: URLs.
โœ” ๐Ÿ”ง solid/jsx-no-undef Disallow references to undefined variables in JSX. Handles custom directives.
โœ” solid/jsx-uses-vars Prevent variables used in JSX from being marked as unused.
solid/no-array-handlers Disallow usage of type-unsafe event handlers.
โœ” ๐Ÿ”ง solid/no-destructure Disallow destructuring props. In Solid, props must be used with property accesses (props.foo) to preserve reactivity. This rule only tracks destructuring in the parameter list.
โœ” ๐Ÿ”ง solid/no-innerhtml Disallow usage of the innerHTML attribute, which can often lead to security vulnerabilities.
solid/no-proxy-apis Disallow usage of APIs that use ES6 Proxies, only to target environments that don't support them.
โœ” ๐Ÿ”ง solid/no-react-deps Disallow usage of dependency arrays in createEffect and createMemo.
โœ” ๐Ÿ”ง solid/no-react-specific-props Disallow usage of React-specific className/htmlFor props, which were deprecated in v1.4.0.
โœ” solid/no-unknown-namespaces Enforce using only Solid-specific namespaced attribute names (i.e. 'on:' in <div on:click={...} />).
๐Ÿ”ง solid/prefer-classlist Enforce using the classlist prop over importing a classnames helper. The classlist prop accepts an object { [class: string]: boolean } just like classnames.
โœ” ๐Ÿ”ง solid/prefer-for Enforce using Solid's <For /> component for mapping an array to JSX elements.
๐Ÿ”ง solid/prefer-show Enforce using Solid's <Show /> component for conditionally showing content. Solid's compiler covers this case, so it's a stylistic rule only.
โœ” solid/reactivity Enforce that reactivity (props, signals, memos, etc.) is properly used, so changes in those values will be tracked and update the view as expected.
โœ” ๐Ÿ”ง solid/self-closing-comp Disallow extra closing tags for components without children.
โœ” ๐Ÿ”ง solid/style-prop Require CSS properties in the style prop to be valid and kebab-cased (ex. 'font-size'), not camel-cased (ex. 'fontSize') like in React, and that property values with dimensions are strings, not numbers with implicit 'px' units.

Troubleshooting

The rules in this plugin provide sensible guidelines as well as possible, but there may be times where you know better than the rule and want to ignore a warning. To do that, add a comment like the following:

// eslint-disable-next-line solid/reactivity
const [editedValue, setEditedValue] = createSignal(props.value);

Please note: there may also be times where a rule correctly warns about a subtle problem, even if it looks like a false positive at first. With solid/reactivity, please look at the reactivity docs before deciding to disable the rule.

When in doubt, feel free to file an issue.

Versioning

Pre-1.0.0, the rules and the recommended and typescript configuations will be stable across patch (0.0.x) versions, but may change across minor (0.x) versions. If you want to pin a minor version, use a tilde in your package.json.

- "eslint-plugin-solid": "^0.13.2"
+ "eslint-plugin-solid": "~0.13.2"

eslint-plugin-solid's People

Contributors

joshwilsonvu avatar nix6839 avatar mitsunee avatar aadito123 avatar dependabot[bot] avatar macarie avatar ulrichstark avatar liaodehui1 avatar youssefm avatar voliva avatar prospectpyxis avatar lukahartwig avatar jfrere avatar creativetechguy avatar hatulapro avatar davedbase avatar someaspy avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.