code-system-graph-core 1.2.1

Core extraction, linking, and query engine for Code System Graph.
Documentation
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
//! Test clients: supertest and Playwright in scripts, `httptest` in Go, and `MockMvc`,
//! `RestAssured`, `WebTestClient`, and `RestTemplate` in Java.

use std::collections::BTreeSet;

use super::{Consumers, canonical, join_base, receiver_call};
use crate::source_http::brace_flows::matching_open;
use crate::source_http::{Token, matching};
use crate::{SourceFramework, UrlPart, UrlTemplate};

const SUPERTEST_METHODS: [&str; 7] = ["get", "post", "put", "patch", "delete", "head", "options"];

impl Consumers<'_, '_> {
    /// `request(app).get(url)` and agents bound to `request(app)` or `request.agent(app)`.
    pub(super) fn supertest(&self, observations: &mut super::SourceObservationCollector<'_>) {
        let tokens = self.tokens();
        let locals = module_locals(tokens, "supertest");
        if locals.is_empty() {
            return;
        }
        let agents = bound_calls(tokens, &locals);
        for index in 0..tokens.len() {
            let Some(name) = tokens[index].ident() else {
                continue;
            };
            if index > 0 && tokens[index - 1].is_punct('.') {
                continue;
            }
            let (base, verb) = if locals.contains(name)
                && tokens
                    .get(index + 1)
                    .is_some_and(|token| token.is_punct('('))
            {
                let Some(close) = matching(tokens, index + 1, '(', ')') else {
                    continue;
                };
                let base = tokens
                    .get(index + 2)
                    .filter(|token| token.literal().is_some())
                    .map(|_| self.scopes.template(index + 2, close, index));
                (base, close + 1)
            } else if agents.contains(name) {
                (None, index + 1)
            } else {
                continue;
            };
            self.chained_verb(
                SourceFramework::Supertest,
                base.as_ref(),
                index,
                verb,
                observations,
            );
        }
    }

    /// Playwright `request.get(url)` through the `request` fixture, `page.request`, or a
    /// context created by `request.newContext()`.
    pub(super) fn playwright(&self, observations: &mut super::SourceObservationCollector<'_>) {
        let tokens = self.tokens();
        if !tokens
            .iter()
            .any(|token| token.literal() == Some("@playwright/test"))
        {
            return;
        }
        let mut receivers = BTreeSet::from(["request".to_owned()]);
        for index in 2..tokens.len() {
            if tokens[index].is_ident("newContext") && tokens[index - 1].is_punct('.') {
                let assign = (index.saturating_sub(8)..index)
                    .rev()
                    .find(|candidate| tokens[*candidate].is_punct('='));
                if let Some(name) = assign
                    .and_then(|assign| assign.checked_sub(1))
                    .and_then(|name| tokens[name].ident())
                {
                    receivers.insert(name.to_owned());
                }
            }
        }
        for index in 0..tokens.len() {
            let Some(name) = tokens[index].ident() else {
                continue;
            };
            let qualified =
                index < 2 || !tokens[index - 1].is_punct('.') || tokens[index - 2].is_ident("page");
            if receivers.contains(name) && qualified {
                self.chained_verb(
                    SourceFramework::Playwright,
                    None,
                    index,
                    index + 1,
                    observations,
                );
            }
        }
    }

    /// `.verb(url, ...)` starting at token `dot`, attributed to the call starting at `start`.
    fn chained_verb(
        &self,
        framework: SourceFramework,
        base: Option<&UrlTemplate>,
        start: usize,
        dot: usize,
        observations: &mut super::SourceObservationCollector<'_>,
    ) {
        let tokens = self.tokens();
        if !tokens.get(dot).is_some_and(|token| token.is_punct('.')) {
            return;
        }
        let Some(verb) = tokens.get(dot + 1).and_then(Token::ident) else {
            return;
        };
        if !SUPERTEST_METHODS.contains(&verb) {
            return;
        }
        let Some((close, arguments)) = self.arguments(dot + 2) else {
            return;
        };
        let template = self
            .argument_template(arguments.first(), start)
            .map(|path| join_base(base, path));
        self.push(
            framework,
            canonical(verb),
            template,
            (start, close),
            observations,
        );
    }

    /// `httptest.NewRequest(method, url, body)`.
    pub(super) fn httptest(&self, observations: &mut super::SourceObservationCollector<'_>) {
        let tokens = self.tokens();
        for index in 0..tokens.len() {
            let Some((receiver, call, open)) = receiver_call(tokens, index) else {
                continue;
            };
            if receiver != "httptest" || call != "NewRequest" {
                continue;
            }
            let Some((close, arguments)) = self.arguments(open) else {
                continue;
            };
            let method = super::go_method(tokens, arguments.first());
            let template = self.argument_template(arguments.get(1), index);
            self.push(
                SourceFramework::Httptest,
                method,
                template,
                (index, close),
                observations,
            );
        }
    }

