0.8.1 • Published 5 months ago

@eslint/css v0.8.1

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
5 months ago

ESLint CSS Language Plugin

Overview

This package contains a plugin that allows you to natively lint CSS files using ESLint.

Important: This plugin requires ESLint v9.6.0 or higher and you must be using the new configuration system.

Installation

For Node.js and compatible runtimes:

npm install @eslint/css -D
# or
yarn add @eslint/css -D
# or
pnpm install @eslint/css -D
# or
bun install @eslint/css -D

For Deno (experimental):

deno add jsr:@eslint/css

Configurations

Configuration NameDescription
recommendedEnables all recommended rules.

In your eslint.config.js file, import @eslint/css and include the recommended config:

// eslint.config.js
import { defineConfig } from "eslint/config";
import css from "@eslint/css";

export default defineConfig([
	// lint CSS files
	{
		files: ["**/*.css"],
		language: "css/css",
		plugins: { css },
		extends: ["css/recommended"],
	},

	// your other configs here
]);

Rules

Rule NameDescriptionRecommended
no-duplicate-importsDisallow duplicate @import rulesyes
no-empty-blocksDisallow empty blocksyes
no-importantDisallow !important flagsyes
no-invalid-at-rulesDisallow invalid at-rulesyes
no-invalid-propertiesDisallow invalid propertiesyes
prefer-logical-propertiesEnforce the use of logical propertiesno
use-baselineEnforce the use of baseline featuresyes
use-layersRequire use of layersno

Note: This plugin does not provide formatting rules. We recommend using a source code formatter such as Prettier for that purpose.

In order to individually configure a rule in your eslint.config.js file, import @eslint/css and configure each rule with a prefix:

// eslint.config.js
import { defineConfig } from "eslint/config";
import css from "@eslint/css";

export default defineConfig([
	{
		files: ["**/*.css"],
		plugins: {
			css,
		},
		language: "css/css",
		rules: {
			"css/no-empty-blocks": "error",
		},
	},
]);

You can individually config, disable, and enable rules in CSS using comments, such as:

/* eslint css/no-empty-blocks: error */

/* eslint-disable css/no-empty-blocks -- this one is ok */
a {
}
/* eslint-enable css/no-empty-blocks */

b { /* eslint-disable-line css/no-empty-blocks */
}

/* eslint-disable-next-line css/no-empty-blocks */
em {
}

Languages

Language NameDescription
cssParse CSS stylesheets.

In order to individually configure a language in your eslint.config.js file, import @eslint/css and configure a language:

// eslint.config.js
import { defineConfig } from "eslint/config";
import css from "@eslint/css";

export default defineConfig([
	{
		files: ["**/*.css"],
		plugins: {
			css,
		},
		language: "css/css",
		rules: {
			"css/no-empty-blocks": "error",
		},
	},
]);

Tolerant Mode

By default, the CSS parser runs in strict mode, which reports all parsing errors. If you'd like to allow recoverable parsing errors (those that the browser automatically fixes on its own), you can set the tolerant option to true:

// eslint.config.js
import { defineConfig } from "eslint/config";
import css from "@eslint/css";

export default defineConfig([
	{
		files: ["**/*.css"],
		plugins: {
			css,
		},
		language: "css/css",
		languageOptions: {
			tolerant: true,
		},
		rules: {
			"css/no-empty-blocks": "error",
		},
	},
]);

Setting tolerant to true is necessary if you are using custom syntax, such as PostCSS plugins, that aren't part of the standard CSS syntax.

Configuring Custom Syntax

The CSS lexer comes prebuilt with a set of known syntax for CSS that is used in rules like no-invalid-properties to validate CSS code. While this works for most cases, there may be cases when you want to define your own extensions to CSS, and this can be done using the customSyntax language option.

The customSyntax option is an object that uses the CSSTree format for defining custom syntax, which allows you to specify at-rules, properties, and some types. For example, suppose you'd like to define a custom at-rule that looks like this:

@my-at-rule "hello world!";

You can configure that syntax as follows:

// eslint.config.js
import { defineConfig } from "eslint/config";
import css from "@eslint/css";

export default defineConfig([
	{
		files: ["**/*.css"],
		plugins: {
			css,
		},
		language: "css/css",
		languageOptions: {
			customSyntax: {
				atrules: {
					"my-at-rule": {
						prelude: "<string>",
					},
				},
			},
		},
		rules: {
			"css/no-empty-blocks": "error",
		},
	},
]);

Configuring Tailwind Syntax

Tailwind specifies some extensions to CSS that will otherwise be flagged as invalid by the rules in this plugin. You can configure most of the custom syntax for Tailwind using the builtin tailwindSyntax object, like this:

// eslint.config.js
import { defineConfig } from "eslint/config";
import css from "@eslint/css";
import { tailwindSyntax } from "@eslint/css/syntax";

export default defineConfig([
	{
		files: ["**/*.css"],
		plugins: {
			css,
		},
		language: "css/css",
		languageOptions: {
			customSyntax: tailwindSyntax,
		},
		rules: {
			"css/no-empty-blocks": "error",
		},
	},
]);

Note: The Tailwind syntax doesn't currently provide for the theme() function. This is a limitation of CSSTree that we hope will be resolved soon.

Editor and IDE Setup

Visual Studio Code

First, ensure you have the ESLint plugin installed.

Then, edit eslint.validate in your settings.json file to include css:

{
	"eslint.validate": ["css"]
}

JetBrains WebStorm

For any JetBrains WebStorm, configure the ESLint scope to include css, such as:

**/*.{js,ts,jsx,tsx,cjs,cts,mjs,mts,html,vue,css}

License

Apache 2.0

Sponsors

The following companies, organizations, and individuals support ESLint's ongoing maintenance and development. Become a Sponsor to get your logo on our READMEs and website.