Skip to content

Commit

Permalink
fix: add type tests (#16)
Browse files Browse the repository at this point in the history
* fix: add type tests

* match formatting to Prettier 3.4
  • Loading branch information
fasttime authored Nov 27, 2024
1 parent 33fb3b8 commit f21e090
Show file tree
Hide file tree
Showing 14 changed files with 86 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#### Prerequisites checklist

- [ ] I have read the [contributing guidelines](https://github.com/eslint/eslint/blob/HEAD/CONTRIBUTING.md).
- [ ] I have read the [contributing guidelines](https://github.com/eslint/eslint/blob/HEAD/CONTRIBUTING.md).

<!--
Please ensure your pull request is ready:
Expand Down
15 changes: 15 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@ jobs:
run: npm install
- name: Run tests
run: npm run test
test_types:
name: Test Types
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: "lts/*"
- name: Install dependencies
run: npm install
- name: Build
run: npm run build
- name: Check Types
run: npm run test:types
jsr_test:
name: Verify JSR Publish
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion docs/rules/no-duplicate-imports.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ If you aren't concerned with duplicate `@import` rules, you can safely disable t

## Prior Art

- [`no-duplicate-at-import-rules`](https://stylelint.io/user-guide/rules/no-duplicate-at-import-rules)
- [`no-duplicate-at-import-rules`](https://stylelint.io/user-guide/rules/no-duplicate-at-import-rules)
4 changes: 2 additions & 2 deletions docs/rules/no-empty-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ If you aren't concerned with empty blocks, you can safely disable this rule.

## Prior Art

- [empty-rules](https://github.com/CSSLint/csslint/wiki/Disallow-empty-rules)
- [`block-no-empty`](https://stylelint.io/user-guide/rules/block-no-empty)
- [empty-rules](https://github.com/CSSLint/csslint/wiki/Disallow-empty-rules)
- [`block-no-empty`](https://stylelint.io/user-guide/rules/block-no-empty)
26 changes: 13 additions & 13 deletions docs/rules/no-invalid-at-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ Disallow invalid at-rules.

CSS contains a number of at-rules, each beginning with a `@`, that perform various operations. Some common at-rules include:

- `@import`
- `@media`
- `@font-face`
- `@keyframes`
- `@supports`
- `@namespace`
- `@page`
- `@charset`
- `@import`
- `@media`
- `@font-face`
- `@keyframes`
- `@supports`
- `@namespace`
- `@page`
- `@charset`

It's important to use a known at-rule because unknown at-rules cause the browser to ignore the entire block, including any rules contained within. For example:

Expand All @@ -40,10 +40,10 @@ Here, `--main-bg-color` is the prelude for `@property` while `syntax`, `inherits

This rule warns when it finds a CSS at-rule that is unknown or invalid according to the CSS specification. As such, the rule warns for the following problems:

- An unknown at-rule
- An invalid prelude for a known at-rule
- An unknown descriptor for a known at-rule
- An invalid descriptor value for a known at-rule
- An unknown at-rule
- An invalid prelude for a known at-rule
- An unknown descriptor for a known at-rule
- An invalid descriptor value for a known at-rule

The at-rule data is provided via the [CSSTree](https://github.com/csstree/csstree) project.

Expand Down Expand Up @@ -77,4 +77,4 @@ If you are purposely using at-rules that aren't part of the CSS specification, t

## Prior Art

- [`at-rule-no-unknown`](https://stylelint.io/user-guide/rules/at-rule-no-unknown)
- [`at-rule-no-unknown`](https://stylelint.io/user-guide/rules/at-rule-no-unknown)
4 changes: 2 additions & 2 deletions docs/rules/no-invalid-properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ If you aren't concerned with invalid properties, then you can safely disable thi

## Prior Art

- [`declaration-property-value-no-unknown`](https://stylelint.io/user-guide/rules/declaration-property-value-no-unknown/)
- [`declaration-property-value-no-unknown`](https://stylelint.io/user-guide/rules/declaration-property-value-no-unknown/)

- [`property-no-unknown`](https://stylelint.io/user-guide/rules/property-no-unknown)
- [`property-no-unknown`](https://stylelint.io/user-guide/rules/property-no-unknown)
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@
"fmt": "prettier --write .",
"fmt:check": "prettier --check .",
"test": "mocha tests/**/*.js",
"test:coverage": "c8 npm test"
"test:coverage": "c8 npm test",
"test:types": "tsc -p tests/types/tsconfig.json"
},
"keywords": [
"eslint",
Expand All @@ -84,7 +85,7 @@
"lint-staged": "^15.2.7",
"mdast-util-from-markdown": "^2.0.2",
"mocha": "^10.4.0",
"prettier": "^3.3.2",
"prettier": "^3.4.1",
"rollup": "^4.16.2",
"rollup-plugin-copy": "^3.5.0",
"typescript": "^5.4.5",
Expand Down
27 changes: 15 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,23 @@ const plugin = {
"no-invalid-at-rules": noInvalidAtRules,
"no-invalid-properties": noInvalidProperties,
},
configs: {},
};

Object.assign(plugin.configs, {
recommended: {
plugins: { css: plugin },
rules: {
"css/no-empty-blocks": "error",
"css/no-duplicate-imports": "error",
"css/no-invalid-at-rules": "error",
"css/no-invalid-properties": "error",
configs: {
recommended: {
plugins: {},
rules: /** @type {const} */ ({
"css/no-empty-blocks": "error",
"css/no-duplicate-imports": "error",
"css/no-invalid-at-rules": "error",
"css/no-invalid-properties": "error",
}),
},
},
});
};

// eslint-disable-next-line no-lone-blocks -- The block syntax { ... } ensures that TypeScript does not get confused about the type of `plugin`.
{
plugin.configs.recommended.plugins.css = plugin;
}

export default plugin;
export { CSSLanguage, CSSSourceCode };
2 changes: 1 addition & 1 deletion src/rules/no-duplicate-imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

export default {
meta: {
type: "problem",
type: /** @type {const} */ ("problem"),

docs: {
description: "Disallow duplicate @import rules",
Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-empty-blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

export default {
meta: {
type: "problem",
type: /** @type {const} */ ("problem"),

docs: {
description: "Disallow empty blocks",
Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-invalid-at-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function extractMetaDataFromError(error) {

export default {
meta: {
type: "problem",
type: /** @type {const} */ ("problem"),

docs: {
description: "Disallow invalid at-rules",
Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-invalid-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { isSyntaxMatchError } from "../util.js";

export default {
meta: {
type: "problem",
type: /** @type {const} */ ("problem"),

docs: {
description: "Disallow invalid properties",
Expand Down
9 changes: 9 additions & 0 deletions tests/types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"noEmit": true,
"rootDir": "../..",
"strict": true
},
"files": ["../../dist/esm/index.d.ts", "types.test.ts"]
}
21 changes: 21 additions & 0 deletions tests/types/types.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import css from "@eslint/css";
import { ESLint } from "eslint";

css satisfies ESLint.Plugin;
css.meta.name satisfies string;
css.meta.version satisfies string;

// Check that these languages are defined:
css.languages.css satisfies object;

// Check that `plugins` in the recommended config is defined:
css.configs.recommended.plugins satisfies object;

{
type RecommendedRuleName = keyof typeof css.configs.recommended.rules;
type RuleName = `css/${keyof typeof css.rules}`;
type AssertAllNamesIn<T1 extends T2, T2> = never;

// Check that all recommended rule names match the names of existing rules in this plugin.
null as AssertAllNamesIn<RecommendedRuleName, RuleName>;
}

0 comments on commit f21e090

Please sign in to comment.