atom.io 0.44.8 → 0.44.10

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.
@@ -0,0 +1,253 @@
1
+ import type { TSESTree } from "@typescript-eslint/utils"
2
+ import { AST_NODE_TYPES, ESLintUtils } from "@typescript-eslint/utils"
3
+ import type {
4
+ InterfaceType,
5
+ Symbol as TsSymbol,
6
+ Type,
7
+ TypeNode,
8
+ } from "typescript"
9
+
10
+ const createRule = ESLintUtils.RuleCreator(
11
+ (name) => `https://atom.io.fyi/docs/eslint-plugin#${name}`,
12
+ )
13
+
14
+ const STATE_FUNCTIONS_WITH_CATCH = [
15
+ `atom`,
16
+ `atomFamily`,
17
+ `selector`,
18
+ `selectorFamily`,
19
+ ]
20
+ const STANDALONE_FUNCTIONS = [`atom`, `selector`]
21
+
22
+ export const exactCatchTypes: ESLintUtils.RuleModule<
23
+ `extraneousErrorTypes` | `invalidCatchProperty` | `missingCatchProperty`,
24
+ [],
25
+ unknown,
26
+ ESLintUtils.RuleListener
27
+ > = createRule({
28
+ name: `catch-constructor-type`,
29
+ meta: {
30
+ type: `problem`,
31
+ docs: {
32
+ description: `Ensures that when an error type (E) is provided to an atom, the 'catch' property is set and all constructors in it are assignable to E.`,
33
+ },
34
+ messages: {
35
+ missingCatchProperty:
36
+ `This {{functionName}} was provided the error type \`{{errorTypeName}}\` ` +
37
+ `but the required 'catch' property is missing from its options. ` +
38
+ `Either remove \`{{errorTypeName}}\`, or add \`catch: [{{errorTypeName}}]\` to the options object.`,
39
+ invalidCatchProperty:
40
+ `This {{functionName}} was provided a catch array containing the class \`{{constructorName}}\`. ` +
41
+ `However, that class is not represented in the {{functionName}}'s error type, \`{{errorTypeName}}\`. ` +
42
+ `As a result, it might catch errors that the {{functionName}} is not designed to handle. ` +
43
+ `Either include \`{{constructorName}}\` in the {{functionName}}'s error type, or remove it from the 'catch' array.`,
44
+ extraneousErrorTypes:
45
+ `This {{functionName}} was provided an error type including the class {{errorTypeName}}, ` +
46
+ `but its 'catch' property doesn't include a constructor for that class. ` +
47
+ `Either include a constructor for {{errorTypeName}} in the 'catch' array, or remove {{errorTypeName}} from the error type.`,
48
+ },
49
+ schema: [],
50
+ },
51
+ defaultOptions: [],
52
+ create(context) {
53
+ const parserServices = ESLintUtils.getParserServices(context)
54
+ const checker = parserServices.program.getTypeChecker()
55
+
56
+ return {
57
+ CallExpression(node) {
58
+ const {
59
+ callee,
60
+ typeArguments: directTypeArguments,
61
+ arguments: callArguments,
62
+ } = node
63
+
64
+ // Check if the function call is one of the targeted state functions
65
+ let functionName: string | null = null
66
+ if (callee.type === AST_NODE_TYPES.Identifier) {
67
+ if (STATE_FUNCTIONS_WITH_CATCH.includes(callee.name)) {
68
+ functionName = callee.name
69
+ }
70
+ } else if (callee.type === AST_NODE_TYPES.MemberExpression) {
71
+ if (
72
+ callee.property.type === AST_NODE_TYPES.Identifier &&
73
+ STATE_FUNCTIONS_WITH_CATCH.includes(callee.property.name)
74
+ ) {
75
+ functionName = callee.property.name
76
+ }
77
+ }
78
+
79
+ if (!functionName) return
80
+
81
+ // Where do the type arguments come from?
82
+ let typeArguments: TSESTree.TSTypeParameterInstantiation | undefined
83
+ if (directTypeArguments) {
84
+ typeArguments = directTypeArguments
85
+ } else {
86
+ const parent = node.parent
87
+ if (
88
+ parent?.type === AST_NODE_TYPES.VariableDeclarator &&
89
+ parent.init === node
90
+ ) {
91
+ // Check if the VariableDeclarator has an id with a TypeAnnotation
92
+ const declaratorId = parent.id
93
+ if (declaratorId.type === AST_NODE_TYPES.Identifier) {
94
+ // Check for 'const myAtom: AtomToken<string> = ...'
95
+ const typeAnnotation = declaratorId.typeAnnotation?.typeAnnotation
96
+ if (
97
+ typeAnnotation &&
98
+ `typeArguments` in typeAnnotation &&
99
+ typeAnnotation.typeArguments
100
+ ) {
101
+ typeArguments = typeAnnotation.typeArguments
102
+ }
103
+ }
104
+ }
105
+ }
106
+
107
+ const optionsObject = callArguments[0]
108
+
109
+ if (optionsObject?.type !== AST_NODE_TYPES.ObjectExpression) return
110
+
111
+ const isStandalone = STANDALONE_FUNCTIONS.includes(functionName)
112
+
113
+ const errorTypeNode = typeArguments
114
+ ? isStandalone
115
+ ? typeArguments.params[1]
116
+ : typeArguments.params[2]
117
+ : undefined
118
+
119
+ let catchProperty: TSESTree.Property | undefined
120
+ optionsObject.properties.forEach((property) => {
121
+ if (property.type === AST_NODE_TYPES.Property) {
122
+ if (
123
+ (property.key.type === AST_NODE_TYPES.Identifier &&
124
+ property.key.name === `catch`) ||
125
+ (property.key.type === AST_NODE_TYPES.Literal &&
126
+ property.key.value === `catch`)
127
+ ) {
128
+ catchProperty = property
129
+ }
130
+ }
131
+ })
132
+
133
+ if (!errorTypeNode) {
134
+ return
135
+ }
136
+
137
+ const typeNode = parserServices.esTreeNodeToTSNodeMap.get(
138
+ errorTypeNode,
139
+ ) as TypeNode
140
+ // Get the TypeScript Type object for E
141
+ const errorTypeTs = checker.getTypeFromTypeNode(typeNode)
142
+ const errorTypeName = checker.typeToString(errorTypeTs)
143
+
144
+ if (!catchProperty) {
145
+ context.report({
146
+ node: optionsObject,
147
+ messageId: `missingCatchProperty`,
148
+ data: { functionName, errorTypeName },
149
+ })
150
+ return
151
+ }
152
+
153
+ // --- New Validation: Check Constructor Types ---
154
+ const catchArray = catchProperty.value
155
+ if (catchArray.type !== AST_NODE_TYPES.ArrayExpression) {
156
+ // We only check array literals (e.g., [Ctor1, Ctor2])
157
+ return
158
+ }
159
+
160
+ // 3. Collect all acceptable nominal symbols from E
161
+ const acceptableErrorSymbols: TsSymbol[] = []
162
+
163
+ // Check if E is a Union Type
164
+ if (errorTypeTs.isUnion()) {
165
+ // Add the symbol of every member of the union (e.g., Symbol(SpecialError), Symbol(FancyError))
166
+ for (const memberType of errorTypeTs.types) {
167
+ const symbol = memberType.getSymbol()
168
+ if (symbol) {
169
+ acceptableErrorSymbols.push(symbol)
170
+ }
171
+ }
172
+ } else {
173
+ // E is a single type, add its symbol
174
+ const symbol = errorTypeTs.getSymbol()
175
+ if (symbol) {
176
+ acceptableErrorSymbols.push(symbol)
177
+ }
178
+ }
179
+
180
+ if (catchArray.elements.length === 0) {
181
+ context.report({
182
+ node: catchProperty,
183
+ messageId: `missingCatchProperty`,
184
+ data: { functionName, errorTypeName },
185
+ })
186
+ return
187
+ }
188
+ const errorSymbolsToRepresent = new Set(acceptableErrorSymbols)
189
+
190
+ // Iterate over each constructor reference in the 'catch' array
191
+ for (const element of catchArray.elements) {
192
+ if (!element || element.type !== AST_NODE_TYPES.Identifier) {
193
+ // Only check simple identifier references (e.g., [ClientError])
194
+ continue
195
+ }
196
+
197
+ // Get the type of the constructor identifier (e.g., the Type of 'Error')
198
+ const constructorTsNode =
199
+ parserServices.esTreeNodeToTSNodeMap.get(element)
200
+ const constructorType = checker.getTypeAtLocation(constructorTsNode)
201
+ const constructorName = element.name
202
+
203
+ // console.log(`constructorName`, constructorName)
204
+
205
+ // Extract the instance type from the constructor type.
206
+ // e.g., turn 'typeof ClientError' into 'ClientError'
207
+ let instanceType: Type | undefined
208
+ if (
209
+ (constructorType as InterfaceType).getConstructSignatures().length >
210
+ 0
211
+ ) {
212
+ // Get the return type of the constructor signature
213
+ const signature = (
214
+ constructorType as InterfaceType
215
+ ).getConstructSignatures()[0]
216
+ instanceType = signature.getReturnType()
217
+ }
218
+
219
+ // If we couldn't get the instance type, skip the check
220
+
221
+ const constructorInstanceSymbol = instanceType?.getSymbol()
222
+ if (!constructorInstanceSymbol) continue
223
+
224
+ // Check for symbol identity
225
+ if (acceptableErrorSymbols.includes(constructorInstanceSymbol)) {
226
+ errorSymbolsToRepresent.delete(constructorInstanceSymbol)
227
+ } else {
228
+ context.report({
229
+ node: element,
230
+ messageId: `invalidCatchProperty`,
231
+ data: {
232
+ functionName,
233
+ constructorName: constructorName,
234
+ errorTypeName: errorTypeName,
235
+ },
236
+ })
237
+ }
238
+ }
239
+
240
+ for (const errorSymbol of errorSymbolsToRepresent) {
241
+ context.report({
242
+ node: catchProperty,
243
+ messageId: `extraneousErrorTypes`,
244
+ data: {
245
+ errorTypeName: checker.symbolToString(errorSymbol),
246
+ functionName,
247
+ },
248
+ })
249
+ }
250
+ },
251
+ }
252
+ },
253
+ })
@@ -1,5 +1,5 @@
1
1
  /* eslint-disable @typescript-eslint/switch-exhaustiveness-check */
