()
| 7 | ) |
| 8 | |
| 9 | func matchJQuery() URLMatcher { |
| 10 | |
| 11 | return URLMatcher{"call_expression", func(n *Node) *URL { |
| 12 | callName := n.ChildByFieldName("function").Content() |
| 13 | |
| 14 | if !slices.Contains( |
| 15 | []string{ |
| 16 | "$.get", "$.post", "$.ajax", |
| 17 | "jQuery.get", "jQuery.post", "jQuery.ajax", |
| 18 | }, |
| 19 | callName, |
| 20 | ) { |
| 21 | return nil |
| 22 | } |
| 23 | |
| 24 | // The jQuery ajax calls have a few different call signatures |
| 25 | // that we need to account for: |
| 26 | // jQuery.post( url [, data ] [, success ] [, dataType ] ) |
| 27 | // jQuery.get( url [, data ] [, success ] [, dataType ] ) |
| 28 | // jQuery.ajax( url [, settings ] ) |
| 29 | // jQuery.post( [settings] ) |
| 30 | // jQuery.get( [settings] ) |
| 31 | // jQuery.ajax( [settings] ) |
| 32 | // |
| 33 | // So we end up with three scenarios to deal with: |
| 34 | // 1. The URL comes first, then a data object |
| 35 | // 2. The URL comes first, then a settings object |
| 36 | // 3. A settings object comes first. |
| 37 | arguments := n.ChildByFieldName("arguments") |
| 38 | if arguments == nil { |
| 39 | return nil |
| 40 | } |
| 41 | |
| 42 | firstArg := arguments.NamedChild(0) |
| 43 | if firstArg == nil { |
| 44 | return nil |
| 45 | } |
| 46 | secondArg := arguments.NamedChild(1) |
| 47 | |
| 48 | m := &URL{ |
| 49 | Type: callName, |
| 50 | Source: n.Content(), |
| 51 | } |
| 52 | |
| 53 | // Infer the method for .post and .get calls |
| 54 | if strings.HasSuffix(callName, ".post") { |
| 55 | m.Method = "POST" |
| 56 | } else if strings.HasSuffix(callName, ".get") { |
| 57 | m.Method = "GET" |
| 58 | } |
| 59 | |
| 60 | var settingsNode *Node |
| 61 | |
| 62 | if firstArg.IsStringy() { |
| 63 | // first argument is the URL |
| 64 | m.URL = firstArg.CollapsedString() |
| 65 | |
| 66 | // If the first arg is a URL, the second arg is a |
no test coverage detected