Skip to content

Commit f21e090

Browse files
authored
fix: add type tests (#16)
* fix: add type tests * match formatting to Prettier 3.4
1 parent 33fb3b8 commit f21e090

14 files changed

+86
-37
lines changed

.github/PULL_REQUEST_TEMPLATE.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#### Prerequisites checklist
88

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

1111
<!--
1212
Please ensure your pull request is ready:

.github/workflows/ci.yml

+15
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,21 @@ jobs:
4545
run: npm install
4646
- name: Run tests
4747
run: npm run test
48+
test_types:
49+
name: Test Types
50+
runs-on: ubuntu-latest
51+
steps:
52+
- uses: actions/checkout@v4
53+
- name: Setup Node.js
54+
uses: actions/setup-node@v4
55+
with:
56+
node-version: "lts/*"
57+
- name: Install dependencies
58+
run: npm install
59+
- name: Build
60+
run: npm run build
61+
- name: Check Types
62+
run: npm run test:types
4863
jsr_test:
4964
name: Verify JSR Publish
5065
runs-on: ubuntu-latest

docs/rules/no-duplicate-imports.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@ If you aren't concerned with duplicate `@import` rules, you can safely disable t
3838

3939
## Prior Art
4040

41-
- [`no-duplicate-at-import-rules`](https://stylelint.io/user-guide/rules/no-duplicate-at-import-rules)
41+
- [`no-duplicate-at-import-rules`](https://stylelint.io/user-guide/rules/no-duplicate-at-import-rules)

docs/rules/no-empty-blocks.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,5 @@ If you aren't concerned with empty blocks, you can safely disable this rule.
5151

5252
## Prior Art
5353

54-
- [empty-rules](https://github.com/CSSLint/csslint/wiki/Disallow-empty-rules)
55-
- [`block-no-empty`](https://stylelint.io/user-guide/rules/block-no-empty)
54+
- [empty-rules](https://github.com/CSSLint/csslint/wiki/Disallow-empty-rules)
55+
- [`block-no-empty`](https://stylelint.io/user-guide/rules/block-no-empty)

docs/rules/no-invalid-at-rules.md

+13-13
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ Disallow invalid at-rules.
66

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

9-
- `@import`
10-
- `@media`
11-
- `@font-face`
12-
- `@keyframes`
13-
- `@supports`
14-
- `@namespace`
15-
- `@page`
16-
- `@charset`
9+
- `@import`
10+
- `@media`
11+
- `@font-face`
12+
- `@keyframes`
13+
- `@supports`
14+
- `@namespace`
15+
- `@page`
16+
- `@charset`
1717

1818
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:
1919

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

4141
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:
4242

43-
- An unknown at-rule
44-
- An invalid prelude for a known at-rule
45-
- An unknown descriptor for a known at-rule
46-
- An invalid descriptor value for a known at-rule
43+
- An unknown at-rule
44+
- An invalid prelude for a known at-rule
45+
- An unknown descriptor for a known at-rule
46+
- An invalid descriptor value for a known at-rule
4747

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

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

7878
## Prior Art
7979

80-
- [`at-rule-no-unknown`](https://stylelint.io/user-guide/rules/at-rule-no-unknown)
80+
- [`at-rule-no-unknown`](https://stylelint.io/user-guide/rules/at-rule-no-unknown)

docs/rules/no-invalid-properties.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,6 @@ If you aren't concerned with invalid properties, then you can safely disable thi
4848

4949
## Prior Art
5050

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

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

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@
5757
"fmt": "prettier --write .",
5858
"fmt:check": "prettier --check .",
5959
"test": "mocha tests/**/*.js",
60-
"test:coverage": "c8 npm test"
60+
"test:coverage": "c8 npm test",
61+
"test:types": "tsc -p tests/types/tsconfig.json"
6162
},
6263
"keywords": [
6364
"eslint",
@@ -84,7 +85,7 @@
8485
"lint-staged": "^15.2.7",
8586
"mdast-util-from-markdown": "^2.0.2",
8687
"mocha": "^10.4.0",
87-
"prettier": "^3.3.2",
88+
"prettier": "^3.4.1",
8889
"rollup": "^4.16.2",
8990
"rollup-plugin-copy": "^3.5.0",
9091
"typescript": "^5.4.5",

src/index.js

+15-12
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,23 @@ const plugin = {
3232
"no-invalid-at-rules": noInvalidAtRules,
3333
"no-invalid-properties": noInvalidProperties,
3434
},
35-
configs: {},
36-
};
37-
38-
Object.assign(plugin.configs, {
39-
recommended: {
40-
plugins: { css: plugin },
41-
rules: {
42-
"css/no-empty-blocks": "error",
43-
"css/no-duplicate-imports": "error",
44-
"css/no-invalid-at-rules": "error",
45-
"css/no-invalid-properties": "error",
35+
configs: {
36+
recommended: {
37+
plugins: {},
38+
rules: /** @type {const} */ ({
39+
"css/no-empty-blocks": "error",
40+
"css/no-duplicate-imports": "error",
41+
"css/no-invalid-at-rules": "error",
42+
"css/no-invalid-properties": "error",
43+
}),
4644
},
4745
},
48-
});
46+
};
47+
48+
// eslint-disable-next-line no-lone-blocks -- The block syntax { ... } ensures that TypeScript does not get confused about the type of `plugin`.
49+
{
50+
plugin.configs.recommended.plugins.css = plugin;
51+
}
4952

5053
export default plugin;
5154
export { CSSLanguage, CSSSourceCode };

src/rules/no-duplicate-imports.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
export default {
77
meta: {
8-
type: "problem",
8+
type: /** @type {const} */ ("problem"),
99

1010
docs: {
1111
description: "Disallow duplicate @import rules",

src/rules/no-empty-blocks.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
export default {
77
meta: {
8-
type: "problem",
8+
type: /** @type {const} */ ("problem"),
99

1010
docs: {
1111
description: "Disallow empty blocks",

src/rules/no-invalid-at-rules.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function extractMetaDataFromError(error) {
4444

4545
export default {
4646
meta: {
47-
type: "problem",
47+
type: /** @type {const} */ ("problem"),
4848

4949
docs: {
5050
description: "Disallow invalid at-rules",

src/rules/no-invalid-properties.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { isSyntaxMatchError } from "../util.js";
1616

1717
export default {
1818
meta: {
19-
type: "problem",
19+
type: /** @type {const} */ ("problem"),
2020

2121
docs: {
2222
description: "Disallow invalid properties",

tests/types/tsconfig.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"noEmit": true,
5+
"rootDir": "../..",
6+
"strict": true
7+
},
8+
"files": ["../../dist/esm/index.d.ts", "types.test.ts"]
9+
}

tests/types/types.test.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import css from "@eslint/css";
2+
import { ESLint } from "eslint";
3+
4+
css satisfies ESLint.Plugin;
5+
css.meta.name satisfies string;
6+
css.meta.version satisfies string;
7+
8+
// Check that these languages are defined:
9+
css.languages.css satisfies object;
10+
11+
// Check that `plugins` in the recommended config is defined:
12+
css.configs.recommended.plugins satisfies object;
13+
14+
{
15+
type RecommendedRuleName = keyof typeof css.configs.recommended.rules;
16+
type RuleName = `css/${keyof typeof css.rules}`;
17+
type AssertAllNamesIn<T1 extends T2, T2> = never;
18+
19+
// Check that all recommended rule names match the names of existing rules in this plugin.
20+
null as AssertAllNamesIn<RecommendedRuleName, RuleName>;
21+
}

0 commit comments

Comments
 (0)