Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit adc3ce8

Browse files
author
Max Schaefer
committed
Extend documentation for package and use it in two more places.
The predicate now works with an empty package path. The way this is implemented is perhaps slightly non-obvious: the `($|/)\\Q" + path + "\\E"` part of the regular expression either matches the end of the string (and `path` must then be empty), or a slash followed by `path` (which may or may not be empty). We do allow non-canonical import paths ending in `/`, which the compiler rejects. We could disallow that by putting a `(?!$)` assertion after the `/`, but that seems overkill.
1 parent 31c636f commit adc3ce8

2 files changed

Lines changed: 8 additions & 5 deletions

File tree

ql/src/semmle/go/Packages.qll

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@ class Package extends @package {
2626
}
2727

2828
/**
29-
* Gets the Go import string that may identify a package in module `mod` with the given path,
30-
* possibly modulo semantic import versioning.
29+
* Gets an import path that identifies a package in module `mod` with the given path,
30+
* possibly modulo [semantic import versioning](https://github.com/golang/go/wiki/Modules#semantic-import-versioning).
31+
*
32+
* For example, `package("github.com/go-pg/pg", "types")` gets an import path that can
33+
* refer to `"github.com/go-pg/pg/types"`, but also to `"github.com/go-pg/pg/v10/types"`.
3134
*/
3235
bindingset[result, mod, path]
3336
string package(string mod, string path) {
34-
result.regexpMatch("\\Q" + mod + "\\E([/.]v[^/]+)?/\\Q" + path + "\\E")
37+
result.regexpMatch("\\Q" + mod + "\\E([/.]v[^/]+)?($|/)\\Q" + path + "\\E")
3538
}

ql/src/semmle/go/frameworks/SQL.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ module SQL {
7676

7777
/** A string that might identify package `go-pg/pg` or a specific version of it. */
7878
bindingset[result]
79-
private string gopg() { result.regexpMatch("github.com/go-pg/pg(/v[^/]+)?") }
79+
private string gopg() { result = package("github.com/go-pg/pg", "") }
8080

8181
/** A string that might identify package `go-pg/pg/orm` or a specific version of it. */
8282
bindingset[result]
83-
private string gopgorm() { result.regexpMatch("github.com/go-pg/pg(/v[^/]+)?/orm") }
83+
private string gopgorm() { result = package("github.com/go-pg/pg", "orm") }
8484

8585
/**
8686
* A string argument to an API of `go-pg/pg` that is directly interpreted as SQL without

0 commit comments

Comments
 (0)