eslint-plugin-primer-react 6.2.0-rc.09a2b48 → 6.2.0-rc.58af654

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -4,6 +4,8 @@
4
4
 
5
5
  ### Minor Changes
6
6
 
7
+ - [#266](https://github.com/primer/eslint-plugin-primer-react/pull/266) [`90134bc`](https://github.com/primer/eslint-plugin-primer-react/commit/90134bcee073c424e81c53e69632e1518798af93) Thanks [@keithamus](https://github.com/keithamus)! - Add enforce-css-module-default-import rule
8
+
7
9
  - [#258](https://github.com/primer/eslint-plugin-primer-react/pull/258) [`83f29f3`](https://github.com/primer/eslint-plugin-primer-react/commit/83f29f339999b9c21d95167bcc2680c1797cbab6) Thanks [@keithamus](https://github.com/keithamus)! - Add enforce-css-module-identifier-casing rule
8
10
 
9
11
  ## 6.1.6
@@ -0,0 +1,53 @@
1
+ # Enforce CSS Module Default Import (enforce-css-module-default-import)
2
+
3
+ CSS Modules should import only the default import. Avoid named imports which can often conflict with other variables (including the function name of the React component) when importing css modules.
4
+
5
+ Additionally, for consistency among the codebase the default import should be consistently named. This rule allows enforcing the name of the default import, which by default expects it to be named either `classes` or be suffixed `Classes`.
6
+
7
+ ## Rule details
8
+
9
+ This rule disallows the use of any CSS Module property that does not match the desired casing.
10
+
11
+ 👎 Examples of **incorrect** code for this rule:
12
+
13
+ ```jsx
14
+ /* eslint primer-react/enforce-css-module-default-import: "error" */
15
+ import {Button} from '@primer/react'
16
+ import {Button as ButtonClass} from './some.module.css' // oops! Conflict!
17
+ ```
18
+
19
+ ```jsx
20
+ /* eslint primer-react/enforce-css-module-default-import: ["error",{enforceName:"^classes$"}] */
21
+ import {Button} from '@primer/react'
22
+ import classnames from './some.module.css' // This default import should match /^classes$/
23
+ ```
24
+
25
+ 👍 Examples of **correct** code for this rule:
26
+
27
+ ```jsx
28
+ /* eslint primer-react/enforce-css-module-default-import: "error" */
29
+ import {Button} from '@primer/react'
30
+ import classes from './some.module.css'
31
+ ```
32
+
33
+ ```jsx
34
+ /* eslint primer-react/enforce-css-module-default-import: ["error",{enforceName:"^classes$"}] */
35
+ import {Button} from '@primer/react'
36
+ import classes from './some.module.css'
37
+ ```
38
+
39
+ ```jsx
40
+ /* eslint primer-react/enforce-css-module-default-import: ["error",{enforceName:"(^classes$|Classes$)"}] */
41
+ import {Button} from '@primer/react'
42
+ import classes from './some.module.css'
43
+ import sharedClasses from './shared.module.css'
44
+ import utilClasses from './util.module.css'
45
+ ```
46
+
47
+ ## Options
48
+
49
+ - `enforceName` (default: `.*`)
50
+
51
+ By default, the `enforce-css-module-default-import` rule will allow any name for the default export,
52
+ however in the _recommended_ ruleset this is set to `(^classes$|Classes$)` meaning that the default
53
+ export name must either be exactly `classes` or end in `Classes`, for example `sharedClasses`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-primer-react",
3
- "version": "6.2.0-rc.09a2b48",
3
+ "version": "6.2.0-rc.58af654",
4
4
  "description": "ESLint rules for Primer React",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -21,6 +21,7 @@ module.exports = {
21
21
  'primer-react/no-unnecessary-components': 'error',
22
22
  'primer-react/prefer-action-list-item-onselect': 'error',
23
23
  'primer-react/enforce-css-module-identifier-casing': 'error',
24
+ 'primer-react/enforce-css-module-default-import': ['error', {enforceName: '(^classes$|Classes$)'}],
24
25
  },
25
26
  settings: {
26
27
  github: {
package/src/index.js CHANGED
@@ -15,6 +15,7 @@ module.exports = {
15
15
  'no-unnecessary-components': require('./rules/no-unnecessary-components'),
16
16
  'prefer-action-list-item-onselect': require('./rules/prefer-action-list-item-onselect'),
17
17
  'enforce-css-module-identifier-casing': require('./rules/enforce-css-module-identifier-casing'),
18
+ 'enforce-css-module-default-import': require('./rules/enforce-css-module-default-import'),
18
19
  },
19
20
  configs: {
20
21
  recommended: require('./configs/recommended'),
@@ -0,0 +1,88 @@
1
+ const rule = require('../enforce-css-module-default-import')
2
+ const {RuleTester} = require('eslint')
3
+
4
+ const ruleTester = new RuleTester({
5
+ parserOptions: {
6
+ ecmaVersion: 'latest',
7
+ sourceType: 'module',
8
+ ecmaFeatures: {
9
+ jsx: true,
10
+ },
11
+ },
12
+ })
13
+
14
+ ruleTester.run('enforce-css-module-default-import', rule, {
15
+ valid: [
16
+ 'import classes from "a.module.css";',
17
+ 'import {default as classes} from "a.module.css";',
18
+ 'import staticClasses from "a.module.css";',
19
+ 'import fooClasses from "a.module.css";',
20
+ 'import whatever from "a.js";',
21
+ 'import whatever from "a.module.js";',
22
+ 'import whatever from "a.css";',
23
+ 'import {whatever} from "a.js";',
24
+ 'import {whatever} from "a.css";',
25
+ {
26
+ code: 'import classes from "a.module.css";',
27
+ options: [{enforceName: '(?:^classes$|Classes$)'}],
28
+ },
29
+ {
30
+ code: 'import sharedClasses from "a.module.css";',
31
+ options: [{enforceName: '(?:^classes$|Classes$)'}],
32
+ },
33
+ ],
34
+ invalid: [
35
+ {
36
+ code: 'import {foo} from "a.module.css";',
37
+ errors: [
38
+ {
39
+ messageId: 'notDefault',
40
+ },
41
+ ],
42
+ },
43
+ {
44
+ code: 'import _, {foo} from "a.module.css";',
45
+ errors: [
46
+ {
47
+ messageId: 'notDefault',
48
+ },
49
+ ],
50
+ },
51
+ {
52
+ code: 'import foobar from "a.module.css";',
53
+ options: [{enforceName: '(?:^classes$|Classes$)'}],
54
+ errors: [
55
+ {
56
+ messageId: 'badName',
57
+ },
58
+ ],
59
+ },
60
+ {
61
+ code: 'import someclasses from "a.module.css";',
62
+ options: [{enforceName: '(?:^classes$|Classes$)'}],
63
+ errors: [
64
+ {
65
+ messageId: 'badName',
66
+ },
67
+ ],
68
+ },
69
+ {
70
+ code: 'import {default as foobar} from "a.module.css";',
71
+ options: [{enforceName: '(?:^classes$|Classes$)'}],
72
+ errors: [
73
+ {
74
+ messageId: 'badName',
75
+ },
76
+ ],
77
+ },
78
+ {
79
+ code: 'import {default as someclasses} from "a.module.css";',
80
+ options: [{enforceName: '(?:^classes$|Classes$)'}],
81
+ errors: [
82
+ {
83
+ messageId: 'badName',
84
+ },
85
+ ],
86
+ },
87
+ ],
88
+ })
@@ -0,0 +1,62 @@
1
+ module.exports = {
2
+ meta: {
3
+ type: 'suggestion',
4
+ fixable: 'code',
5
+ schema: [
6
+ {
7
+ properties: {
8
+ enforceName: {
9
+ type: 'string',
10
+ },
11
+ },
12
+ },
13
+ ],
14
+ messages: {
15
+ badName: 'This default import should match {{enforceName}}',
16
+ notDefault: 'Class modules should only import the default object.',
17
+ noDefault: 'Class modules should always import default.',
18
+ },
19
+ },
20
+ create(context) {
21
+ const enforceName = new RegExp(context.options[0]?.enforceName || '.*')
22
+ return {
23
+ ['ImportDeclaration>Literal[value=/.module.css$/]']: function (node) {
24
+ node = node.parent
25
+ const defaultSpecifier = node.specifiers.find(spec => spec.type === 'ImportDefaultSpecifier')
26
+ const otherSpecifiers = node.specifiers.filter(spec => spec.type !== 'ImportDefaultSpecifier')
27
+ const asDefaultSpecifier = otherSpecifiers.find(spec => spec.imported?.name === 'default')
28
+ if (!node.specifiers.length) {
29
+ context.report({
30
+ node,
31
+ messageId: 'noDefault',
32
+ })
33
+ } else if (otherSpecifiers.length === 1 && asDefaultSpecifier) {
34
+ if (!enforceName.test(asDefaultSpecifier.local.name)) {
35
+ context.report({
36
+ node,
37
+ messageId: 'badName',
38
+ data: {enforceName},
39
+ })
40
+ }
41
+ } else if (otherSpecifiers.length) {
42
+ context.report({
43
+ node,
44
+ messageId: 'notDefault',
45
+ })
46
+ } else if (!defaultSpecifier) {
47
+ context.report({
48
+ node,
49
+ messageId: 'noDefault',
50
+ })
51
+ }
52
+ if (defaultSpecifier && !enforceName.test(defaultSpecifier.local.name)) {
53
+ context.report({
54
+ node,
55
+ messageId: 'badName',
56
+ data: {enforceName},
57
+ })
58
+ }
59
+ },
60
+ }
61
+ },
62
+ }