2
- import { ESLintUtils } from "@typescript-eslint/utils"
2
+ import { AST_NODE_TYPES, ESLintUtils } from "@typescript-eslint/utils"
3
3
 
4
4
  const createRule = ESLintUtils.RuleCreator(
5
5
  (name) => `https://atom.io.fyi/docs/eslint-plugin#${name}`,
@@ -14,9 +14,15 @@ const STATE_FUNCTIONS = [
14
14
  `selectorFamily`,
15
15
  ]
16
16
 
17
+ type Options = [
18
+ {
19
+ permitAnnotation?: boolean
20
+ },
21
+ ]
22
+
17
23
  export const explicitStateTypes: ESLintUtils.RuleModule<
18
- `noTypeArgument`,
19
- [],
24
+ `noTypeArgument` | `noTypeArgumentOrAnnotation`,
25
+ Options,
20
26
  unknown,
21
27
  ESLintUtils.RuleListener
22
28
  > = createRule({
@@ -28,40 +34,87 @@ export const explicitStateTypes: ESLintUtils.RuleModule<
28
34
  },
29
35
  messages: {
30
36
  noTypeArgument: `State declarations must have generic type arguments directly passed to them.`,
37
+ noTypeArgumentOrAnnotation: `State declarations must have generic type arguments directly passed to them, or a top-level type annotation.`,
31
38
  },
32
- schema: [], // no options
39
+ schema: [
40
+ {
41
+ type: `object`,
42
+ properties: {
43
+ permitAnnotation: {
44
+ type: `boolean`,
45
+ default: false,
46
+ },
47
+ },
48
+ additionalProperties: false,
49
+ },
50
+ ],
33
51
  },
34
- defaultOptions: [],
52
+ defaultOptions: [
53
+ {
54
+ permitAnnotation: false,
55
+ },
56
+ ],
35
57
  create(context) {
58
+ const options = context.options[0]
59
+ const permitAnnotation = options?.permitAnnotation ?? false
60
+
36
61
  return {
37
62
  CallExpression(node) {
38
- const { callee } = node
63
+ const callee = node.callee
64
+
39
65
  switch (callee.type) {
40
- case `Identifier`: {
41
- if (STATE_FUNCTIONS.includes(callee.name)) {
42
- if (!node.typeArguments) {
43
- context.report({
44
- node,
45
- messageId: `noTypeArgument`,
46
- })
47
- }
66
+ case `Identifier`:
67
+ if (STATE_FUNCTIONS.includes(callee.name) === false) {
68
+ return
48
69
  }
49
70
  break
50
- }
51
- case `MemberExpression`: {
71
+ case `MemberExpression`:
52
72
  if (
53
- callee.property.type === `Identifier` &&
54
- STATE_FUNCTIONS.includes(callee.property.name)
73
+ (callee.property.type === `Identifier` &&
74
+ STATE_FUNCTIONS.includes(callee.property.name)) === false
55
75
  ) {
56
- if (!node.typeArguments) {
57
- context.report({
58
- node,
59
- messageId: `noTypeArgument`,
60
- })
61
- }
76
+ return
77
+ }
78
+ break
79
+ default:
80
+ return
81
+ }
82
+
83
+ // Check for the *required* generic type argument first
84
+ if (node.typeArguments) {
85
+ return // Generic type argument is present, no error
86
+ }
87
+
88
+ // If generic arguments are missing, check if the top-level annotation exception is enabled AND present
89
+ if (permitAnnotation) {
90
+ let hasAnnotation = false
91
+ // Check if the CallExpression is the initializer of a variable declarator
92
+ const parent = node.parent
93
+ if (
94
+ parent?.type === AST_NODE_TYPES.VariableDeclarator &&
95
+ parent.init === node
96
+ ) {
97
+ // Check if the VariableDeclarator has an id with a TypeAnnotation
98
+ const declaratorId = parent.id
99
+ if (declaratorId.type === AST_NODE_TYPES.Identifier) {
100
+ // Check for 'const myAtom: AtomToken<string> = ...'
101
+ hasAnnotation = Boolean(declaratorId.typeAnnotation)
62
102
  }
63
103
  }
104
+ if (hasAnnotation) {
105
+ return // Exception met: type annotation is on the variable declaration
106
+ }
107
+ context.report({
108
+ node,
109
+ messageId: `noTypeArgumentOrAnnotation`,
110
+ })
111
+ return
64
112
  }
113
+
114
+ context.report({
115
+ node,
116
+ messageId: `noTypeArgument`,
117
+ })
65
118
  },
66
119
  }
67
120
  },
@@ -1 +1,2 @@
1
+ export * from "./exact-catch-types"
1
2
  export * from "./explicit-state-types"
@@ -2,10 +2,7 @@ import type { AtomFamilyToken } from "atom.io"
2
2
  import { atomFamily } from "atom.io"
3
3
  import type { Canonical } from "atom.io/json"
4
4
 
5
- export const mutexAtoms: AtomFamilyToken<boolean, Canonical> = atomFamily<
6
- boolean,
7
- Canonical
8
- >({
5
+ export const mutexAtoms: AtomFamilyToken<boolean, Canonical> = atomFamily({
9
6
  key: `mutex`,
10
7
  default: false,
11
8
  })
@@ -20,9 +20,7 @@ export type RoomSocketInterface<RoomNames extends string> = {
20
20
  [deleteRoom: `deleteRoom:${string}`]: () => void
21
21
  }
22
22
 
23
- export const roomKeysAtom: MutableAtomToken<UList<string>> = mutableAtom<
24
- UList<string>
25
- >({
23
+ export const roomKeysAtom: MutableAtomToken<UList<string>> = mutableAtom({
26
24
  key: `roomIndex`,
27
25
  class: UList,
28
26
  })
@@ -45,7 +43,7 @@ export const usersInRooms: JoinToken<`room`, RoomKey, `user`, UserKey, `1:n`> =
45
43
  export const usersInMyRoomView: ReadonlyPureSelectorFamilyToken<
46
44
  MutableAtomToken<UList<RoomKey>>[],
47
45
  UserKey
48
- > = selectorFamily<MutableAtomToken<UList<RoomKey>>[], UserKey>({
46
+ > = selectorFamily({
49
47
  key: `usersInMyRoomView`,
50
48
  get:
51
49
  (myUsername) =>
@@ -49,12 +49,7 @@ export const redactorAtoms: RegularAtomFamilyToken<
49
49
  occlude: (updates: TransactionSubEvent[]) => TransactionSubEvent[]
50
50
  },
51
51
  UserKey
52
- > = atomFamily<
53
- {
54
- occlude: (updates: TransactionSubEvent[]) => TransactionSubEvent[]
55
- },
56
- UserKey
57
- >({
52
+ > = atomFamily({
58
53
  key: `redactor`,
59
54
  default: { occlude: (updates) => updates },
60
55
  })
@@ -66,7 +61,7 @@ export type ContinuitySyncTransactionUpdate = Pick<
66
61
  export const unacknowledgedUpdatesAtoms: RegularAtomFamilyToken<
67
62
  ContinuitySyncTransactionUpdate[],
68
63
  UserKey
69
- > = atomFamily<ContinuitySyncTransactionUpdate[], UserKey>({
64
+ > = atomFamily({
70
65
  key: `unacknowledgedUpdates`,
71
66
  default: () => [],
72
67
  })
@@ -20,20 +20,16 @@ export type SocketSystemHierarchy = Hierarchy<
20
20
  >
21
21
 
22
22
  export const socketAtoms: RegularAtomFamilyToken<Socket | null, SocketKey> =
23
- atomFamily<Socket | null, SocketKey>({
23
+ atomFamily({
24
24
  key: `sockets`,
25
25
  default: null,
26
26
  })
27
27
 
28
- export const socketKeysAtom: MutableAtomToken<UList<SocketKey>> = mutableAtom<
29
- UList<SocketKey>
30
- >({
28
+ export const socketKeysAtom: MutableAtomToken<UList<SocketKey>> = mutableAtom({
31
29
  key: `socketsIndex`,
32
30
  class: UList,
33
31
  })
34
- export const userKeysAtom: MutableAtomToken<UList<UserKey>> = mutableAtom<
35
- UList<UserKey>
36
- >({
32
+ export const userKeysAtom: MutableAtomToken<UList<UserKey>> = mutableAtom({
37
33
  key: `usersIndex`,
38
34
  class: UList,
39
35
  })
@@ -52,7 +48,7 @@ export const usersOfSockets: JoinToken<
52
48
  })
53
49
 
54
50
  export const selfListSelectors: PureSelectorFamilyToken<UserKey[], UserKey> =
55
- selectorFamily<UserKey[], UserKey>({
51
+ selectorFamily({
56
52
  key: `selfList`,
57
53
  get: (userId) => () => [userId],
58
54
  })
@@ -1,13 +0,0 @@
1
- //#region rolldown:runtime
2
- var __defProp = Object.defineProperty;
3
- var __export = (all) => {
4
- let target = {};
5
- for (var name in all) __defProp(target, name, {
6
- get: all[name],
7
- enumerable: true
8
- });
9
- return target;
10
- };
11
-
12
- //#endregion
13
- export { __export as t };