Extract BQRS locations from string results

This commit is contained in:
Andrew Eisenberg 2020-05-07 12:45:40 -07:00 коммит произвёл Andrew Eisenberg
Родитель 010ae64da3
Коммит c3e3390647
2 изменённых файлов: 59 добавлений и 30 удалений

Просмотреть файл

@ -1,6 +1,6 @@
import { expect } from 'chai';
import 'mocha';
import { LocationStyle, StringLocation, tryGetWholeFileLocation } from 'semmle-bqrs';
import { LocationStyle, StringLocation, tryGetResolvableLocation } from 'semmle-bqrs';
describe('processing string locations', function () {
it('should detect Windows whole-file locations', function () {
@ -8,7 +8,7 @@ describe('processing string locations', function () {
t: LocationStyle.String,
loc: 'file://C:/path/to/file.ext:0:0:0:0'
};
const wholeFileLoc = tryGetWholeFileLocation(loc);
const wholeFileLoc = tryGetResolvableLocation(loc);
expect(wholeFileLoc).to.eql({t: LocationStyle.WholeFile, file: 'C:/path/to/file.ext'});
});
it('should detect Unix whole-file locations', function () {
@ -16,12 +16,27 @@ describe('processing string locations', function () {
t: LocationStyle.String,
loc: 'file:///path/to/file.ext:0:0:0:0'
};
const wholeFileLoc = tryGetWholeFileLocation(loc);
const wholeFileLoc = tryGetResolvableLocation(loc);
expect(wholeFileLoc).to.eql({t: LocationStyle.WholeFile, file: '/path/to/file.ext'});
});
it('should detect Unix 5-part locations', function () {
const loc: StringLocation = {
t: LocationStyle.String,
loc: 'file:///path/to/file.ext:1:2:3:4'
};
const wholeFileLoc = tryGetResolvableLocation(loc);
expect(wholeFileLoc).to.eql({
t: LocationStyle.FivePart,
file: '/path/to/file.ext',
lineStart: 1,
colStart: 2,
lineEnd: 3,
colEnd: 4
});
});
it('should ignore other string locations', function () {
for (const loc of ['file:///path/to/file.ext', 'I am not a location']) {
const wholeFileLoc = tryGetWholeFileLocation({
const wholeFileLoc = tryGetResolvableLocation({
t: LocationStyle.String,
loc: loc
});

Просмотреть файл

@ -1,4 +1,4 @@
import { LocationStyle } from './bqrs-schema';
import { LocationStyle } from "./bqrs-schema";
// See https://help.semmle.com/QL/learn-ql/ql/locations.html for how these are used.
export interface FivePartLocation {
@ -31,54 +31,69 @@ export type LocationValue = RawLocationValue | WholeFileLocation;
/** A location that may be resolved to a source code element. */
export type ResolvableLocationValue = FivePartLocation | WholeFileLocation;
/**
* The CodeQL filesystem libraries use this pattern in `getURL()` predicates
* to describe the location of an entire filesystem resource.
* Such locations appear as `StringLocation`s instead of `FivePartLocation`s.
*
*
* Folder resources also get similar URLs, but with the `folder` scheme.
* They are deliberately ignored here, since there is no suitable location to show the user.
*/
const WHOLE_FILE_LOCATION_REGEX = /file:\/\/(.+):0:0:0:0/;
const FILE_LOCATION_REGEX = /file:\/\/(.+):([0-9]+):([0-9]+):([0-9]+):([0-9]+)/;
/**
* Gets a resolvable source file location for the specified `LocationValue`, if possible.
* @param loc The location to test.
*/
export function tryGetResolvableLocation(loc: LocationValue | undefined): ResolvableLocationValue | undefined {
export function tryGetResolvableLocation(
loc: LocationValue | undefined
): ResolvableLocationValue | undefined {
if (loc === undefined) {
return undefined;
}
else if ((loc.t === LocationStyle.FivePart) && loc.file) {
} else if (loc.t === LocationStyle.FivePart && loc.file) {
return loc;
}
else if ((loc.t === LocationStyle.WholeFile) && loc.file) {
} else if (loc.t === LocationStyle.WholeFile && loc.file) {
return loc;
}
else if ((loc.t === LocationStyle.String) && loc.loc) {
return tryGetWholeFileLocation(loc);
}
else {
} else if (loc.t === LocationStyle.String && loc.loc) {
return tryGetLocationFromString(loc);
} else {
return undefined;
}
}
export function tryGetWholeFileLocation(loc: StringLocation): WholeFileLocation | undefined {
const matches = WHOLE_FILE_LOCATION_REGEX.exec(loc.loc);
export function tryGetLocationFromString(
loc: StringLocation
): ResolvableLocationValue | undefined {
const matches = FILE_LOCATION_REGEX.exec(loc.loc);
if (matches && matches.length > 1 && matches[1]) {
// Whole-file location.
// We could represent this as a FivePartLocation with all numeric fields set to zero,
// but that would be a deliberate misuse as those fields are intended to be 1-based.
return {
t: LocationStyle.WholeFile,
file: matches[1]
};
if (isWholeFileMatch(matches)) {
return {
t: LocationStyle.WholeFile,
file: matches[1],
};
} else {
return {
t: LocationStyle.FivePart,
file: matches[1],
lineStart: Number(matches[2]),
colStart: Number(matches[3]),
lineEnd: Number(matches[4]),
colEnd: Number(matches[5]),
}
}
} else {
return undefined;
}
}
function isWholeFileMatch(matches: RegExpExecArray): boolean {
return (
matches[2] === "0" &&
matches[3] === "0" &&
matches[4] === "0" &&
matches[5] === "0"
);
}
export interface ElementBase {
id: PrimitiveColumnValue;
label?: string;
@ -93,8 +108,7 @@ export interface ElementWithLocation extends ElementBase {
location: LocationValue;
}
export interface Element extends Required<ElementBase> {
}
export interface Element extends Required<ElementBase> {}
export type PrimitiveColumnValue = string | boolean | number | Date;
export type ColumnValue = PrimitiveColumnValue | ElementBase;