@sailshq/language-server 0.2.1 → 0.2.3
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.
|
@@ -18,18 +18,12 @@ module.exports = function modelAttributesCompletion(
|
|
|
18
18
|
const criteriaMatch = before.match(
|
|
19
19
|
/(?:sails\.models\.([A-Za-z_$][\w$]*)|([A-Za-z_$][\w$]*))\s*\.\w+\s*\(\s*\{[^}]*([a-zA-Z0-9_]*)?$/
|
|
20
20
|
)
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
)
|
|
24
|
-
const selectArrayMatch = before.match(
|
|
21
|
+
const sortStringMatch = before.match(/sort\s*:\s*['"]([a-zA-Z0-9_]*)?$/)
|
|
22
|
+
const criteriaOptionsArrayMatch = before.match(
|
|
25
23
|
/(?:select|omit|sort)\s*:\s*\[\s*['"]([a-zA-Z0-9_]*)?$/
|
|
26
24
|
)
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
)
|
|
30
|
-
const sortStringMatch = before.match(/sort\s*:\s*['"]([a-zA-Z0-9_]*)?$/)
|
|
31
|
-
const sortArrayStringMatch = before.match(
|
|
32
|
-
/sort\s*:\s*\[\s*[^\{\]]*['"]([a-zA-Z0-9_]*)?$/
|
|
25
|
+
const popuplateMethodMatch = before.match(
|
|
26
|
+
/\.populate\s*\(\s*['"]([a-zA-Z0-9_]*)?$/
|
|
33
27
|
)
|
|
34
28
|
const sortArrayObjectMatch = before.match(
|
|
35
29
|
/sort\s*:\s*\[\s*\{\s*([a-zA-Z0-9_]*)?$/
|
|
@@ -44,7 +38,7 @@ module.exports = function modelAttributesCompletion(
|
|
|
44
38
|
// Detect if we are inside a .select([]), .omit([]), .sort([]), etc. as a method call (e.g. User.find().select([]))
|
|
45
39
|
// This matches e.g. .select(['foo', '']) or .omit(["bar", '']) or .select('foo')
|
|
46
40
|
const chainableDirectCallMatch = before.match(
|
|
47
|
-
/\.(select|omit|sort|populate|where)\s*\(\s*\[?\s*['"]
|
|
41
|
+
/\.(select|omit|sort|populate|where)\s*\(\s*\[.*(?:,|\[)?\s*['"`]([a-zA-Z0-9_]*)?$/
|
|
48
42
|
)
|
|
49
43
|
|
|
50
44
|
// Also allow completions in .where({ ... }) chainable method call context
|
|
@@ -56,41 +50,52 @@ module.exports = function modelAttributesCompletion(
|
|
|
56
50
|
/([A-Za-z_$][\w$]*)\s*\.where\s*\(\s*\{[^}]*([a-zA-Z0-9_]*)?$/
|
|
57
51
|
)
|
|
58
52
|
|
|
53
|
+
// Support chained .where({ ... }) completions ---
|
|
54
|
+
// Try to infer model name from chained calls like User.find().where({ ... })
|
|
55
|
+
const chainedWhereMatch = before.match(
|
|
56
|
+
/([A-Za-z_$][\w$]*)\s*\.[\w$]+\s*\(.*?\)\s*\.where\s*\(\s*\{[^}]*([a-zA-Z0-9_]*)?$/
|
|
57
|
+
)
|
|
58
|
+
|
|
59
59
|
// Only suppress completions after a colon (:) in object literals for static methods,
|
|
60
60
|
// but always allow completions in .select(['']), .omit(['']), .sort(['']), .where({}), etc.
|
|
61
61
|
const inChainableString =
|
|
62
|
-
|
|
63
|
-
selectArrayMatch ||
|
|
62
|
+
criteriaOptionsArrayMatch ||
|
|
64
63
|
sortStringMatch ||
|
|
65
|
-
sortArrayStringMatch ||
|
|
66
64
|
sortArrayObjectMatch ||
|
|
67
|
-
|
|
65
|
+
popuplateMethodMatch ||
|
|
68
66
|
isInChainableMethodCall ||
|
|
69
67
|
isInWhereMethodCall ||
|
|
70
68
|
!!chainableDirectCallMatch
|
|
71
69
|
|
|
72
70
|
// Suppress completions after a colon only if NOT in a chainable string/array context
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
// If
|
|
84
|
-
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
(
|
|
89
|
-
|
|
71
|
+
// Also suppress completions after colon in .where({ ... }) context, unless after a comma or at start
|
|
72
|
+
// FIX: Do not run this suppression logic at all if we are in a select/omit/sort array (object property form)
|
|
73
|
+
if (!criteriaOptionsArrayMatch) {
|
|
74
|
+
if (
|
|
75
|
+
!inChainableString ||
|
|
76
|
+
((isInWhereMethodCall || chainedWhereMatch) && !criteriaOptionsArrayMatch)
|
|
77
|
+
) {
|
|
78
|
+
const lines = before.split('\n')
|
|
79
|
+
const line = lines[lines.length - 1]
|
|
80
|
+
const beforeCursor = line.slice(0, position.character)
|
|
81
|
+
// If the last non-whitespace character before the cursor is a colon, suppress completion
|
|
82
|
+
// (but allow after comma, or at start of line/object)
|
|
83
|
+
const lastColon = beforeCursor.lastIndexOf(':')
|
|
84
|
+
const lastComma = beforeCursor.lastIndexOf(',')
|
|
85
|
+
if (lastColon > lastComma && lastColon > beforeCursor.lastIndexOf('{')) {
|
|
86
|
+
// Check if we are inside a string (e.g. after a colon and inside quotes)
|
|
87
|
+
// If so, suppress completion
|
|
88
|
+
const quoteBefore = beforeCursor.lastIndexOf("'")
|
|
89
|
+
const dquoteBefore = beforeCursor.lastIndexOf('"')
|
|
90
|
+
if (
|
|
91
|
+
(quoteBefore > lastColon && quoteBefore > lastComma) ||
|
|
92
|
+
(dquoteBefore > lastColon && dquoteBefore > lastComma)
|
|
93
|
+
) {
|
|
94
|
+
return []
|
|
95
|
+
}
|
|
96
|
+
// Otherwise, suppress completion after colon
|
|
90
97
|
return []
|
|
91
98
|
}
|
|
92
|
-
// Otherwise, suppress completion after colon
|
|
93
|
-
return []
|
|
94
99
|
}
|
|
95
100
|
}
|
|
96
101
|
|
|
@@ -114,23 +119,35 @@ module.exports = function modelAttributesCompletion(
|
|
|
114
119
|
return last[1] || last[2] || null
|
|
115
120
|
}
|
|
116
121
|
|
|
122
|
+
// Determine the current Sails.js model context and attribute prefix for completions
|
|
123
|
+
// by matching the code before the cursor against various Sails.js query patterns.
|
|
124
|
+
// This enables context-aware attribute completions for all supported query forms.
|
|
117
125
|
if (criteriaMatch) {
|
|
118
126
|
modelName = criteriaMatch[1] || criteriaMatch[2]
|
|
119
127
|
prefix = criteriaMatch[3] || ''
|
|
120
|
-
} else if (
|
|
128
|
+
} else if (criteriaOptionsArrayMatch) {
|
|
121
129
|
modelName = inferModelName(before)
|
|
122
|
-
prefix =
|
|
123
|
-
} else if (
|
|
130
|
+
prefix = criteriaOptionsArrayMatch[1] || ''
|
|
131
|
+
} else if (popuplateMethodMatch) {
|
|
124
132
|
isPopulate = true
|
|
125
133
|
modelName = inferModelName(before)
|
|
126
|
-
prefix =
|
|
127
|
-
} else if (
|
|
134
|
+
prefix = popuplateMethodMatch[1] || ''
|
|
135
|
+
} else if (
|
|
136
|
+
sortStringMatch ||
|
|
137
|
+
criteriaOptionsArrayMatch ||
|
|
138
|
+
sortArrayObjectMatch
|
|
139
|
+
) {
|
|
128
140
|
modelName = inferModelName(before)
|
|
129
141
|
prefix =
|
|
130
|
-
(sortStringMatch ||
|
|
142
|
+
(sortStringMatch ||
|
|
143
|
+
criteriaOptionsArrayMatch ||
|
|
144
|
+
sortArrayObjectMatch)[1] || ''
|
|
131
145
|
} else if (whereMethodCallMatch) {
|
|
132
146
|
modelName = whereMethodCallMatch[1]
|
|
133
147
|
prefix = whereMethodCallMatch[2] || ''
|
|
148
|
+
} else if (chainedWhereMatch) {
|
|
149
|
+
modelName = chainedWhereMatch[1]
|
|
150
|
+
prefix = chainedWhereMatch[2] || ''
|
|
134
151
|
} else if (chainableDirectCallMatch) {
|
|
135
152
|
modelName = inferModelName(before)
|
|
136
153
|
prefix = chainableDirectCallMatch[2] || ''
|
|
@@ -166,6 +183,59 @@ module.exports = function modelAttributesCompletion(
|
|
|
166
183
|
}
|
|
167
184
|
}
|
|
168
185
|
|
|
186
|
+
// Remove already-used attributes for both object and array/chainable forms
|
|
187
|
+
// Collect all used attributes from any select/omit/sort array in object or chainable form up to the cursor
|
|
188
|
+
const allArrayRegex =
|
|
189
|
+
/(select|omit|sort)\s*:\s*\[([^\]]*)\]|\.(select|omit|sort)\s*\(\s*\[([^\]]*)/g
|
|
190
|
+
let match
|
|
191
|
+
while ((match = allArrayRegex.exec(before)) !== null) {
|
|
192
|
+
const arrayContent = match[2] || match[4] || ''
|
|
193
|
+
const usedInArray = Array.from(
|
|
194
|
+
arrayContent.matchAll(/['"`]\s*([a-zA-Z0-9_]+)\s*['"`]/g)
|
|
195
|
+
).map((m) => m[1])
|
|
196
|
+
usedInArray.forEach((attr) => usedProps.add(attr))
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// Improved: Only trigger completions in object form select/omit/sort arrays when inside a string (between quotes)
|
|
200
|
+
if (criteriaOptionsArrayMatch) {
|
|
201
|
+
// Find the last '[' before the cursor
|
|
202
|
+
const arrayStart = before.lastIndexOf('[')
|
|
203
|
+
if (arrayStart !== -1) {
|
|
204
|
+
const arrayContent = before.slice(arrayStart, offset)
|
|
205
|
+
// Use the same logic as chainable: check for a quote before the cursor (inside a string)
|
|
206
|
+
const quoteMatch = arrayContent.match(/['"`]([^'"`]*)$/)
|
|
207
|
+
if (!quoteMatch) {
|
|
208
|
+
// Not inside a string, suppress completions
|
|
209
|
+
return []
|
|
210
|
+
}
|
|
211
|
+
// Also: filter out already-used attributes in this array
|
|
212
|
+
const usedInArray = Array.from(
|
|
213
|
+
arrayContent.matchAll(/['"`]\s*([a-zA-Z0-9_]+)\s*['"`]/g)
|
|
214
|
+
).map((m) => m[1])
|
|
215
|
+
usedInArray.forEach((attr) => usedProps.add(attr))
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if (chainableDirectCallMatch) {
|
|
220
|
+
// For array/chainable forms, parse the array up to the cursor and collect used attributes
|
|
221
|
+
const arrayMatch = before.match(/\[([^\]]*)$/)
|
|
222
|
+
if (arrayMatch) {
|
|
223
|
+
const arrayContent = arrayMatch[1]
|
|
224
|
+
// Fix: allow completions for any string in the array, not just the first
|
|
225
|
+
// Find the last quote and ensure the cursor is after it (inside a string)
|
|
226
|
+
const quoteMatch = arrayContent.match(/['"`][^'"`]*$/)
|
|
227
|
+
if (!quoteMatch) {
|
|
228
|
+
// Not inside a string, suppress completions
|
|
229
|
+
return []
|
|
230
|
+
}
|
|
231
|
+
// Also: filter out already-used attributes in this array
|
|
232
|
+
const usedInArray = Array.from(
|
|
233
|
+
arrayContent.matchAll(/['"`]\s*([a-zA-Z0-9_]+)\s*['"`]/g)
|
|
234
|
+
).map((m) => m[1])
|
|
235
|
+
usedInArray.forEach((attr) => usedProps.add(attr))
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
169
239
|
if (isPopulate) {
|
|
170
240
|
attributes = Object.entries(model.attributes || {})
|
|
171
241
|
.filter(([, def]) => def && (def.model || def.collection))
|
|
@@ -174,22 +244,25 @@ module.exports = function modelAttributesCompletion(
|
|
|
174
244
|
attributes = Object.keys(model.attributes || {})
|
|
175
245
|
}
|
|
176
246
|
|
|
177
|
-
return
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
247
|
+
return Array.from(
|
|
248
|
+
new Set(
|
|
249
|
+
attributes
|
|
250
|
+
.filter((attr) => attr.toLowerCase().startsWith(prefix.toLowerCase()))
|
|
251
|
+
.filter((attr) => !usedProps.has(attr))
|
|
252
|
+
)
|
|
253
|
+
).map((attr) => {
|
|
254
|
+
const attrDef = model.attributes && model.attributes[attr]
|
|
255
|
+
let type = attrDef && attrDef.type ? attrDef.type : ''
|
|
256
|
+
let required = attrDef && attrDef.required ? 'required' : 'optional'
|
|
257
|
+
let detail = type ? `${type} (${required})` : required
|
|
258
|
+
return {
|
|
259
|
+
label: attr,
|
|
260
|
+
kind: lsp.CompletionItemKind.Field,
|
|
261
|
+
detail,
|
|
262
|
+
documentation: `${modelName}.${attr}`,
|
|
263
|
+
sortText: attr,
|
|
264
|
+
filterText: attr,
|
|
265
|
+
insertText: attr
|
|
266
|
+
}
|
|
267
|
+
})
|
|
195
268
|
}
|
|
@@ -15,10 +15,10 @@ module.exports = function modelMethodsCompletion(document, position, typeMap) {
|
|
|
15
15
|
const staticCallMatch = before.match(
|
|
16
16
|
/(?:sails\.models\.([A-Za-z_$][\w$]*)|([A-Za-z_$][\w$]*))\.\s*([a-zA-Z]*)?$/
|
|
17
17
|
)
|
|
18
|
-
// Match chainable calls
|
|
19
|
-
const
|
|
20
|
-
/([A-Za-z_$][\w$]*)\.[a-zA-Z_]+\([^)]*\)
|
|
21
|
-
|
|
18
|
+
// Match all chainable calls and get the last one for completion
|
|
19
|
+
const chainableCallMatches = [
|
|
20
|
+
...before.matchAll(/([A-Za-z_$][\w$]*)\.[a-zA-Z_]+\([^)]*\)/g)
|
|
21
|
+
]
|
|
22
22
|
|
|
23
23
|
let modelName, prefix, methods
|
|
24
24
|
|
|
@@ -26,11 +26,15 @@ module.exports = function modelMethodsCompletion(document, position, typeMap) {
|
|
|
26
26
|
const models = typeMap.models || {}
|
|
27
27
|
const modelKeys = Object.keys(models)
|
|
28
28
|
|
|
29
|
-
if (
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
29
|
+
if (chainableCallMatches.length > 0) {
|
|
30
|
+
// Use the last chainable call in the chain
|
|
31
|
+
const lastMatch = chainableCallMatches[chainableCallMatches.length - 1]
|
|
32
|
+
modelName = lastMatch[1]
|
|
33
|
+
// Get the prefix after the last dot (if user is typing e.g. .select)
|
|
34
|
+
const afterLastChain = before.slice(lastMatch.index + lastMatch[0].length)
|
|
35
|
+
const prefixMatch = afterLastChain.match(/\.\s*([a-zA-Z]*)?$/)
|
|
36
|
+
prefix = (prefixMatch && prefixMatch[1]) || ''
|
|
37
|
+
const foundKey = Object.keys(models).find(
|
|
34
38
|
(k) => k.toLowerCase() === modelName.toLowerCase()
|
|
35
39
|
)
|
|
36
40
|
methods = foundKey ? models[foundKey].chainableMethods || [] : []
|
|
@@ -52,7 +56,7 @@ module.exports = function modelMethodsCompletion(document, position, typeMap) {
|
|
|
52
56
|
let insertText = method.name + '($0)'
|
|
53
57
|
// For chainable .select or .omit, insert ([''])
|
|
54
58
|
if (
|
|
55
|
-
|
|
59
|
+
chainableCallMatches.length > 0 &&
|
|
56
60
|
(method.name === 'select' || method.name === 'omit')
|
|
57
61
|
) {
|
|
58
62
|
insertText = method.name + '([$0])'
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sailshq/language-server",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
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
|
}
|
|
@@ -34,32 +34,236 @@ module.exports = function validateModelAttributeExist(document, typeMap) {
|
|
|
34
34
|
})
|
|
35
35
|
walk.simple(ast, {
|
|
36
36
|
CallExpression(node) {
|
|
37
|
-
if (
|
|
38
|
-
node.callee &&
|
|
39
|
-
node.callee.type === 'MemberExpression' &&
|
|
40
|
-
node.arguments &&
|
|
41
|
-
node.arguments.length > 0 &&
|
|
42
|
-
node.arguments[0].type === 'ObjectExpression'
|
|
43
|
-
) {
|
|
37
|
+
if (node.callee && node.callee.type === 'MemberExpression') {
|
|
44
38
|
const method = node.callee.property.name
|
|
45
|
-
|
|
46
|
-
|
|
39
|
+
// --- Robust model name extraction for ALL method calls (including chainable) ---
|
|
40
|
+
let effectiveModelName = undefined
|
|
41
|
+
let obj = node.callee.object
|
|
42
|
+
while (obj) {
|
|
43
|
+
if (obj.type === 'Identifier') {
|
|
44
|
+
effectiveModelName = obj.name
|
|
45
|
+
break
|
|
46
|
+
} else if (
|
|
47
|
+
obj.type === 'CallExpression' &&
|
|
48
|
+
obj.callee &&
|
|
49
|
+
obj.callee.type === 'MemberExpression'
|
|
50
|
+
) {
|
|
51
|
+
obj = obj.callee.object
|
|
52
|
+
} else {
|
|
53
|
+
break
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
const model = getModelByName(effectiveModelName)
|
|
57
|
+
if (!model) return
|
|
58
|
+
|
|
59
|
+
// Only validate chainable methods that are select, omit, sort, or populate
|
|
60
|
+
const allowedChainable = ['select', 'omit', 'sort', 'populate']
|
|
61
|
+
|
|
62
|
+
// --- Ignore validation for arguments to non-model chainable methods like .intercept ---
|
|
63
|
+
// If the current method is NOT a model method or allowedChainable, skip validation for its arguments
|
|
64
|
+
const modelMethods = [
|
|
65
|
+
'create',
|
|
66
|
+
'createEach',
|
|
67
|
+
'count',
|
|
68
|
+
'find',
|
|
69
|
+
'findOne',
|
|
70
|
+
'update',
|
|
71
|
+
'destroy',
|
|
72
|
+
'where',
|
|
73
|
+
'findOrCreate',
|
|
74
|
+
'sum',
|
|
75
|
+
...allowedChainable
|
|
76
|
+
]
|
|
77
|
+
if (!modelMethods.includes(method)) {
|
|
78
|
+
// This is a non-model method (e.g., intercept, using, etc.), skip validation for its arguments
|
|
79
|
+
return
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// handle createEach array of objects
|
|
83
|
+
if (
|
|
84
|
+
method === 'createEach' &&
|
|
85
|
+
node.arguments[0] &&
|
|
86
|
+
node.arguments[0].type === 'ArrayExpression'
|
|
87
|
+
) {
|
|
88
|
+
for (const el of node.arguments[0].elements) {
|
|
89
|
+
if (!el || el.type !== 'ObjectExpression') continue
|
|
90
|
+
for (const prop of el.properties) {
|
|
91
|
+
const attribute = prop.key && (prop.key.name || prop.key.value)
|
|
92
|
+
if (
|
|
93
|
+
!model.attributes ||
|
|
94
|
+
!Object.prototype.hasOwnProperty.call(
|
|
95
|
+
model.attributes,
|
|
96
|
+
attribute
|
|
97
|
+
)
|
|
98
|
+
) {
|
|
99
|
+
diagnostics.push(
|
|
100
|
+
lsp.Diagnostic.create(
|
|
101
|
+
lsp.Range.create(
|
|
102
|
+
document.positionAt(prop.key.start),
|
|
103
|
+
document.positionAt(prop.key.end)
|
|
104
|
+
),
|
|
105
|
+
`'${attribute}' is not a valid attribute of model '${effectiveModelName}'. Valid attributes: ${Object.keys(model.attributes || {}).join(', ')}`,
|
|
106
|
+
lsp.DiagnosticSeverity.Error,
|
|
107
|
+
'sails-lsp'
|
|
108
|
+
)
|
|
109
|
+
)
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// Validate attributes in chained .where({ ... }) calls
|
|
47
117
|
if (
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
'count',
|
|
52
|
-
'find',
|
|
53
|
-
'findOne',
|
|
54
|
-
'update',
|
|
55
|
-
'destroy',
|
|
56
|
-
'where',
|
|
57
|
-
'findOrCreate',
|
|
58
|
-
'sum'
|
|
59
|
-
].includes(method)
|
|
118
|
+
method === 'where' &&
|
|
119
|
+
node.arguments[0] &&
|
|
120
|
+
node.arguments[0].type === 'ObjectExpression'
|
|
60
121
|
) {
|
|
61
|
-
const
|
|
62
|
-
|
|
122
|
+
for (const prop of node.arguments[0].properties) {
|
|
123
|
+
if (!prop.key) continue
|
|
124
|
+
const whereAttr = prop.key.name || prop.key.value
|
|
125
|
+
if (
|
|
126
|
+
!model.attributes ||
|
|
127
|
+
!Object.prototype.hasOwnProperty.call(
|
|
128
|
+
model.attributes,
|
|
129
|
+
whereAttr
|
|
130
|
+
)
|
|
131
|
+
) {
|
|
132
|
+
diagnostics.push(
|
|
133
|
+
lsp.Diagnostic.create(
|
|
134
|
+
lsp.Range.create(
|
|
135
|
+
document.positionAt(prop.key.start),
|
|
136
|
+
document.positionAt(prop.key.end)
|
|
137
|
+
),
|
|
138
|
+
`'${whereAttr}' is not a valid attribute of model '${effectiveModelName}'. Valid attributes: ${Object.keys(model.attributes || {}).join(', ')}`,
|
|
139
|
+
lsp.DiagnosticSeverity.Error,
|
|
140
|
+
'sails-lsp'
|
|
141
|
+
)
|
|
142
|
+
)
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// --- Unified validation for .select([]), .omit([]), .sort([]), .populate([]) chainable calls only ---
|
|
149
|
+
if (
|
|
150
|
+
allowedChainable.includes(method) &&
|
|
151
|
+
node.arguments[0] &&
|
|
152
|
+
node.arguments[0].type === 'ArrayExpression'
|
|
153
|
+
) {
|
|
154
|
+
for (const el of node.arguments[0].elements) {
|
|
155
|
+
if (!el) continue
|
|
156
|
+
let arrAttr = undefined
|
|
157
|
+
if (el.type === 'Literal' || el.type === 'StringLiteral') {
|
|
158
|
+
arrAttr = el.value
|
|
159
|
+
} else if (
|
|
160
|
+
el.type === 'TemplateLiteral' &&
|
|
161
|
+
el.expressions.length === 0
|
|
162
|
+
) {
|
|
163
|
+
arrAttr = el.quasis[0].value.cooked
|
|
164
|
+
} else if (el.type === 'ObjectExpression' && method === 'sort') {
|
|
165
|
+
// For sort([{ foo: 1 }])
|
|
166
|
+
for (const sortProp of el.properties) {
|
|
167
|
+
if (!sortProp.key) continue
|
|
168
|
+
const sortAttr = sortProp.key.name || sortProp.key.value
|
|
169
|
+
if (
|
|
170
|
+
!model.attributes ||
|
|
171
|
+
!Object.prototype.hasOwnProperty.call(
|
|
172
|
+
model.attributes,
|
|
173
|
+
sortAttr
|
|
174
|
+
)
|
|
175
|
+
) {
|
|
176
|
+
diagnostics.push(
|
|
177
|
+
lsp.Diagnostic.create(
|
|
178
|
+
lsp.Range.create(
|
|
179
|
+
document.positionAt(sortProp.key.start),
|
|
180
|
+
document.positionAt(sortProp.key.end)
|
|
181
|
+
),
|
|
182
|
+
`'${sortAttr}' is not a valid attribute of model '${effectiveModelName}'. Valid attributes: ${Object.keys(model.attributes || {}).join(', ')}`,
|
|
183
|
+
lsp.DiagnosticSeverity.Error,
|
|
184
|
+
'sails-lsp'
|
|
185
|
+
)
|
|
186
|
+
)
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
continue
|
|
190
|
+
}
|
|
191
|
+
if (arrAttr !== undefined) {
|
|
192
|
+
// For sort, allow 'foo ASC' or 'foo DESC'
|
|
193
|
+
let checkAttr = arrAttr
|
|
194
|
+
if (method === 'sort' && typeof arrAttr === 'string') {
|
|
195
|
+
checkAttr = arrAttr.split(' ')[0]
|
|
196
|
+
}
|
|
197
|
+
if (
|
|
198
|
+
!checkAttr ||
|
|
199
|
+
typeof checkAttr !== 'string' ||
|
|
200
|
+
checkAttr.trim() === ''
|
|
201
|
+
) {
|
|
202
|
+
diagnostics.push(
|
|
203
|
+
lsp.Diagnostic.create(
|
|
204
|
+
lsp.Range.create(
|
|
205
|
+
document.positionAt(el.start),
|
|
206
|
+
document.positionAt(el.end)
|
|
207
|
+
),
|
|
208
|
+
`Empty or invalid attribute in .${method}() for model '${effectiveModelName}'.`,
|
|
209
|
+
lsp.DiagnosticSeverity.Error,
|
|
210
|
+
'sails-lsp'
|
|
211
|
+
)
|
|
212
|
+
)
|
|
213
|
+
continue
|
|
214
|
+
}
|
|
215
|
+
if (
|
|
216
|
+
!model.attributes ||
|
|
217
|
+
!Object.prototype.hasOwnProperty.call(
|
|
218
|
+
model.attributes,
|
|
219
|
+
checkAttr
|
|
220
|
+
)
|
|
221
|
+
) {
|
|
222
|
+
diagnostics.push(
|
|
223
|
+
lsp.Diagnostic.create(
|
|
224
|
+
lsp.Range.create(
|
|
225
|
+
document.positionAt(el.start),
|
|
226
|
+
document.positionAt(el.end)
|
|
227
|
+
),
|
|
228
|
+
`'${checkAttr}' is not a valid attribute of model '${effectiveModelName}'. Valid attributes: ${Object.keys(model.attributes || {}).join(', ')}`,
|
|
229
|
+
lsp.DiagnosticSeverity.Error,
|
|
230
|
+
'sails-lsp'
|
|
231
|
+
)
|
|
232
|
+
)
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
// Do NOT return here; allow walker to continue to chained calls
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// Validate attributes in .find({ ... }) and similar methods
|
|
240
|
+
if (
|
|
241
|
+
node.arguments[0] &&
|
|
242
|
+
node.arguments[0].type === 'ObjectExpression'
|
|
243
|
+
) {
|
|
244
|
+
// Only validate if this is a top-level model method call, not an argument to another method (e.g., intercept)
|
|
245
|
+
// Check that the callee is a direct property of an Identifier (the model), not a nested CallExpression
|
|
246
|
+
let isTopLevelModelCall = false
|
|
247
|
+
let calleeObj = node.callee.object
|
|
248
|
+
if (calleeObj && calleeObj.type === 'Identifier') {
|
|
249
|
+
isTopLevelModelCall = true
|
|
250
|
+
} else if (calleeObj && calleeObj.type === 'CallExpression') {
|
|
251
|
+
// If the parent is a CallExpression, but the root is an Identifier, still allow
|
|
252
|
+
let root = calleeObj
|
|
253
|
+
while (
|
|
254
|
+
root &&
|
|
255
|
+
root.type === 'CallExpression' &&
|
|
256
|
+
root.callee &&
|
|
257
|
+
root.callee.type === 'MemberExpression'
|
|
258
|
+
) {
|
|
259
|
+
root = root.callee.object
|
|
260
|
+
}
|
|
261
|
+
if (root && root.type === 'Identifier') {
|
|
262
|
+
isTopLevelModelCall = true
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
// If this is an argument to a non-model method (e.g., intercept), skip validation
|
|
266
|
+
if (!isTopLevelModelCall) return
|
|
63
267
|
for (const prop of node.arguments[0].properties) {
|
|
64
268
|
const attribute = prop.key && (prop.key.name || prop.key.value)
|
|
65
269
|
const queryOptionKeys = [
|
|
@@ -80,7 +284,33 @@ module.exports = function validateModelAttributeExist(document, typeMap) {
|
|
|
80
284
|
'distinct',
|
|
81
285
|
'meta'
|
|
82
286
|
]
|
|
83
|
-
//
|
|
287
|
+
// For non-create methods, validate all top-level keys except query option keys
|
|
288
|
+
if (
|
|
289
|
+
method !== 'create' &&
|
|
290
|
+
method !== 'createEach' &&
|
|
291
|
+
!queryOptionKeys.includes(attribute)
|
|
292
|
+
) {
|
|
293
|
+
if (
|
|
294
|
+
!model.attributes ||
|
|
295
|
+
!Object.prototype.hasOwnProperty.call(
|
|
296
|
+
model.attributes,
|
|
297
|
+
attribute
|
|
298
|
+
)
|
|
299
|
+
) {
|
|
300
|
+
diagnostics.push(
|
|
301
|
+
lsp.Diagnostic.create(
|
|
302
|
+
lsp.Range.create(
|
|
303
|
+
document.positionAt(prop.key.start),
|
|
304
|
+
document.positionAt(prop.key.end)
|
|
305
|
+
),
|
|
306
|
+
`'${attribute}' is not a valid attribute of model '${effectiveModelName}'. Valid attributes: ${Object.keys(model.attributes || {}).join(', ')}`,
|
|
307
|
+
lsp.DiagnosticSeverity.Error,
|
|
308
|
+
'sails-lsp'
|
|
309
|
+
)
|
|
310
|
+
)
|
|
311
|
+
}
|
|
312
|
+
continue
|
|
313
|
+
}
|
|
84
314
|
if (
|
|
85
315
|
method !== 'create' &&
|
|
86
316
|
method !== 'createEach' &&
|
|
@@ -107,7 +337,7 @@ module.exports = function validateModelAttributeExist(document, typeMap) {
|
|
|
107
337
|
document.positionAt(whereProp.key.start),
|
|
108
338
|
document.positionAt(whereProp.key.end)
|
|
109
339
|
),
|
|
110
|
-
`'${whereAttr}' is not a valid attribute of model '${
|
|
340
|
+
`'${whereAttr}' is not a valid attribute of model '${effectiveModelName}'. Valid attributes: ${Object.keys(model.attributes || {}).join(', ')}`,
|
|
111
341
|
lsp.DiagnosticSeverity.Error,
|
|
112
342
|
'sails-lsp'
|
|
113
343
|
)
|
|
@@ -138,7 +368,7 @@ module.exports = function validateModelAttributeExist(document, typeMap) {
|
|
|
138
368
|
document.positionAt(el.start),
|
|
139
369
|
document.positionAt(el.end)
|
|
140
370
|
),
|
|
141
|
-
`'${arrAttr}' is not a valid attribute of model '${
|
|
371
|
+
`'${arrAttr}' is not a valid attribute of model '${effectiveModelName}'. Valid attributes: ${Object.keys(model.attributes || {}).join(', ')}`,
|
|
142
372
|
lsp.DiagnosticSeverity.Error,
|
|
143
373
|
'sails-lsp'
|
|
144
374
|
)
|
|
@@ -169,7 +399,7 @@ module.exports = function validateModelAttributeExist(document, typeMap) {
|
|
|
169
399
|
document.positionAt(prop.value.start),
|
|
170
400
|
document.positionAt(prop.value.end)
|
|
171
401
|
),
|
|
172
|
-
`'${sortAttr}' is not a valid attribute of model '${
|
|
402
|
+
`'${sortAttr}' is not a valid attribute of model '${effectiveModelName}'. Valid attributes: ${Object.keys(model.attributes || {}).join(', ')}`,
|
|
173
403
|
lsp.DiagnosticSeverity.Error,
|
|
174
404
|
'sails-lsp'
|
|
175
405
|
)
|
|
@@ -197,7 +427,7 @@ module.exports = function validateModelAttributeExist(document, typeMap) {
|
|
|
197
427
|
document.positionAt(sortProp.key.start),
|
|
198
428
|
document.positionAt(sortProp.key.end)
|
|
199
429
|
),
|
|
200
|
-
`'${sortAttr}' is not a valid attribute of model '${
|
|
430
|
+
`'${sortAttr}' is not a valid attribute of model '${effectiveModelName}'. Valid attributes: ${Object.keys(model.attributes || {}).join(', ')}`,
|
|
201
431
|
lsp.DiagnosticSeverity.Error,
|
|
202
432
|
'sails-lsp'
|
|
203
433
|
)
|
|
@@ -224,7 +454,7 @@ module.exports = function validateModelAttributeExist(document, typeMap) {
|
|
|
224
454
|
document.positionAt(el.start),
|
|
225
455
|
document.positionAt(el.end)
|
|
226
456
|
),
|
|
227
|
-
`'${sortAttr}' is not a valid attribute of model '${
|
|
457
|
+
`'${sortAttr}' is not a valid attribute of model '${effectiveModelName}'. Valid attributes: ${Object.keys(model.attributes || {}).join(', ')}`,
|
|
228
458
|
lsp.DiagnosticSeverity.Error,
|
|
229
459
|
'sails-lsp'
|
|
230
460
|
)
|
|
@@ -250,7 +480,7 @@ module.exports = function validateModelAttributeExist(document, typeMap) {
|
|
|
250
480
|
document.positionAt(sortProp.key.start),
|
|
251
481
|
document.positionAt(sortProp.key.end)
|
|
252
482
|
),
|
|
253
|
-
`'${sortAttr}' is not a valid attribute of model '${
|
|
483
|
+
`'${sortAttr}' is not a valid attribute of model '${effectiveModelName}'. Valid attributes: ${Object.keys(model.attributes || {}).join(', ')}`,
|
|
254
484
|
lsp.DiagnosticSeverity.Error,
|
|
255
485
|
'sails-lsp'
|
|
256
486
|
)
|
|
@@ -263,7 +493,6 @@ module.exports = function validateModelAttributeExist(document, typeMap) {
|
|
|
263
493
|
// For all other query option keys, skip validation
|
|
264
494
|
continue
|
|
265
495
|
}
|
|
266
|
-
// --- END ROBUST FIX ---
|
|
267
496
|
// Only validate top-level for create/createEach
|
|
268
497
|
if (
|
|
269
498
|
(method === 'create' || method === 'createEach') &&
|
|
@@ -279,7 +508,7 @@ module.exports = function validateModelAttributeExist(document, typeMap) {
|
|
|
279
508
|
document.positionAt(prop.key.start),
|
|
280
509
|
document.positionAt(prop.key.end)
|
|
281
510
|
),
|
|
282
|
-
`'${attribute}' is not a valid attribute of model '${
|
|
511
|
+
`'${attribute}' is not a valid attribute of model '${effectiveModelName}'. Valid attributes: ${Object.keys(model.attributes || {}).join(', ')}`,
|
|
283
512
|
lsp.DiagnosticSeverity.Error,
|
|
284
513
|
'sails-lsp'
|
|
285
514
|
)
|