Skip to content

Commit d1eb31e

Browse files
Finish creating check for non-printing characters
1 parent 245db7c commit d1eb31e

2 files changed

Lines changed: 33 additions & 36 deletions

File tree

extensions/ql-vscode/src/view/RawTableValue.tsx

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,14 @@ interface Props {
88
databaseUri: string;
99
}
1010

11-
const codes: { [key: string]: any } = {
12-
'\0': 'U+0000',
13-
'\b': 'U+0008',
14-
'\t': 'U+0009',
15-
'\v': 'U+000B',
16-
'\r': 'U+000D'
17-
};
18-
19-
function onlyNewLine(element: string, index: number, array: string[]) {
20-
return element === '\n';
21-
}
22-
2311
export default function RawTableValue(props: Props): JSX.Element {
2412
const rawValue = props.value;
25-
2613
if (
2714
typeof rawValue === 'string'
2815
|| typeof rawValue === 'number'
2916
|| typeof rawValue === 'boolean'
3017
) {
31-
const text = rawValue.toString();
32-
const newVal = text.split('').filter((element: string) => element !== ' ');
33-
if (newVal.every(onlyNewLine)) {
34-
const cleanVal = '[' + newVal.join('') + ']';
35-
return <span>{cleanVal}</span>;
36-
} else {
37-
for (let i = 0; i < newVal.length; i++) {
38-
newVal[i] = codes[newVal[i]] || newVal[i];
39-
}
40-
const cleanVal = newVal.join('');
41-
return <span>{cleanVal}</span>;
42-
}
18+
return <span>{renderLocation(undefined, rawValue.toString())}</span>;
4319
}
4420

4521
return renderLocation(rawValue.url, rawValue.label, props.databaseUri);

extensions/ql-vscode/src/view/result-table-utils.tsx

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ export const oddRowClassName = 'vscode-codeql__result-table-row--odd';
3737
export const pathRowClassName = 'vscode-codeql__result-table-row--path';
3838
export const selectedRowClassName = 'vscode-codeql__result-table-row--selected';
3939

40+
const CONTROL_CODE = '\u001F'.codePointAt(0)!;
41+
const CONTROL_LABEL = '\u2400'.codePointAt(0)!;
42+
4043
export function jumpToLocationHandler(
4144
loc: ResolvableLocationValue,
4245
databaseUri: string,
@@ -67,24 +70,42 @@ export function openFile(filePath: string): void {
6770
});
6871
}
6972

73+
function convertedNonprintableChars(label: string) {
74+
// If the label was empty, use a placeholder instead, so the link is still clickable.
75+
if (!label) {
76+
return '[empty string]';
77+
} else if (label.match(/^\s+$/)) {
78+
return `[whitespace: "${label}"]`;
79+
} else {
80+
/**
81+
* If the label contains certain non-printable characters, loop through each
82+
* character and replace it with the cooresponding unicode control label.
83+
*/
84+
const convertedLabelArray: any[] = [];
85+
for (let i = 0; i < label.length; i++) {
86+
const labelCheck = label.codePointAt(i)!;
87+
if (labelCheck <= CONTROL_CODE) {
88+
convertedLabelArray[i] = String.fromCodePoint(labelCheck + CONTROL_LABEL);
89+
} else {
90+
convertedLabelArray[i] = label.charAt(i);
91+
}
92+
}
93+
return convertedLabelArray.join('');
94+
}
95+
}
96+
7097
/**
7198
* Render a location as a link which when clicked displays the original location.
7299
*/
73100
export function renderLocation(
74-
loc: UrlValue | undefined,
75-
label: string | undefined,
76-
databaseUri: string,
101+
loc?: UrlValue,
102+
label?: string,
103+
databaseUri?: string,
77104
title?: string,
78105
callback?: () => void
79106
): JSX.Element {
80107

81-
// If the label was empty, use a placeholder instead, so the link is still clickable.
82-
let displayLabel = label;
83-
if (!label) {
84-
displayLabel = '[empty string]';
85-
} else if (label.match(/^\s+$/)) {
86-
displayLabel = `[whitespace: "${label}"]`;
87-
}
108+
const displayLabel = convertedNonprintableChars(label!);
88109

89110
if (loc === undefined) {
90111
return <span>{displayLabel}</span>;
@@ -93,7 +114,7 @@ export function renderLocation(
93114
}
94115

95116
const resolvableLoc = tryGetResolvableLocation(loc);
96-
if (resolvableLoc !== undefined) {
117+
if (databaseUri !== undefined && resolvableLoc !== undefined) {
97118
return (
98119
<a href="#"
99120
className="vscode-codeql__result-table-location-link"

0 commit comments

Comments
 (0)