    /// `mockMvc.perform(get(url))`, also through `MockMvcRequestBuilders` and `request`.
    pub(super) fn mock_mvc(&self, observations: &mut super::SourceObservationCollector<'_>) {
        let tokens = self.tokens();
        for index in 1..tokens.len() {
            if !tokens[index].is_ident("perform")
                || !tokens[index - 1].is_punct('.')
                || !tokens
                    .get(index + 1)
                    .is_some_and(|token| token.is_punct('('))
            {
                continue;
            }
            let mut builder = index + 2;
            if tokens
                .get(builder)
                .is_some_and(|token| token.is_ident("MockMvcRequestBuilders"))
                && tokens
                    .get(builder + 1)
                    .is_some_and(|token| token.is_punct('.'))
            {
                builder += 2;
            }
            let Some(verb) = tokens.get(builder).and_then(Token::ident) else {
                continue;
            };
            let Some((close, arguments)) = self.arguments(builder + 1) else {
                continue;
            };
            let (method, url) = match verb {
                "request" => (
                    java_http_method(tokens, arguments.first()),
                    arguments.get(1),
                ),
                "multipart" => (Some("POST".to_owned()), arguments.first()),
                verb => match canonical(verb) {
                    Some(method) => (Some(method), arguments.first()),
                    None => continue,
                },
            };
            let template = self.argument_template(url, index);
            self.push(
                SourceFramework::MockMvc,
                method,
                template,
                (index, close),
                observations,
            );
        }
    }

    /// `given()...when().get(url)` and `RestAssured.get(url)` chains.
    pub(super) fn rest_assured(&self, observations: &mut super::SourceObservationCollector<'_>) {
        let tokens = self.tokens();
        for index in 1..tokens.len() {
            let Some(verb) = tokens[index].ident() else {
                continue;
            };
            if !SUPERTEST_METHODS.contains(&verb)
                || !tokens[index - 1].is_punct('.')
                || !tokens
                    .get(index + 1)
                    .is_some_and(|token| token.is_punct('('))
            {
                continue;
            }
            let heads = chain_heads(tokens, index - 1);
            if !heads
                .iter()
                .any(|head| matches!(*head, "when" | "given" | "RestAssured"))
            {
                continue;
            }
            let Some((close, arguments)) = self.arguments(index + 1) else {
                continue;
            };
            let Some(template) = self.argument_template(arguments.first(), index) else {
                continue;
            };
            self.push(
                SourceFramework::RestAssured,
                canonical(verb),
                Some(template),
                (index, close),
                observations,
            );
        }
    }

    /// Calls on fields, parameters, and variables declared as `RestTemplate` or
    /// `TestRestTemplate`.
    pub(super) fn rest_template(&self, observations: &mut super::SourceObservationCollector<'_>) {
        let tokens = self.tokens();
        let mut receivers = Vec::new();
        for index in 0..tokens.len().saturating_sub(2) {
            let framework = match tokens[index].ident() {
                Some("RestTemplate") => SourceFramework::RestTemplate,
                Some("TestRestTemplate") => SourceFramework::TestRestTemplate,
                _ => continue,
            };
            let declares = tokens[index + 2].is_punct(';')
                || tokens[index + 2].is_punct('=')
                || tokens[index + 2].is_punct(',')
                || tokens[index + 2].is_punct(')');
            if let Some(name) = tokens[index + 1].ident().filter(|_| declares) {
                receivers.push((name.to_owned(), framework));
            }
        }
        if receivers.is_empty() {
            return;
        }
        for index in 0..tokens.len() {
            let Some((receiver, call, open)) = receiver_call(tokens, index) else {
                continue;
            };
            let receiver = receiver.strip_prefix("this.").unwrap_or(&receiver);
            let Some((_, framework)) = receivers.iter().find(|(name, _)| name == receiver) else {
                continue;
            };
            let Some((close, arguments)) = self.arguments(open) else {
                continue;
            };
            let method = match call.as_str() {
                "getForObject" | "getForEntity" => Some("GET".to_owned()),
                "postForObject" | "postForEntity" | "postForLocation" => Some("POST".to_owned()),
                "put" => Some("PUT".to_owned()),
                "delete" => Some("DELETE".to_owned()),
                "patchForObject" => Some("PATCH".to_owned()),
                "headForHeaders" => Some("HEAD".to_owned()),
                "optionsForAllow" => Some("OPTIONS".to_owned()),
                "exchange" => java_http_method(tokens, arguments.get(1)),
                _ => continue,
            };
            let template = self.argument_template(arguments.first(), index);
            self.push(*framework, method, template, (index, close), observations);
        }
    }
}

