@sailshq/language-server 0.2.1 → 0.2.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sailshq/language-server",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "Language Server Protocol server for Sails Language Service",
5
5
  "homepage": "https://sailjs.com",
6
6
  "repository": {
@@ -27,5 +27,8 @@
27
27
  },
28
28
  "publishConfig": {
29
29
  "access": "public"
30
+ },
31
+ "devDependencies": {
32
+ "@sailshq/language-server": "^0.2.1"
30
33
  }
31
34
  }
@@ -38,8 +38,7 @@ module.exports = function validateModelAttributeExist(document, typeMap) {
38
38
  node.callee &&
39
39
  node.callee.type === 'MemberExpression' &&
40
40
  node.arguments &&
41
- node.arguments.length > 0 &&
42
- node.arguments[0].type === 'ObjectExpression'
41
+ node.arguments.length > 0
43
42
  ) {
44
43
  const method = node.callee.property.name
45
44
  const modelName = node.callee.object.name
@@ -60,163 +59,142 @@ module.exports = function validateModelAttributeExist(document, typeMap) {
60
59
  ) {
61
60
  const model = getModelByName(modelName)
62
61
  if (!model) return
63
- for (const prop of node.arguments[0].properties) {
64
- const attribute = prop.key && (prop.key.name || prop.key.value)
65
- const queryOptionKeys = [
66
- 'where',
67
- 'select',
68
- 'omit',
69
- 'sort',
70
- 'limit',
71
- 'skip',
72
- 'page',
73
- 'populate',
74
- 'groupBy',
75
- 'having',
76
- 'sum',
77
- 'average',
78
- 'min',
79
- 'max',
80
- 'distinct',
81
- 'meta'
82
- ]
83
- // --- FINAL FIX: treat 'where' exactly like 'sort', 'select', 'omit' for non-create methods ---
84
- if (
85
- method !== 'create' &&
86
- method !== 'createEach' &&
87
- queryOptionKeys.includes(attribute)
88
- ) {
62
+ // --- FIX: handle createEach array of objects ---
63
+ if (
64
+ method === 'createEach' &&
65
+ node.arguments[0].type === 'ArrayExpression'
66
+ ) {
67
+ for (const el of node.arguments[0].elements) {
68
+ if (!el || el.type !== 'ObjectExpression') continue
69
+ for (const prop of el.properties) {
70
+ const attribute =
71
+ prop.key && (prop.key.name || prop.key.value)
72
+ if (
73
+ !model.attributes ||
74
+ !Object.prototype.hasOwnProperty.call(
75
+ model.attributes,
76
+ attribute
77
+ )
78
+ ) {
79
+ diagnostics.push(
80
+ lsp.Diagnostic.create(
81
+ lsp.Range.create(
82
+ document.positionAt(prop.key.start),
83
+ document.positionAt(prop.key.end)
84
+ ),
85
+ `'${attribute}' is not a valid attribute of model '${modelName}'. Valid attributes: ${Object.keys(model.attributes || {}).join(', ')}`,
86
+ lsp.DiagnosticSeverity.Error,
87
+ 'sails-lsp'
88
+ )
89
+ )
90
+ }
91
+ }
92
+ }
93
+ return
94
+ }
95
+ // --- END FIX ---
96
+ if (node.arguments[0].type === 'ObjectExpression') {
97
+ for (const prop of node.arguments[0].properties) {
98
+ const attribute = prop.key && (prop.key.name || prop.key.value)
99
+ const queryOptionKeys = [
100
+ 'where',
101
+ 'select',
102
+ 'omit',
103
+ 'sort',
104
+ 'limit',
105
+ 'skip',
106
+ 'page',
107
+ 'populate',
108
+ 'groupBy',
109
+ 'having',
110
+ 'sum',
111
+ 'average',
112
+ 'min',
113
+ 'max',
114
+ 'distinct',
115
+ 'meta'
116
+ ]
117
+ // For non-create methods, validate all top-level keys except query option keys
89
118
  if (
90
- attribute === 'where' &&
91
- prop.value &&
92
- prop.value.type === 'ObjectExpression'
119
+ method !== 'create' &&
120
+ method !== 'createEach' &&
121
+ !queryOptionKeys.includes(attribute)
93
122
  ) {
94
- for (const whereProp of prop.value.properties) {
95
- if (!whereProp.key) continue
96
- const whereAttr = whereProp.key.name || whereProp.key.value
97
- if (
98
- !model.attributes ||
99
- !Object.prototype.hasOwnProperty.call(
100
- model.attributes,
101
- whereAttr
102
- )
103
- ) {
104
- diagnostics.push(
105
- lsp.Diagnostic.create(
106
- lsp.Range.create(
107
- document.positionAt(whereProp.key.start),
108
- document.positionAt(whereProp.key.end)
109
- ),
110
- `'${whereAttr}' is not a valid attribute of model '${modelName}'. Valid attributes: ${Object.keys(model.attributes || {}).join(', ')}`,
111
- lsp.DiagnosticSeverity.Error,
112
- 'sails-lsp'
113
- )
123
+ if (
124
+ !model.attributes ||
125
+ !Object.prototype.hasOwnProperty.call(
126
+ model.attributes,
127
+ attribute
128
+ )
129
+ ) {
130
+ diagnostics.push(
131
+ lsp.Diagnostic.create(
132
+ lsp.Range.create(
133
+ document.positionAt(prop.key.start),
134
+ document.positionAt(prop.key.end)
135
+ ),
136
+ `'${attribute}' is not a valid attribute of model '${modelName}'. Valid attributes: ${Object.keys(model.attributes || {}).join(', ')}`,
137
+ lsp.DiagnosticSeverity.Error,
138
+ 'sails-lsp'
114
139
  )
115
- }
140
+ )
116
141
  }
117
142
  continue
118
143
  }
119
144
  if (
120
- (attribute === 'select' || attribute === 'omit') &&
121
- prop.value &&
122
- prop.value.type === 'ArrayExpression'
145
+ method !== 'create' &&
146
+ method !== 'createEach' &&
147
+ queryOptionKeys.includes(attribute)
123
148
  ) {
124
- for (const el of prop.value.elements) {
125
- if (!el) continue
126
- if (el.type === 'Literal' || el.type === 'StringLiteral') {
127
- const arrAttr = el.value
149
+ if (
150
+ attribute === 'where' &&
151
+ prop.value &&
152
+ prop.value.type === 'ObjectExpression'
153
+ ) {
154
+ for (const whereProp of prop.value.properties) {
155
+ if (!whereProp.key) continue
156
+ const whereAttr =
157
+ whereProp.key.name || whereProp.key.value
128
158
  if (
129
159
  !model.attributes ||
130
160
  !Object.prototype.hasOwnProperty.call(
131
161
  model.attributes,
132
- arrAttr
162
+ whereAttr
133
163
  )
134
164
  ) {
135
165
  diagnostics.push(
136
166
  lsp.Diagnostic.create(
137
167
  lsp.Range.create(
138
- document.positionAt(el.start),
139
- document.positionAt(el.end)
168
+ document.positionAt(whereProp.key.start),
169
+ document.positionAt(whereProp.key.end)
140
170
  ),
141
- `'${arrAttr}' is not a valid attribute of model '${modelName}'. Valid attributes: ${Object.keys(model.attributes || {}).join(', ')}`,
171
+ `'${whereAttr}' is not a valid attribute of model '${modelName}'. Valid attributes: ${Object.keys(model.attributes || {}).join(', ')}`,
142
172
  lsp.DiagnosticSeverity.Error,
143
173
  'sails-lsp'
144
174
  )
145
175
  )
146
176
  }
147
177
  }
178
+ continue
148
179
  }
149
- continue
150
- }
151
- if (attribute === 'sort' && prop.value) {
152
180
  if (
153
- prop.value.type === 'Literal' ||
154
- prop.value.type === 'StringLiteral'
181
+ (attribute === 'select' || attribute === 'omit') &&
182
+ prop.value &&
183
+ prop.value.type === 'ArrayExpression'
155
184
  ) {
156
- const sortStr = prop.value.value
157
- const sortAttr = sortStr && sortStr.split(' ')[0]
158
- if (
159
- sortAttr &&
160
- (!model.attributes ||
161
- !Object.prototype.hasOwnProperty.call(
162
- model.attributes,
163
- sortAttr
164
- ))
165
- ) {
166
- diagnostics.push(
167
- lsp.Diagnostic.create(
168
- lsp.Range.create(
169
- document.positionAt(prop.value.start),
170
- document.positionAt(prop.value.end)
171
- ),
172
- `'${sortAttr}' is not a valid attribute of model '${modelName}'. Valid attributes: ${Object.keys(model.attributes || {}).join(', ')}`,
173
- lsp.DiagnosticSeverity.Error,
174
- 'sails-lsp'
175
- )
176
- )
177
- }
178
- continue
179
- } else if (prop.value.type === 'ArrayExpression') {
180
185
  for (const el of prop.value.elements) {
181
186
  if (!el) continue
182
- if (el.type === 'ObjectExpression') {
183
- for (const sortProp of el.properties) {
184
- if (!sortProp.key) continue
185
- const sortAttr =
186
- sortProp.key.name || sortProp.key.value
187
- if (
188
- !model.attributes ||
189
- !Object.prototype.hasOwnProperty.call(
190
- model.attributes,
191
- sortAttr
192
- )
193
- ) {
194
- diagnostics.push(
195
- lsp.Diagnostic.create(
196
- lsp.Range.create(
197
- document.positionAt(sortProp.key.start),
198
- document.positionAt(sortProp.key.end)
199
- ),
200
- `'${sortAttr}' is not a valid attribute of model '${modelName}'. Valid attributes: ${Object.keys(model.attributes || {}).join(', ')}`,
201
- lsp.DiagnosticSeverity.Error,
202
- 'sails-lsp'
203
- )
204
- )
205
- }
206
- }
207
- } else if (
187
+ if (
208
188
  el.type === 'Literal' ||
209
189
  el.type === 'StringLiteral'
210
190
  ) {
211
- const sortStr = el.value
212
- const sortAttr = sortStr && sortStr.split(' ')[0]
191
+ const arrAttr = el.value
213
192
  if (
214
- sortAttr &&
215
- (!model.attributes ||
216
- !Object.prototype.hasOwnProperty.call(
217
- model.attributes,
218
- sortAttr
219
- ))
193
+ !model.attributes ||
194
+ !Object.prototype.hasOwnProperty.call(
195
+ model.attributes,
196
+ arrAttr
197
+ )
220
198
  ) {
221
199
  diagnostics.push(
222
200
  lsp.Diagnostic.create(
@@ -224,7 +202,7 @@ module.exports = function validateModelAttributeExist(document, typeMap) {
224
202
  document.positionAt(el.start),
225
203
  document.positionAt(el.end)
226
204
  ),
227
- `'${sortAttr}' is not a valid attribute of model '${modelName}'. Valid attributes: ${Object.keys(model.attributes || {}).join(', ')}`,
205
+ `'${arrAttr}' is not a valid attribute of model '${modelName}'. Valid attributes: ${Object.keys(model.attributes || {}).join(', ')}`,
228
206
  lsp.DiagnosticSeverity.Error,
229
207
  'sails-lsp'
230
208
  )
@@ -233,22 +211,27 @@ module.exports = function validateModelAttributeExist(document, typeMap) {
233
211
  }
234
212
  }
235
213
  continue
236
- } else if (prop.value.type === 'ObjectExpression') {
237
- for (const sortProp of prop.value.properties) {
238
- if (!sortProp.key) continue
239
- const sortAttr = sortProp.key.name || sortProp.key.value
214
+ }
215
+ if (attribute === 'sort' && prop.value) {
216
+ if (
217
+ prop.value.type === 'Literal' ||
218
+ prop.value.type === 'StringLiteral'
219
+ ) {
220
+ const sortStr = prop.value.value
221
+ const sortAttr = sortStr && sortStr.split(' ')[0]
240
222
  if (
241
- !model.attributes ||
242
- !Object.prototype.hasOwnProperty.call(
243
- model.attributes,
244
- sortAttr
245
- )
223
+ sortAttr &&
224
+ (!model.attributes ||
225
+ !Object.prototype.hasOwnProperty.call(
226
+ model.attributes,
227
+ sortAttr
228
+ ))
246
229
  ) {
247
230
  diagnostics.push(
248
231
  lsp.Diagnostic.create(
249
232
  lsp.Range.create(
250
- document.positionAt(sortProp.key.start),
251
- document.positionAt(sortProp.key.end)
233
+ document.positionAt(prop.value.start),
234
+ document.positionAt(prop.value.end)
252
235
  ),
253
236
  `'${sortAttr}' is not a valid attribute of model '${modelName}'. Valid attributes: ${Object.keys(model.attributes || {}).join(', ')}`,
254
237
  lsp.DiagnosticSeverity.Error,
@@ -256,34 +239,116 @@ module.exports = function validateModelAttributeExist(document, typeMap) {
256
239
  )
257
240
  )
258
241
  }
242
+ continue
243
+ } else if (prop.value.type === 'ArrayExpression') {
244
+ for (const el of prop.value.elements) {
245
+ if (!el) continue
246
+ if (el.type === 'ObjectExpression') {
247
+ for (const sortProp of el.properties) {
248
+ if (!sortProp.key) continue
249
+ const sortAttr =
250
+ sortProp.key.name || sortProp.key.value
251
+ if (
252
+ !model.attributes ||
253
+ !Object.prototype.hasOwnProperty.call(
254
+ model.attributes,
255
+ sortAttr
256
+ )
257
+ ) {
258
+ diagnostics.push(
259
+ lsp.Diagnostic.create(
260
+ lsp.Range.create(
261
+ document.positionAt(sortProp.key.start),
262
+ document.positionAt(sortProp.key.end)
263
+ ),
264
+ `'${sortAttr}' is not a valid attribute of model '${modelName}'. Valid attributes: ${Object.keys(model.attributes || {}).join(', ')}`,
265
+ lsp.DiagnosticSeverity.Error,
266
+ 'sails-lsp'
267
+ )
268
+ )
269
+ }
270
+ }
271
+ } else if (
272
+ el.type === 'Literal' ||
273
+ el.type === 'StringLiteral'
274
+ ) {
275
+ const sortStr = el.value
276
+ const sortAttr = sortStr && sortStr.split(' ')[0]
277
+ if (
278
+ sortAttr &&
279
+ (!model.attributes ||
280
+ !Object.prototype.hasOwnProperty.call(
281
+ model.attributes,
282
+ sortAttr
283
+ ))
284
+ ) {
285
+ diagnostics.push(
286
+ lsp.Diagnostic.create(
287
+ lsp.Range.create(
288
+ document.positionAt(el.start),
289
+ document.positionAt(el.end)
290
+ ),
291
+ `'${sortAttr}' is not a valid attribute of model '${modelName}'. Valid attributes: ${Object.keys(model.attributes || {}).join(', ')}`,
292
+ lsp.DiagnosticSeverity.Error,
293
+ 'sails-lsp'
294
+ )
295
+ )
296
+ }
297
+ }
298
+ }
299
+ continue
300
+ } else if (prop.value.type === 'ObjectExpression') {
301
+ for (const sortProp of prop.value.properties) {
302
+ if (!sortProp.key) continue
303
+ const sortAttr = sortProp.key.name || sortProp.key.value
304
+ if (
305
+ !model.attributes ||
306
+ !Object.prototype.hasOwnProperty.call(
307
+ model.attributes,
308
+ sortAttr
309
+ )
310
+ ) {
311
+ diagnostics.push(
312
+ lsp.Diagnostic.create(
313
+ lsp.Range.create(
314
+ document.positionAt(sortProp.key.start),
315
+ document.positionAt(sortProp.key.end)
316
+ ),
317
+ `'${sortAttr}' is not a valid attribute of model '${modelName}'. Valid attributes: ${Object.keys(model.attributes || {}).join(', ')}`,
318
+ lsp.DiagnosticSeverity.Error,
319
+ 'sails-lsp'
320
+ )
321
+ )
322
+ }
323
+ }
324
+ continue
259
325
  }
260
- continue
261
326
  }
327
+ // For all other query option keys, skip validation
328
+ continue
262
329
  }
263
- // For all other query option keys, skip validation
264
- continue
265
- }
266
- // --- END ROBUST FIX ---
267
- // Only validate top-level for create/createEach
268
- if (
269
- (method === 'create' || method === 'createEach') &&
270
- (!model.attributes ||
271
- !Object.prototype.hasOwnProperty.call(
272
- model.attributes,
273
- attribute
274
- ))
275
- ) {
276
- diagnostics.push(
277
- lsp.Diagnostic.create(
278
- lsp.Range.create(
279
- document.positionAt(prop.key.start),
280
- document.positionAt(prop.key.end)
281
- ),
282
- `'${attribute}' is not a valid attribute of model '${modelName}'. Valid attributes: ${Object.keys(model.attributes || {}).join(', ')}`,
283
- lsp.DiagnosticSeverity.Error,
284
- 'sails-lsp'
330
+ // --- END ROBUST FIX ---
331
+ // Only validate top-level for create/createEach
332
+ if (
333
+ (method === 'create' || method === 'createEach') &&
334
+ (!model.attributes ||
335
+ !Object.prototype.hasOwnProperty.call(
336
+ model.attributes,
337
+ attribute
338
+ ))
339
+ ) {
340
+ diagnostics.push(
341
+ lsp.Diagnostic.create(
342
+ lsp.Range.create(
343
+ document.positionAt(prop.key.start),
344
+ document.positionAt(prop.key.end)
345
+ ),
346
+ `'${attribute}' is not a valid attribute of model '${modelName}'. Valid attributes: ${Object.keys(model.attributes || {}).join(', ')}`,
347
+ lsp.DiagnosticSeverity.Error,
348
+ 'sails-lsp'
349
+ )
285
350
  )
286
- )
351
+ }
287
352
  }
288
353
  }
289
354
  }