-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
347 lines (317 loc) · 11.2 KB
/
index.js
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
function isModuleExports(node) {
if (!node) return false;
if (!node?.object || !node?.property) return false;
if (node.object.name !== "module" || node.property.name !== "exports") return false;
return true;
}
function isDefaultExport(node) {
return node?.type === "ExportDefaultDeclaration";
}
function isObjectWithProperties(node) {
if (!node) return false;
const {
properties,
type,
} = node;
if (type !== "ObjectExpression") return false;
if (!properties || Object.keys(properties).length === 0) return false;
return true;
}
// Default interface props don't need labels and descriptions
function isDefaultInterfaceProperty(propertyName, properties) {
if (propertyName === "label" || propertyName === "description") {
const interfacePropValue = findPropertyWithName("type", properties)?.value;
return (interfacePropValue?.value === "$.interface.timer" || interfacePropValue?.value === "$.interface.http");
}
return false;
}
function getComponentFromNode(node) {
if (isDefaultExport(node) && isObjectWithProperties(node.declaration)) {
return node.declaration;
}
if (node.expression) {
const {
left,
right,
} = node.expression;
if (isModuleExports(left) && isObjectWithProperties(right)) {
return right;
}
}
return null;
}
// Objects can contain key names surrounded by quotes, or not
// propertyArray is the array of Property nodes in the component object
function astIncludesProperty(name, propertyArray) {
// value for Literals (quotes), name for Identifiers (no quotes)
const propertyNames = propertyArray.map((p) => {
return p?.key?.value ?? p?.key?.name;
});
const val = propertyNames.includes(name) || propertyNames.includes(`"${name}"`);
return val;
}
// Returns the Property node matching the given name
// propertyArray is the array of Property nodes in the component object
function findPropertyWithName(name, propertyArray) {
return propertyArray.find((p) => {
return p?.key?.name === name ||
p?.key?.name === `"${name}"` ||
p?.key?.value === name ||
p?.key?.value === `"${name}"`;
});
}
// Does a component contain the right property? e.g. key, version
function componentContainsPropertyCheck(context, node, propertyName, message) {
const component = getComponentFromNode(node);
if (!component) return;
if (!astIncludesProperty(propertyName, component.properties)) {
context.report({
node: node,
message: message ?? `Components must export a ${propertyName} property. See https://pipedream.com/docs/components/guidelines/#required-metadata`,
});
}
}
// Extract props or propDefintions from the object properties of the module
function getProps(moduleProperties) {
return moduleProperties.find((p) => {
return p?.key?.name === "props" ||
p?.key?.value === "props" ||
p?.key?.name === "propDefinitions" ||
p?.key?.value === "propDefinitions";
});
}
// Do component props contain the right properties? e.g. label, description
function componentPropsContainsPropertyCheck(context, node, propertyName) {
const component = getComponentFromNode(node);
if (!component) return;
const { properties } = component;
if (!(astIncludesProperty("props", properties) || astIncludesProperty("propDefinitions", properties))) return;
const props = getProps(properties);
if (!isObjectWithProperties(props?.value)) return;
for (const prop of props.value?.properties) {
const {
key,
value: propDef,
} = prop;
// We don't want to lint app props or props that are defined in propDefinitions
if (!isObjectWithProperties(propDef)) continue;
if (astIncludesProperty("propDefinition", propDef.properties)) continue;
if (isDefaultInterfaceProperty(propertyName, propDef.properties)) continue;
if (!astIncludesProperty(propertyName, propDef.properties)) {
context.report({
node: prop,
message: `Component prop ${key?.name ?? key?.value} must have a ${propertyName}. See https://pipedream.com/docs/components/guidelines/#props`,
});
}
}
}
function optionalComponentPropsHaveDefaultProperty(context, node) {
const component = getComponentFromNode(node);
if (!component) return;
const { properties } = component;
if (!(astIncludesProperty("props", properties) || astIncludesProperty("propDefinitions", properties))) return;
const props = getProps(properties);
if (!isObjectWithProperties(props?.value)) return;
for (const prop of props.value?.properties) {
const {
key,
value: propDef,
} = prop;
// We don't want to lint app props or props that are defined in propDefinitions
if (!isObjectWithProperties(propDef)) continue;
if (!isObjectWithProperties(component)) continue;
if (astIncludesProperty("propDefinition", component.properties)) continue;
// value for Literals (quotes), name for Identifiers (no quotes)
const optionalProp = findPropertyWithName("optional", propDef.properties);
const optionalValue = optionalProp?.value?.value;
if (astIncludesProperty("optional", propDef.properties) && optionalValue && !astIncludesProperty("default", propDef.properties)) {
context.report({
node: prop,
message: `Component prop ${key?.name ?? key?.value} is marked "optional", so it may need a "default" property. See https://pipedream.com/docs/components/guidelines/#default-values`,
});
}
}
}
// Checks to confirm the component is a source, and returns
// the node with the name specified by the user
function checkComponentIsSourceAndReturnTargetProp(node, propertyName) {
const component = getComponentFromNode(node);
if (!component) return;
const { properties } = component;
const typeProp = findPropertyWithName("type", properties);
// A separate rule checks the presence of the type property
if (!typeProp) return;
if (typeProp?.value?.value !== "source") return;
return findPropertyWithName(propertyName, properties);
}
function componentSourceNameCheck(context, node) {
const nameProp = checkComponentIsSourceAndReturnTargetProp(node, "name");
if (!nameProp) return;
if (!nameProp?.value?.value.startsWith("New ")) {
context.report({
node: nameProp,
message: "Source names should start with \"New\". See https://pipedream.com/docs/components/guidelines/#source-name",
});
}
}
function componentSourceDescriptionCheck(context, node) {
const nameProp = checkComponentIsSourceAndReturnTargetProp(node, "description");
if (!nameProp || typeof nameProp?.value?.value !== "string") return;
if (!nameProp.value.value.startsWith("Emit new ")) {
context.report({
node: nameProp,
message: "Source descriptions should start with \"Emit new\". See https://pipedream.com/docs/components/guidelines/#source-description",
});
}
}
function componentVersionTsMacroCheck(context, node) {
const component = getComponentFromNode(node);
if (!component) return;
const { properties } = component;
const versionProp = findPropertyWithName("version", properties);
if (!versionProp) return;
if (versionProp?.value?.value.includes("{{ts}}")) {
context.report({
node: versionProp,
message: "{{ts}} macro should be removed before committing",
});
}
}
// Rules run on two different AST node types: ExpressionStatement (CJS) and
// ExportDefaultDeclaration (ESM)
module.exports = {
rules: {
"required-properties-key": {
create: function (context) {
return {
ExpressionStatement(node) {
componentContainsPropertyCheck(context, node, "key");
},
ExportDefaultDeclaration(node) {
componentContainsPropertyCheck(context, node, "key");
},
};
},
},
"required-properties-name": {
create: function (context) {
return {
ExpressionStatement(node) {
componentContainsPropertyCheck(context, node, "name");
},
ExportDefaultDeclaration(node) {
componentContainsPropertyCheck(context, node, "name");
},
};
},
},
"required-properties-version": {
create: function (context) {
return {
ExpressionStatement(node) {
componentContainsPropertyCheck(context, node, "version");
},
ExportDefaultDeclaration(node) {
componentContainsPropertyCheck(context, node, "version");
},
};
},
},
"required-properties-description": {
create: function (context) {
return {
ExpressionStatement(node) {
componentContainsPropertyCheck(context, node, "description");
},
ExportDefaultDeclaration(node) {
componentContainsPropertyCheck(context, node, "description");
},
};
},
},
"required-properties-type": {
create: function (context) {
return {
ExpressionStatement(node) {
componentContainsPropertyCheck(context, node, "type", "Components must export a type property (\"source\" or \"action\")");
},
ExportDefaultDeclaration(node) {
componentContainsPropertyCheck(context, node, "type", "Components must export a type property (\"source\" or \"action\")");
},
};
},
},
"props-label": {
create: function (context) {
return {
ExpressionStatement(node) {
componentPropsContainsPropertyCheck(context, node, "label");
},
ExportDefaultDeclaration(node) {
componentPropsContainsPropertyCheck(context, node, "label");
},
};
},
},
"props-description": {
create: function (context) {
return {
ExpressionStatement(node) {
componentPropsContainsPropertyCheck(context, node, "description");
},
ExportDefaultDeclaration(node) {
componentPropsContainsPropertyCheck(context, node, "description");
},
};
},
},
"default-value-required-for-optional-props": {
create: function (context) {
return {
ExpressionStatement(node) {
optionalComponentPropsHaveDefaultProperty(context, node);
},
ExportDefaultDeclaration(node) {
optionalComponentPropsHaveDefaultProperty(context, node);
},
};
},
},
"source-name": {
create: function (context) {
return {
ExpressionStatement(node) {
componentSourceNameCheck(context, node);
},
ExportDefaultDeclaration(node) {
componentSourceNameCheck(context, node);
},
};
},
},
"source-description": {
create: function (context) {
return {
ExpressionStatement(node) {
componentSourceDescriptionCheck(context, node);
},
ExportDefaultDeclaration(node) {
componentSourceDescriptionCheck(context, node);
},
};
},
},
"no-ts-version": {
create: function (context) {
return {
ExpressionStatement(node) {
componentVersionTsMacroCheck(context, node);
},
ExportDefaultDeclaration(node) {
componentVersionTsMacroCheck(context, node);
},
};
},
},
},
};