eslint-plugin-th-rules 1.16.0 → 1.17.1
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 +14 -0
- package/README.md +8 -7
- package/bun.lockb +0 -0
- package/docs/rules/styles-in-styles-file.md +142 -0
- package/package.json +1 -1
- package/src/index.js +2 -0
- package/src/rules/styles-in-styles-file.js +143 -0
- package/src/rules/types-in-dts.js +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
## [1.17.1](https://github.com/tomerh2001/eslint-plugin-th-rules/compare/v1.17.0...v1.17.1) (2026-01-05)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* improve style file validation messages and simplify code structure ([5c165b4](https://github.com/tomerh2001/eslint-plugin-th-rules/commit/5c165b4fcbc103df752e7c56f988c725dae6a62c))
|
|
7
|
+
|
|
8
|
+
# [1.17.0](https://github.com/tomerh2001/eslint-plugin-th-rules/compare/v1.16.0...v1.17.0) (2026-01-05)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Features
|
|
12
|
+
|
|
13
|
+
* add 'styles-in-styles-file' rule to enforce React-Native styles in dedicated files ([eca8af0](https://github.com/tomerh2001/eslint-plugin-th-rules/commit/eca8af0850aa197a35fb47cf08a9f04839a6a3cb))
|
|
14
|
+
|
|
1
15
|
# [1.16.0](https://github.com/tomerh2001/eslint-plugin-th-rules/compare/v1.15.6...v1.16.0) (2026-01-05)
|
|
2
16
|
|
|
3
17
|
|
package/README.md
CHANGED
|
@@ -14,13 +14,14 @@ This repository contains custom ESLint rules to enhance code quality and consist
|
|
|
14
14
|
✅ Set in the `recommended` configuration.\
|
|
15
15
|
🔧 Automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).
|
|
16
16
|
|
|
17
|
-
| Name
|
|
18
|
-
|
|
|
19
|
-
| [no-comments](docs/rules/no-comments.md)
|
|
20
|
-
| [no-default-export](docs/rules/no-default-export.md)
|
|
21
|
-
| [no-destructuring](docs/rules/no-destructuring.md)
|
|
22
|
-
| [
|
|
23
|
-
| [
|
|
17
|
+
| Name | Description | 💼 | 🔧 |
|
|
18
|
+
| :----------------------------------------------------------- | :---------------------------------------------------------------------------------------- | :--------------------------------------------------------------- | :- |
|
|
19
|
+
| [no-comments](docs/rules/no-comments.md) | Disallow comments except for specified allowed patterns. | ✅ ![badge-recommended-react][] ![badge-recommended-typescript][] | 🔧 |
|
|
20
|
+
| [no-default-export](docs/rules/no-default-export.md) | Convert unnamed default exports to named default exports based on the file name. | ✅ ![badge-recommended-react][] ![badge-recommended-typescript][] | 🔧 |
|
|
21
|
+
| [no-destructuring](docs/rules/no-destructuring.md) | Disallow destructuring that does not meet certain conditions | ✅ ![badge-recommended-react][] ![badge-recommended-typescript][] | |
|
|
22
|
+
| [styles-in-styles-file](docs/rules/styles-in-styles-file.md) | Require React-Native StyleSheet.create(...) to be placed in a .styles.ts/.styles.tsx file | ✅ ![badge-recommended-react][] ![badge-recommended-typescript][] | |
|
|
23
|
+
| [top-level-functions](docs/rules/top-level-functions.md) | Require all top-level functions to be named/regular functions. | ✅ ![badge-recommended-react][] ![badge-recommended-typescript][] | 🔧 |
|
|
24
|
+
| [types-in-dts](docs/rules/types-in-dts.md) | Require TypeScript type declarations (type/interface/enum) to be placed in .d.ts files | ✅ ![badge-recommended-react][] ![badge-recommended-typescript][] | |
|
|
24
25
|
|
|
25
26
|
<!-- end auto-generated rules list -->
|
|
26
27
|
|
package/bun.lockb
CHANGED
|
Binary file
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
<pre>
|
|
2
|
+
# Require React-Native StyleSheet.create(...) to be placed in a .styles.ts/.styles.tsx file (`th-rules/styles-in-styles-file`)
|
|
3
|
+
|
|
4
|
+
💼 This rule is enabled in the following configs: ✅ `recommended`, `recommended-react`, `recommended-typescript`.
|
|
5
|
+
|
|
6
|
+
<!-- end auto-generated rule header -->
|
|
7
|
+
|
|
8
|
+
This rule enforces that React-Native stylesheet declarations created via `StyleSheet.create(...)` live in a dedicated styles file, typically ending with `.styles.ts` or `.styles.tsx`.
|
|
9
|
+
|
|
10
|
+
In practice, this prevents implementation/component files from containing large style objects, and encourages consistent separation of concerns.
|
|
11
|
+
|
|
12
|
+
## Rationale
|
|
13
|
+
|
|
14
|
+
Keeping styles in dedicated files:
|
|
15
|
+
- improves readability of component files by reducing visual noise,
|
|
16
|
+
- encourages reuse and consistency across components,
|
|
17
|
+
- makes style changes easier to review (diffs focus only on styles),
|
|
18
|
+
- standardizes project structure and naming conventions.
|
|
19
|
+
|
|
20
|
+
## What the rule reports
|
|
21
|
+
|
|
22
|
+
The rule reports any `StyleSheet.create(...)` call in files whose names do **not** match one of the allowed suffixes (by default, `.styles.ts` and `.styles.tsx`).
|
|
23
|
+
|
|
24
|
+
Optionally, it can also report `StyleSheet.compose(...)` calls.
|
|
25
|
+
|
|
26
|
+
## Examples
|
|
27
|
+
|
|
28
|
+
### ❌ Incorrect
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
// ArticleCard.tsx
|
|
32
|
+
import {StyleSheet} from 'react-native';
|
|
33
|
+
|
|
34
|
+
const styles = StyleSheet.create({
|
|
35
|
+
container: {padding: 12},
|
|
36
|
+
});
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
// AnyOtherFile.ts
|
|
41
|
+
import {StyleSheet} from 'react-native';
|
|
42
|
+
|
|
43
|
+
export default StyleSheet.create({
|
|
44
|
+
row: {flexDirection: 'row'},
|
|
45
|
+
});
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### ✅ Correct
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
// ArticleCard.styles.ts
|
|
52
|
+
import {StyleSheet} from 'react-native';
|
|
53
|
+
|
|
54
|
+
export const styles = StyleSheet.create({
|
|
55
|
+
container: {padding: 12},
|
|
56
|
+
});
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
// ArticleCard.tsx
|
|
61
|
+
import React from 'react';
|
|
62
|
+
import {View} from 'react-native';
|
|
63
|
+
import {styles} from './ArticleCard.styles';
|
|
64
|
+
|
|
65
|
+
export function ArticleCard() {
|
|
66
|
+
return <View style={styles.container} />;
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### `includeCompose` example
|
|
71
|
+
|
|
72
|
+
When `includeCompose: true`, the following becomes invalid outside a `.styles.ts(x)` file:
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
// ArticleCard.tsx
|
|
76
|
+
import {StyleSheet} from 'react-native';
|
|
77
|
+
|
|
78
|
+
const combined = StyleSheet.compose(
|
|
79
|
+
{padding: 12},
|
|
80
|
+
{margin: 8},
|
|
81
|
+
);
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
With the same code moved to `ArticleCard.styles.ts`, it becomes valid.
|
|
85
|
+
|
|
86
|
+
## Options
|
|
87
|
+
|
|
88
|
+
<!-- begin auto-generated rule options list -->
|
|
89
|
+
|
|
90
|
+
| Name | Type |
|
|
91
|
+
| :---------------- | :------- |
|
|
92
|
+
| `allowedSuffixes` | String[] |
|
|
93
|
+
| `includeCompose` | Boolean |
|
|
94
|
+
|
|
95
|
+
<!-- end auto-generated rule options list -->
|
|
96
|
+
|
|
97
|
+
### `allowedSuffixes`
|
|
98
|
+
|
|
99
|
+
An array of filename suffixes that are allowed to contain `StyleSheet.create(...)`.
|
|
100
|
+
|
|
101
|
+
Default:
|
|
102
|
+
- `.styles.ts`
|
|
103
|
+
- `.styles.tsx`
|
|
104
|
+
|
|
105
|
+
Example:
|
|
106
|
+
|
|
107
|
+
```json
|
|
108
|
+
{
|
|
109
|
+
"rules": {
|
|
110
|
+
"th-rules/styles-in-styles-file": ["error", {
|
|
111
|
+
"allowedSuffixes": [".styles.ts", ".styles.tsx", ".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.
|
|
142
|
+
</pre>
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -19,6 +19,7 @@ const configs = {
|
|
|
19
19
|
'th-rules/no-default-export': 'error',
|
|
20
20
|
'th-rules/no-comments': 'error',
|
|
21
21
|
'th-rules/top-level-functions': 'error',
|
|
22
|
+
'th-rules/styles-in-styles-file': 'error',
|
|
22
23
|
'th-rules/types-in-dts': 'error',
|
|
23
24
|
'unicorn/prefer-module': 'warn',
|
|
24
25
|
'unicorn/filename-case': 'off',
|
|
@@ -73,6 +74,7 @@ for (const configName of Object.keys(configs)) {
|
|
|
73
74
|
'plugin:react/recommended',
|
|
74
75
|
'plugin:react-hooks/recommended',
|
|
75
76
|
...configs[configName].extends,
|
|
77
|
+
|
|
76
78
|
],
|
|
77
79
|
rules: {
|
|
78
80
|
...configs[configName].rules,
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
const meta = {
|
|
2
|
+
type: 'problem',
|
|
3
|
+
docs: {
|
|
4
|
+
description: 'Require React-Native StyleSheet.create(...) to be placed in a .styles.ts/.styles.tsx 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{{target}} must be defined in a dedicated styles file ({{suffixes}}). Current file: "{{filename}}".',
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
function create(context) {
|
|
30
|
+
const options = context.options?.[0] ?? {};
|
|
31
|
+
const allowedSuffixes = options.allowedSuffixes ?? ['.styles.ts', '.styles.tsx'];
|
|
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};
|