/// Whether `template` is addressed to a Go `httptest` server: it starts with the `URL` field of
/// one of `servers`, or it is a relative path in a file that uses `httptest`.
pub(super) fn targets_httptest(
    template: &UrlTemplate,
    servers: &[String],
    uses_httptest: bool,
) -> bool {
    match template.parts.first() {
        Some(UrlPart::Value(Some(name))) => name
            .strip_suffix(".URL")
            .is_some_and(|server| servers.iter().any(|candidate| candidate == server)),
        Some(UrlPart::Text(text)) => uses_httptest && text.starts_with('/'),
        _ => false,
    }
}

/// Names bound to `httptest.NewServer`, `NewTLSServer`, or `NewUnstartedServer`.
pub(super) fn httptest_servers(tokens: &[Token]) -> Vec<String> {
    let mut servers = Vec::new();
    for index in 0..tokens.len() {
        let Some((receiver, call, _)) = receiver_call(tokens, index) else {
            continue;
        };
        if receiver != "httptest"
            || !matches!(
                call.as_str(),
                "NewServer" | "NewTLSServer" | "NewUnstartedServer"
            )
        {
            continue;
        }
        let Some(assign) = index
            .checked_sub(1)
            .filter(|assign| tokens[*assign].is_punct('='))
        else {
            continue;
        };
        let target = if assign > 0 && tokens[assign - 1].is_punct(':') {
            assign - 1
        } else {
            assign
        };
        if let Some(name) = target.checked_sub(1).and_then(|name| tokens[name].ident()) {
            servers.push(name.to_owned());
        }
    }
    servers
}

/// `HttpMethod.X` argument of a Spring call.
fn java_http_method(tokens: &[Token], argument: Option<&(usize, usize)>) -> Option<String> {
    let (start, end) = *argument?;
    (end == start + 3 && tokens[start].is_ident("HttpMethod") && tokens[start + 1].is_punct('.'))
        .then(|| tokens[start + 2].ident())
        .flatten()
        .and_then(canonical)
}

/// Identifiers of the calls and names chained before the `.` at token `dot`.
fn chain_heads(tokens: &[Token], dot: usize) -> Vec<&str> {
    let mut heads = Vec::new();
    let mut cursor = dot;
    while cursor > 0 && tokens[cursor].is_punct('.') {
        let mut previous = cursor - 1;
        if tokens[previous].is_punct(')') {
            let Some(open) = matching_open(tokens, previous) else {
                break;
            };
            let Some(callee) = open.checked_sub(1) else {
                break;
            };
            previous = callee;
        }
        let Some(name) = tokens[previous].ident() else {
            break;
        };
        heads.push(name);
        let Some(next) = previous.checked_sub(1) else {
            break;
        };
        cursor = next;
    }
    heads
}

/// Local names bound to the default or namespace export of the script module `module`.
fn module_locals(tokens: &[Token], module: &str) -> BTreeSet<String> {
    let mut locals = BTreeSet::new();
    for index in 0..tokens.len() {
        if tokens[index].literal() != Some(module) || index < 2 {
            continue;
        }
        if tokens[index - 1].is_ident("from") {
            let Some(import) = (index.saturating_sub(16)..index)
                .rev()
                .find(|candidate| tokens[*candidate].is_ident("import"))
            else {
                continue;
            };
            let clause = &tokens[import + 1..index - 1];
            if let Some(name) = clause
                .first()
                .and_then(Token::ident)
                .filter(|name| *name != "type")
            {
                locals.insert(name.to_owned());
            } else if let [star, alias, name, ..] = clause
                && star.is_punct('*')
                && alias.is_ident("as")
                && let Some(name) = name.ident()
            {
                locals.insert(name.to_owned());
            }
        } else if tokens[index - 1].is_punct('(')
            && tokens[index - 2].is_ident("require")
            && index >= 4
            && tokens[index - 3].is_punct('=')
            && let Some(name) = tokens[index - 4].ident()
        {
            locals.insert(name.to_owned());
        }
    }
    locals
}

/// Names bound to a call of one of `callees`, directly or through one member such as `agent`.
fn bound_calls(tokens: &[Token], callees: &BTreeSet<String>) -> BTreeSet<String> {
    let mut names = BTreeSet::new();
    for index in 2..tokens.len() {
        let Some(callee) = tokens[index].ident() else {
            continue;
        };
        if !callees.contains(callee) || !tokens[index - 1].is_punct('=') {
            continue;
        }
        let direct = tokens
            .get(index + 1)
            .is_some_and(|token| token.is_punct('('));
        let member = tokens
            .get(index + 1)
            .is_some_and(|token| token.is_punct('.'))
            && tokens
                .get(index + 3)
                .is_some_and(|token| token.is_punct('('));
        if !direct && !member {
            continue;
        }
        if let Some(name) = tokens[index - 2].ident() {
            names.insert(name.to_owned());
        }
    }
    names
}