-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
145 lines (132 loc) · 5.21 KB
/
Copy pathindex.js
File metadata and controls
145 lines (132 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import path from 'node:path';
import { isValid } from './is-valid.js';
/**
* Custom ESLint rule: if a file has exactly one named export,
* the filename must match it. Files with 0 or 2+ named exports are ignored.
*
* Understands Angular's dot-separated naming convention:
* seats-position-autofix.service.ts → SeatsPositionAutofixService
* my-component.component.ts → MyComponentComponent
* some-util.ts → SomeUtil / someUtil
*/
/*
How it works:
┌──────────────────────────┐
│ Parse file with ESLint │
└────────────┬─────────────┘
│
┌────────────▼─────────────┐
│ Collect all named │
│ ExportNamedDeclarations │
└────────────┬─────────────┘
│
┌────────────────▼────────────────┐
│ exports.length === 1 ? │
│ │
│ 0 or 2+ ──────► skip (pass) │
│ exactly 1 ──────► continue │
└────────────────┬────────────────┘
│
┌────────────────▼────────────────┐
│ Skip index / spec / test / │
│ stories files │
└────────────────┬────────────────┘
│
┌────────────────▼────────────────┐
│ Normalize filename: │
│ │
│ "my-thing.service.ts" │
│ → strip ".ts" │
│ → split on "." │
│ → ["my-thing", "service"] │
│ → kebab → PascalCase each │
│ → ["MyThing", "Service"] │
│ → join → "MyThingService" │
└────────────────┬────────────────┘
│
┌────────────────▼────────────────┐
│ Case-insensitive comparison │
│ │
│ "mythingservice" === │
│ "mythingservice" → pass ✅ │
│ │
│ "mythingservice" !== │
│ "userapiservice" → error ✗ │
└─────────────────────────────────┘
*/
/** @type {import('eslint').Rule.RuleModule} */
const rule = {
meta: {
type: 'suggestion',
docs: {
description:
'If a file has exactly one named export, the filename must match it',
},
messages: {
mismatch:
'Filename "{{filename}}" does not match the single named export "{{exportName}}".',
},
schema: [
{
type: 'object',
properties: {
ignore: {
type: 'array',
items: { type: 'string' },
},
},
additionalProperties: false,
},
],
},
create(context) {
const namedExports = [];
return {
ExportNamedDeclaration(node) {
if (node.declaration) {
if (node.declaration.id) {
// export function foo() {} / export class Foo {} /
// export interface Foo {} / export type Foo = ...
namedExports.push(node.declaration.id.name);
} else if (node.declaration.declarations) {
// export const foo = ..., bar = ...
for (const decl of node.declaration.declarations) {
if (decl.id.type === 'Identifier') {
namedExports.push(decl.id.name);
}
}
}
}
// export { foo, bar } or export { foo } from './bar'
if (node.specifiers) {
for (const spec of node.specifiers) {
namedExports.push(
spec.exported.type === 'Identifier'
? spec.exported.name
: spec.exported.value,
);
}
}
},
'Program:exit'(programNode) {
if (namedExports.length !== 1) return;
const namedExport = namedExports[0];
const ignore = context.options[0]?.ignore ?? [];
const filename = path.basename(context.filename);
if (ignore.includes(filename)) return;
if (!isValid(filename, namedExport)) {
context.report({
node: programNode,
messageId: 'mismatch',
data: { filename, exportName: namedExport },
});
}
},
};
},
};
export default {
rules: {
'match-named-export': rule,
},
};