-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathsourcing.ts
231 lines (197 loc) · 7.17 KB
/
sourcing.ts
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
import * as fs from 'fs'
import * as path from 'path'
import * as LSP from 'vscode-languageserver'
import * as Parser from 'web-tree-sitter'
import { parseShellCheckDirective } from '../shellcheck/directive'
import { discriminate } from './discriminate'
import { untildify } from './fs'
import * as TreeSitterUtil from './tree-sitter'
const SOURCING_COMMANDS = ['source', '.']
export type SourceCommand = {
range: LSP.Range
uri: string | null // resolved URIs
error: string | null
}
/**
* Analysis the given tree for source commands.
*/
export function getSourceCommands({
fileUri,
rootPath,
tree,
}: {
fileUri: string
rootPath: string | null
tree: Parser.Tree
}): SourceCommand[] {
const sourceCommands: SourceCommand[] = []
const rootPaths = [path.dirname(fileUri), rootPath].filter(Boolean) as string[]
TreeSitterUtil.forEach(tree.rootNode, (node) => {
const sourcedPathInfo = getSourcedPathInfoFromNode({ node })
if (sourcedPathInfo) {
const { sourcedPath, parseError } = sourcedPathInfo
const uri = sourcedPath ? resolveSourcedUri({ rootPaths, sourcedPath }) : null
sourceCommands.push({
range: TreeSitterUtil.range(node),
uri,
error: uri ? null : parseError || 'failed to resolve path',
})
}
return true
})
return sourceCommands
}
function getSourcedPathInfoFromNode({
node,
}: {
node: Parser.SyntaxNode
}): null | { sourcedPath?: string; parseError?: string } {
if (node.type === 'command') {
const [commandNameNode, argumentNode] = node.namedChildren
if (!commandNameNode || !argumentNode) {
return null
}
if (
commandNameNode.type === 'command_name' &&
SOURCING_COMMANDS.includes(commandNameNode.text)
) {
const previousCommentNode =
node.previousSibling?.type === 'comment' ? node.previousSibling : null
if (previousCommentNode?.text.includes('shellcheck')) {
const directives = parseShellCheckDirective(previousCommentNode.text)
const sourcedPath = directives.find(discriminate('type', 'source'))?.path
if (sourcedPath === '/dev/null') {
return null
}
if (sourcedPath) {
return {
sourcedPath,
}
}
const isNotFollowErrorDisabled = !!directives
.filter(discriminate('type', 'disable'))
.flatMap(({ rules }) => rules)
.find((rule) => rule === 'SC1091')
if (isNotFollowErrorDisabled) {
return null
}
const rootFolder = directives.find(discriminate('type', 'source-path'))?.path
if (rootFolder && rootFolder !== 'SCRIPTDIR' && argumentNode.type === 'word') {
return {
sourcedPath: path.join(rootFolder, argumentNode.text),
}
}
}
const strValue = TreeSitterUtil.resolveStaticString(argumentNode)
if (strValue !== null) {
return {
sourcedPath: strValue,
}
}
// Strip one leading dynamic section.
if (argumentNode.type === 'string' && argumentNode.namedChildren.length === 1) {
const [variableNode] = argumentNode.namedChildren
if (TreeSitterUtil.isExpansion(variableNode)) {
const stringContents = argumentNode.text.slice(1, -1)
if (stringContents.startsWith(`${variableNode.text}/`)) {
return {
sourcedPath: `.${stringContents.slice(variableNode.text.length)}`,
}
}
}
}
if (argumentNode.type === 'concatenation') {
// Strip one leading dynamic section from a concatenation node.
const sourcedPath = resolveSourceFromConcatenation(argumentNode)
if (sourcedPath) {
return {
sourcedPath,
}
}
}
// TODO: we could try to parse any ShellCheck "source "directive
// # shellcheck source=src/examples/config.sh
return {
parseError: `non-constant source not supported`,
}
}
}
return null
}
/**
* Tries to resolve the given sourced path and returns a URI if possible.
* - Converts a relative paths to absolute paths
* - Converts a tilde path to an absolute path
* - Resolves the path
*
* NOTE: for future improvements:
* "If filename does not contain a slash, file names in PATH are used to find
* the directory containing filename." (see https://ss64.com/osx/source.html)
*/
function resolveSourcedUri({
rootPaths,
sourcedPath,
}: {
rootPaths: string[]
sourcedPath: string
}): string | null {
if (sourcedPath.startsWith('~')) {
sourcedPath = untildify(sourcedPath)
}
if (sourcedPath.startsWith('/')) {
if (fs.existsSync(sourcedPath)) {
return `file://${sourcedPath}`
}
return null
}
// resolve relative path
for (const rootPath of rootPaths) {
const potentialPath = path.join(rootPath.replace('file://', ''), sourcedPath)
// check if path is a file
if (fs.existsSync(potentialPath)) {
return `file://${potentialPath}`
}
}
return null
}
/*
* Resolves the source path from a concatenation node, stripping a leading dynamic directory segment.
* Returns null if the source path can't be statically determined after stripping a segment.
* Note: If a non-concatenation node is passed, null will be returned. This is likely a programmer error.
*/
function resolveSourceFromConcatenation(node: Parser.SyntaxNode): string | null {
if (node.type !== 'concatenation') return null
const stringValue = TreeSitterUtil.resolveStaticString(node)
if (stringValue !== null) return stringValue // This string is fully static.
const values: string[] = []
// Since the string must begin with the variable, the variable must be in the first child.
const [firstNode, ...rest] = node.namedChildren
// The first child is static, this means one of the other children is not!
if (TreeSitterUtil.resolveStaticString(firstNode) !== null) return null
// if the string is unquoted, the first child is the variable, so there's no more text in it.
if (!TreeSitterUtil.isExpansion(firstNode)) {
if (firstNode.namedChildCount > 1) return null // Only one variable is allowed.
// Since the string must begin with the variable, the variable must be first child.
const variableNode = firstNode.namedChildren[0] // Get the variable (quoted case)
// This is command substitution!
if (!TreeSitterUtil.isExpansion(variableNode)) return null
const stringContents = firstNode.text.slice(1, -1)
// The string doesn't start with the variable!
if (!stringContents.startsWith(variableNode.text)) return null
// Get the remaining static portion the string
values.push(stringContents.slice(variableNode.text.length))
}
for (const child of rest) {
const value = TreeSitterUtil.resolveStaticString(child)
// The other values weren't statically determinable!
if (value === null) return null
values.push(value)
}
// Join all our found static values together.
const staticResult = values.join('')
// The path starts with slash, so trim the leading variable and replace with a dot
if (staticResult.startsWith('/')) return `.${staticResult}`
// The path doesn't start with a slash, so it's invalid
// PERF: can we fail earlier than this?
return null
}