@@ -9,7 +9,6 @@ import { untildify } from './fs'
9
9
import * as TreeSitterUtil from './tree-sitter'
10
10
11
11
const SOURCING_COMMANDS = [ 'source' , '.' ]
12
- const VARIABLE_NODE_TYPES = [ 'expansion' , 'simple_expansion' ]
13
12
14
13
export type SourceCommand = {
15
14
range : LSP . Range
@@ -103,7 +102,7 @@ function getSourcedPathInfoFromNode({
103
102
}
104
103
}
105
104
106
- const strValue = resolveStaticString ( argumentNode )
105
+ const strValue = TreeSitterUtil . resolveStaticString ( argumentNode )
107
106
if ( strValue !== null ) {
108
107
return {
109
108
sourcedPath : strValue ,
@@ -113,7 +112,7 @@ function getSourcedPathInfoFromNode({
113
112
// Strip one leading dynamic section.
114
113
if ( argumentNode . type === 'string' && argumentNode . namedChildren . length === 1 ) {
115
114
const [ variableNode ] = argumentNode . namedChildren
116
- if ( VARIABLE_NODE_TYPES . includes ( variableNode . type ) ) {
115
+ if ( TreeSitterUtil . isExpansion ( variableNode ) ) {
117
116
const stringContents = argumentNode . text . slice ( 1 , - 1 )
118
117
if ( stringContents . startsWith ( `${ variableNode . text } /` ) ) {
119
118
return {
@@ -192,22 +191,22 @@ function resolveSourcedUri({
192
191
*/
193
192
function resolveSourceFromConcatenation ( node : Parser . SyntaxNode ) : string | null {
194
193
if ( node . type !== 'concatenation' ) return null
195
- const stringValue = resolveStaticString ( node )
194
+ const stringValue = TreeSitterUtil . resolveStaticString ( node )
196
195
if ( stringValue !== null ) return stringValue // This string is fully static.
197
196
198
197
const values : string [ ] = [ ]
199
198
// Since the string must begin with the variable, the variable must be in the first child.
200
199
const [ firstNode , ...rest ] = node . namedChildren
201
200
// The first child is static, this means one of the other children is not!
202
- if ( resolveStaticString ( firstNode ) !== null ) return null
201
+ if ( TreeSitterUtil . resolveStaticString ( firstNode ) !== null ) return null
203
202
204
203
// if the string is unquoted, the first child is the variable, so there's no more text in it.
205
- if ( ! VARIABLE_NODE_TYPES . includes ( firstNode . type ) ) {
204
+ if ( ! TreeSitterUtil . isExpansion ( firstNode ) ) {
206
205
if ( firstNode . namedChildCount > 1 ) return null // Only one variable is allowed.
207
206
// Since the string must begin with the variable, the variable must be first child.
208
207
const variableNode = firstNode . namedChildren [ 0 ] // Get the variable (quoted case)
209
208
// This is command substitution!
210
- if ( ! VARIABLE_NODE_TYPES . includes ( variableNode . type ) ) return null
209
+ if ( ! TreeSitterUtil . isExpansion ( variableNode ) ) return null
211
210
const stringContents = firstNode . text . slice ( 1 , - 1 )
212
211
// The string doesn't start with the variable!
213
212
if ( ! stringContents . startsWith ( variableNode . text ) ) return null
@@ -216,7 +215,7 @@ function resolveSourceFromConcatenation(node: Parser.SyntaxNode): string | null
216
215
}
217
216
218
217
for ( const child of rest ) {
219
- const value = resolveStaticString ( child )
218
+ const value = TreeSitterUtil . resolveStaticString ( child )
220
219
// The other values weren't statically determinable!
221
220
if ( value === null ) return null
222
221
values . push ( value )
@@ -230,26 +229,3 @@ function resolveSourceFromConcatenation(node: Parser.SyntaxNode): string | null
230
229
// PERF: can we fail earlier than this?
231
230
return null
232
231
}
233
-
234
- /**
235
- * Resolves the full string value of a node
236
- * Returns null if the value can't be statically determined (ie, it contains a variable or command substition).
237
- * Supports: word, string, raw_string, and concatenation
238
- */
239
- function resolveStaticString ( node : Parser . SyntaxNode ) : string | null {
240
- if ( node . type === 'concatenation' ) {
241
- const values = [ ]
242
- for ( const child of node . namedChildren ) {
243
- const value = resolveStaticString ( child )
244
- if ( value === null ) return null
245
- values . push ( value )
246
- }
247
- return values . join ( '' )
248
- }
249
- if ( node . type === 'word' ) return node . text
250
- if ( node . type === 'string' || node . type === 'raw_string' ) {
251
- if ( node . namedChildCount === 0 ) return node . text . slice ( 1 , - 1 )
252
- return null
253
- }
254
- return null
255
- }
0 commit comments