eslint-plugin-th-rules 1.19.2 → 1.20.0
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/.vscode/settings.json +3 -0
- package/CHANGELOG.md +14 -0
- package/README.md +0 -1
- package/package.json +1 -1
- package/src/index.js +81 -63
- package/docs/rules/styles-in-styles-file.md +0 -141
- package/src/rules/styles-in-styles-file.js +0 -143
package/.vscode/settings.json
CHANGED
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
# [1.20.0](https://github.com/tomerh2001/eslint-plugin-th-rules/compare/v1.19.3...v1.20.0) (2026-01-06)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* **eslint:** :boom: migrated to ESLint flat configs ([0af8a5e](https://github.com/tomerh2001/eslint-plugin-th-rules/commit/0af8a5e49026af3145924363949cdb2978886dbf))
|
|
7
|
+
|
|
8
|
+
## [1.19.3](https://github.com/tomerh2001/eslint-plugin-th-rules/compare/v1.19.2...v1.19.3) (2026-01-06)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* remove styles-in-styles-file rule and update configs ([e6779c2](https://github.com/tomerh2001/eslint-plugin-th-rules/commit/e6779c2d6bf367fc9474c0ded5977b0a03ad159f))
|
|
14
|
+
|
|
1
15
|
## [1.19.2](https://github.com/tomerh2001/eslint-plugin-th-rules/compare/v1.19.1...v1.19.2) (2026-01-05)
|
|
2
16
|
|
|
3
17
|
|
package/README.md
CHANGED
|
@@ -22,7 +22,6 @@ This repository contains custom ESLint rules to enhance code quality and consist
|
|
|
22
22
|
| [no-default-export](docs/rules/no-default-export.md) | Convert unnamed default exports to named default exports based on the file name. | ✅ ⚛️ 🟦 | 🔧 |
|
|
23
23
|
| [no-destructuring](docs/rules/no-destructuring.md) | Disallow destructuring that does not meet certain conditions | ✅ ⚛️ 🟦 | |
|
|
24
24
|
| [schemas-in-schemas-file](docs/rules/schemas-in-schemas-file.md) | Require Zod schema declarations to be placed in a .schemas.ts file | ✅ ⚛️ 🟦 | |
|
|
25
|
-
| [styles-in-styles-file](docs/rules/styles-in-styles-file.md) | Require React-Native StyleSheet.create(...) to be placed in a .styles.ts file | ✅ ⚛️ 🟦 | |
|
|
26
25
|
| [top-level-functions](docs/rules/top-level-functions.md) | Require all top-level functions to be named/regular functions. | ✅ ⚛️ 🟦 | 🔧 |
|
|
27
26
|
| [types-in-dts](docs/rules/types-in-dts.md) | Require TypeScript type declarations (type/interface/enum) to be placed in .d.ts files | ✅ ⚛️ 🟦 | |
|
|
28
27
|
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -2,61 +2,83 @@
|
|
|
2
2
|
/* eslint-disable unicorn/prefer-module */
|
|
3
3
|
|
|
4
4
|
'use strict';
|
|
5
|
+
|
|
5
6
|
const requireIndex = require('requireindex');
|
|
7
|
+
const globals = require('globals');
|
|
8
|
+
const sonarjs = require('eslint-plugin-sonarjs');
|
|
9
|
+
const security = require('eslint-plugin-security');
|
|
10
|
+
const reactPlugin = require('eslint-plugin-react');
|
|
11
|
+
const reactHooks = require('eslint-plugin-react-hooks');
|
|
12
|
+
const tseslint = require('typescript-eslint');
|
|
6
13
|
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
'unicorn/prefer-module': 'warn',
|
|
26
|
-
'unicorn/filename-case': 'off',
|
|
27
|
-
'unicorn/no-array-callback-reference': 'off',
|
|
28
|
-
'import/extensions': 'off',
|
|
29
|
-
'unicorn/no-static-only-class': 'off',
|
|
30
|
-
'unicorn/no-await-expression-member': 'off',
|
|
31
|
-
'new-cap': 'off',
|
|
32
|
-
'no-await-in-loop': 'off',
|
|
33
|
-
'n/file-extension-in-import': 'off',
|
|
34
|
-
'import/no-cycle': 'off',
|
|
35
|
-
camelcase: 'warn',
|
|
36
|
-
'sonarjs/mouse-events-a11y': 'off',
|
|
37
|
-
'sonarjs/no-unstable-nested-components': 'off',
|
|
38
|
-
'unicorn/prefer-global-this': 'off',
|
|
39
|
-
'unicorn/no-thenable': 'off',
|
|
40
|
-
'sonarjs/no-clear-text-protocols': 'off',
|
|
41
|
-
},
|
|
42
|
-
env: {
|
|
43
|
-
node: true,
|
|
44
|
-
es2024: true,
|
|
45
|
-
jest: true,
|
|
14
|
+
const plugin = {
|
|
15
|
+
rules: requireIndex(`${__dirname}/rules`),
|
|
16
|
+
configs: {},
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const baseRecommended = {
|
|
20
|
+
plugins: {
|
|
21
|
+
'th-rules': plugin,
|
|
22
|
+
security,
|
|
23
|
+
react: reactPlugin,
|
|
24
|
+
'react-hooks': reactHooks,
|
|
25
|
+
},
|
|
26
|
+
languageOptions: {
|
|
27
|
+
ecmaVersion: 2024,
|
|
28
|
+
globals: {
|
|
29
|
+
...globals.node,
|
|
30
|
+
...globals.es2024,
|
|
31
|
+
...globals.jest,
|
|
46
32
|
},
|
|
47
33
|
},
|
|
34
|
+
settings: {
|
|
35
|
+
react: {version: 'detect'},
|
|
36
|
+
},
|
|
37
|
+
rules: {
|
|
38
|
+
'th-rules/no-destructuring': 'error',
|
|
39
|
+
'th-rules/no-default-export': 'error',
|
|
40
|
+
'th-rules/no-comments': 'error',
|
|
41
|
+
'th-rules/top-level-functions': 'error',
|
|
42
|
+
'th-rules/schemas-in-schemas-file': 'error',
|
|
43
|
+
'th-rules/types-in-dts': 'error',
|
|
44
|
+
|
|
45
|
+
'unicorn/prefer-module': 'warn',
|
|
46
|
+
'unicorn/filename-case': 'off',
|
|
47
|
+
'unicorn/no-array-callback-reference': 'off',
|
|
48
|
+
'import/extensions': 'off',
|
|
49
|
+
'unicorn/no-static-only-class': 'off',
|
|
50
|
+
'unicorn/no-await-expression-member': 'off',
|
|
51
|
+
'new-cap': 'off',
|
|
52
|
+
'no-await-in-loop': 'off',
|
|
53
|
+
'n/file-extension-in-import': 'off',
|
|
54
|
+
'import/no-cycle': 'off',
|
|
55
|
+
camelcase: 'warn',
|
|
56
|
+
|
|
57
|
+
'sonarjs/mouse-events-a11y': 'off',
|
|
58
|
+
'sonarjs/no-unstable-nested-components': 'off',
|
|
59
|
+
'unicorn/prefer-global-this': 'off',
|
|
60
|
+
'unicorn/no-thenable': 'off',
|
|
61
|
+
'sonarjs/no-clear-text-protocols': 'off',
|
|
62
|
+
},
|
|
48
63
|
};
|
|
49
64
|
|
|
50
|
-
|
|
51
|
-
configs
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
65
|
+
plugin.configs.recommended = [
|
|
66
|
+
sonarjs.configs.recommended,
|
|
67
|
+
security.configs.recommended,
|
|
68
|
+
baseRecommended,
|
|
69
|
+
];
|
|
70
|
+
|
|
71
|
+
plugin.configs['recommended-typescript'] = [
|
|
72
|
+
...plugin.configs.recommended,
|
|
73
|
+
tseslint.configs.strictTypeChecked,
|
|
74
|
+
tseslint.configs.stylisticTypeChecked,
|
|
75
|
+
{
|
|
76
|
+
languageOptions: {
|
|
77
|
+
parserOptions: {
|
|
78
|
+
projectService: true,
|
|
79
|
+
},
|
|
80
|
+
},
|
|
58
81
|
rules: {
|
|
59
|
-
...configs[configName].rules,
|
|
60
82
|
'@typescript-eslint/naming-convention': 'warn',
|
|
61
83
|
'@typescript-eslint/consistent-type-definitions': 'off',
|
|
62
84
|
'@typescript-eslint/no-extraneous-class': 'off',
|
|
@@ -68,22 +90,18 @@ for (const configName of Object.keys(configs)) {
|
|
|
68
90
|
'@typescript-eslint/no-unsafe-return': 'off',
|
|
69
91
|
'@typescript-eslint/no-unsafe-argument': 'off',
|
|
70
92
|
},
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
93
|
+
},
|
|
94
|
+
];
|
|
95
|
+
|
|
96
|
+
plugin.configs['recommended-react'] = [
|
|
97
|
+
...plugin.configs.recommended,
|
|
98
|
+
reactPlugin.configs.flat.recommended,
|
|
99
|
+
reactHooks.configs['recommended-latest'] ?? reactHooks.configs.recommended,
|
|
100
|
+
{
|
|
79
101
|
rules: {
|
|
80
|
-
...configs[configName].rules,
|
|
81
102
|
'n/prefer-global/process': 'off',
|
|
82
103
|
},
|
|
83
|
-
}
|
|
84
|
-
|
|
104
|
+
},
|
|
105
|
+
];
|
|
85
106
|
|
|
86
|
-
module.exports =
|
|
87
|
-
rules: requireIndex(`${__dirname}/rules`),
|
|
88
|
-
configs,
|
|
89
|
-
};
|
|
107
|
+
module.exports = plugin;
|
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
# th-rules/styles-in-styles-file
|
|
2
|
-
|
|
3
|
-
📝 Require React-Native StyleSheet.create(...) to be placed in a .styles.ts file.
|
|
4
|
-
|
|
5
|
-
💼 This rule is enabled in the following configs: ✅ `recommended`, ⚛️ `recommended-react`, 🟦 `recommended-typescript`.
|
|
6
|
-
|
|
7
|
-
<!-- end auto-generated rule header -->
|
|
8
|
-
|
|
9
|
-
This rule enforces that React-Native stylesheet declarations created via `StyleSheet.create(...)` live in a dedicated styles file, typically ending with `.styles.ts`.
|
|
10
|
-
|
|
11
|
-
In practice, this prevents implementation/component files from containing large style objects, and encourages consistent separation of concerns.
|
|
12
|
-
|
|
13
|
-
## Rationale
|
|
14
|
-
|
|
15
|
-
Keeping styles in dedicated files:
|
|
16
|
-
- improves readability of component files by reducing visual noise,
|
|
17
|
-
- encourages reuse and consistency across components,
|
|
18
|
-
- makes style changes easier to review (diffs focus only on styles),
|
|
19
|
-
- standardizes project structure and naming conventions.
|
|
20
|
-
|
|
21
|
-
## What the rule reports
|
|
22
|
-
|
|
23
|
-
The rule reports any `StyleSheet.create(...)` call in files whose names do **not** match one of the allowed suffixes (by default, `.styles.ts`).
|
|
24
|
-
|
|
25
|
-
Optionally, it can also report `StyleSheet.compose(...)` calls.
|
|
26
|
-
|
|
27
|
-
## Examples
|
|
28
|
-
|
|
29
|
-
### ❌ Incorrect
|
|
30
|
-
|
|
31
|
-
```ts
|
|
32
|
-
// ArticleCard.tsx
|
|
33
|
-
import {StyleSheet} from 'react-native';
|
|
34
|
-
|
|
35
|
-
const styles = StyleSheet.create({
|
|
36
|
-
container: {padding: 12},
|
|
37
|
-
});
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
```ts
|
|
41
|
-
// AnyOtherFile.ts
|
|
42
|
-
import {StyleSheet} from 'react-native';
|
|
43
|
-
|
|
44
|
-
export default StyleSheet.create({
|
|
45
|
-
row: {flexDirection: 'row'},
|
|
46
|
-
});
|
|
47
|
-
```
|
|
48
|
-
|
|
49
|
-
### ✅ Correct
|
|
50
|
-
|
|
51
|
-
```ts
|
|
52
|
-
// ArticleCard.styles.ts
|
|
53
|
-
import {StyleSheet} from 'react-native';
|
|
54
|
-
|
|
55
|
-
export const styles = StyleSheet.create({
|
|
56
|
-
container: {padding: 12},
|
|
57
|
-
});
|
|
58
|
-
```
|
|
59
|
-
|
|
60
|
-
```ts
|
|
61
|
-
// ArticleCard.tsx
|
|
62
|
-
import React from 'react';
|
|
63
|
-
import {View} from 'react-native';
|
|
64
|
-
import {styles} from './ArticleCard.styles';
|
|
65
|
-
|
|
66
|
-
export function ArticleCard() {
|
|
67
|
-
return <View style={styles.container} />;
|
|
68
|
-
}
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
### `includeCompose` example
|
|
72
|
-
|
|
73
|
-
When `includeCompose: true`, the following becomes invalid outside a `.styles.ts(x)` file:
|
|
74
|
-
|
|
75
|
-
```ts
|
|
76
|
-
// ArticleCard.tsx
|
|
77
|
-
import {StyleSheet} from 'react-native';
|
|
78
|
-
|
|
79
|
-
const combined = StyleSheet.compose(
|
|
80
|
-
{padding: 12},
|
|
81
|
-
{margin: 8},
|
|
82
|
-
);
|
|
83
|
-
```
|
|
84
|
-
|
|
85
|
-
With the same code moved to `ArticleCard.styles.ts`, it becomes valid.
|
|
86
|
-
|
|
87
|
-
## Options
|
|
88
|
-
|
|
89
|
-
<!-- begin auto-generated rule options list -->
|
|
90
|
-
|
|
91
|
-
| Name | Type |
|
|
92
|
-
| :---------------- | :------- |
|
|
93
|
-
| `allowedSuffixes` | String[] |
|
|
94
|
-
| `includeCompose` | Boolean |
|
|
95
|
-
|
|
96
|
-
<!-- end auto-generated rule options list -->
|
|
97
|
-
|
|
98
|
-
### `allowedSuffixes`
|
|
99
|
-
|
|
100
|
-
An array of filename suffixes that are allowed to contain `StyleSheet.create(...)`.
|
|
101
|
-
|
|
102
|
-
Default:
|
|
103
|
-
- `.styles.ts`
|
|
104
|
-
|
|
105
|
-
Example:
|
|
106
|
-
|
|
107
|
-
```json
|
|
108
|
-
{
|
|
109
|
-
"rules": {
|
|
110
|
-
"th-rules/styles-in-styles-file": ["error", {
|
|
111
|
-
"allowedSuffixes": [".styles.ts", ".native-styles.ts"]
|
|
112
|
-
}]
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
```
|
|
116
|
-
|
|
117
|
-
### `includeCompose`
|
|
118
|
-
|
|
119
|
-
When set to `true`, the rule also enforces the file restriction for `StyleSheet.compose(...)`.
|
|
120
|
-
|
|
121
|
-
Default: `false`
|
|
122
|
-
|
|
123
|
-
Example:
|
|
124
|
-
|
|
125
|
-
```json
|
|
126
|
-
{
|
|
127
|
-
"rules": {
|
|
128
|
-
"th-rules/styles-in-styles-file": ["error", {
|
|
129
|
-
"includeCompose": true
|
|
130
|
-
}]
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
```
|
|
134
|
-
|
|
135
|
-
## Notes
|
|
136
|
-
|
|
137
|
-
- This rule only targets `StyleSheet.create(...)` (and optionally `StyleSheet.compose(...)`). It does not restrict:
|
|
138
|
-
- plain object styles (e.g., `const styles = { ... }`),
|
|
139
|
-
- other styling systems (e.g., styled-components, Tamagui, Emotion),
|
|
140
|
-
- calls to other `StyleSheet.*` helpers (e.g., `flatten`, `hairlineWidth`).
|
|
141
|
-
- The rule is filename-based. If ESLint is invoked with `<input>` (stdin), the rule will treat it as not being an allowed styles file.
|
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
const meta = {
|
|
2
|
-
type: 'problem',
|
|
3
|
-
docs: {
|
|
4
|
-
description: 'Require React-Native StyleSheet.create(...) to be placed in a .styles.ts file',
|
|
5
|
-
category: 'Stylistic Issues',
|
|
6
|
-
recommended: false,
|
|
7
|
-
url: 'https://github.com/tomerh2001/eslint-plugin-th-rules/blob/main/docs/rules/styles-in-styles-file.md',
|
|
8
|
-
},
|
|
9
|
-
schema: [
|
|
10
|
-
{
|
|
11
|
-
type: 'object',
|
|
12
|
-
properties: {
|
|
13
|
-
allowedSuffixes: {
|
|
14
|
-
type: 'array',
|
|
15
|
-
items: {type: 'string', minLength: 1},
|
|
16
|
-
minItems: 1,
|
|
17
|
-
},
|
|
18
|
-
includeCompose: {type: 'boolean'},
|
|
19
|
-
},
|
|
20
|
-
additionalProperties: false,
|
|
21
|
-
},
|
|
22
|
-
],
|
|
23
|
-
messages: {
|
|
24
|
-
moveStyles:
|
|
25
|
-
'React-Native styles must be defined in a dedicated styles file ({{suffixes}}).',
|
|
26
|
-
},
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
function create(context) {
|
|
30
|
-
const options = context.options?.[0] ?? {};
|
|
31
|
-
const allowedSuffixes = options.allowedSuffixes ?? ['.styles.ts'];
|
|
32
|
-
const includeCompose = Boolean(options.includeCompose);
|
|
33
|
-
|
|
34
|
-
function isAllowedStyleFile(filename) {
|
|
35
|
-
if (!filename || filename === '<input>') {
|
|
36
|
-
return false;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
return allowedSuffixes.some(suffix => filename.endsWith(suffix));
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function isStyleSheetMemberCall(node, memberName) {
|
|
43
|
-
const callee = node?.callee;
|
|
44
|
-
if (!callee || callee.type !== 'MemberExpression') {
|
|
45
|
-
return false;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const object = callee.object;
|
|
49
|
-
const property = callee.property;
|
|
50
|
-
|
|
51
|
-
return (
|
|
52
|
-
object?.type === 'Identifier'
|
|
53
|
-
&& object.name === 'StyleSheet'
|
|
54
|
-
&& !callee.computed
|
|
55
|
-
&& property?.type === 'Identifier'
|
|
56
|
-
&& property.name === memberName
|
|
57
|
-
);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Try to infer the “name” of the style object, e.g.:
|
|
62
|
-
* const styles = StyleSheet.create(...) -> "styles"
|
|
63
|
-
* styles = StyleSheet.create(...) -> "styles"
|
|
64
|
-
* exports.styles = StyleSheet.create(...) -> "exports.styles"
|
|
65
|
-
*/
|
|
66
|
-
function getAssignmentTargetName(callNode) {
|
|
67
|
-
const p = callNode.parent;
|
|
68
|
-
|
|
69
|
-
if (p?.type === 'VariableDeclarator') {
|
|
70
|
-
const id = p.id;
|
|
71
|
-
if (id?.type === 'Identifier') {
|
|
72
|
-
return id.name;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
return null;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
if (p?.type === 'AssignmentExpression') {
|
|
79
|
-
const left = p.left;
|
|
80
|
-
|
|
81
|
-
if (left?.type === 'Identifier') {
|
|
82
|
-
return left.name;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
if (left?.type === 'MemberExpression' && !left.computed) {
|
|
86
|
-
const object = left.object;
|
|
87
|
-
const property = left.property;
|
|
88
|
-
|
|
89
|
-
const objectName
|
|
90
|
-
= object?.type === 'Identifier'
|
|
91
|
-
? object.name
|
|
92
|
-
: (object?.type === 'ThisExpression'
|
|
93
|
-
? 'this'
|
|
94
|
-
: null);
|
|
95
|
-
|
|
96
|
-
const propertyName = property?.type === 'Identifier' ? property.name : null;
|
|
97
|
-
|
|
98
|
-
if (objectName && propertyName) {
|
|
99
|
-
return `${objectName}.${propertyName}`;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
return null;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
return null;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
function report(node) {
|
|
110
|
-
const filename = context.getFilename();
|
|
111
|
-
if (isAllowedStyleFile(filename)) {
|
|
112
|
-
return;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
const targetName = getAssignmentTargetName(node);
|
|
116
|
-
const target = targetName ? ` "${targetName}"` : '';
|
|
117
|
-
|
|
118
|
-
context.report({
|
|
119
|
-
node,
|
|
120
|
-
messageId: 'moveStyles',
|
|
121
|
-
data: {
|
|
122
|
-
filename,
|
|
123
|
-
suffixes: allowedSuffixes.join(' or '),
|
|
124
|
-
target,
|
|
125
|
-
},
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
return {
|
|
130
|
-
CallExpression(node) {
|
|
131
|
-
if (isStyleSheetMemberCall(node, 'create')) {
|
|
132
|
-
report(node);
|
|
133
|
-
return;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
if (includeCompose && isStyleSheetMemberCall(node, 'compose')) {
|
|
137
|
-
report(node);
|
|
138
|
-
}
|
|
139
|
-
},
|
|
140
|
-
};
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
module.exports = {meta, create};
|