Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion javascript/ql/src/Expressions/UnknownDirective.ql
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ from Directive d
where not d instanceof KnownDirective and
// but exclude attribute top-levels: `<a href="javascript:'some-attribute-string'">`
not (d.getParent() instanceof CodeInAttribute)
select d, "Unknown directive: '" + d.getDirectiveText() + "'."
select d, "Unknown directive: '" + truncate(d.getDirectiveText(), 20, " ... (truncated)") + "'."
22 changes: 16 additions & 6 deletions javascript/ql/src/semmle/javascript/Util.qll
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,25 @@ string capitalize(string s) {
result = s.charAt(0).toUpperCase() + s.suffix(1)
}

/**
* Gets the pluralization for `n` occurrences of `noun`.
*
* For example, the pluralization of `"function"` for `n = 2` is `"functions"`.
*/
/**
* Gets the pluralization for `n` occurrences of `noun`.
*
* For example, the pluralization of `"function"` for `n = 2` is `"functions"`.
*/
bindingset[noun, n]
string pluralize(string noun, int n) {
if n = 1 then
result = noun
else
result = noun + "s"
}
}

/**
* Gets `str` or a truncated version of `str` with `explanation` appended if its length exceeds `maxLength`.
*
* For example, the truncation of `"long_string"` for `maxLength = 5` and explanation `" ..."` is `"long_ ..."`.
*/
bindingset[str, maxLength, explanation]
string truncate(string str, int maxLength, string explanation) {
if str.length() > maxLength then result = str.prefix(maxLength) + explanation else result = str
}
1 change: 1 addition & 0 deletions javascript/ql/test/library-tests/Util/truncate.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
| y | | X | XX | XXy |
3 changes: 3 additions & 0 deletions javascript/ql/test/library-tests/Util/truncate.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import semmle.javascript.Util

select truncate("X", 0, "y"), truncate("", 2, "y"), truncate("X", 2, "y"), truncate("XX", 2, "y"), truncate("XXX", 2, "y")
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@
| UnknownDirective.js:12:5:12:17 | "use struct;" | Unknown directive: 'use struct;'. |
| UnknownDirective.js:13:5:13:17 | "Use Strict"; | Unknown directive: 'Use Strict'. |
| UnknownDirective.js:14:5:14:14 | "use bar"; | Unknown directive: 'use bar'. |
| UnknownDirective.js:38:5:38:17 | "[0, 0, 0];"; | Unknown directive: '[0, 0, 0];'. |
| UnknownDirective.js:39:5:39:65 | "[0, 0, ... , 0];"; | Unknown directive: '[0, 0, 0, 0, 0, 0, 0 ... (truncated)'. |
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,8 @@ function good() {
"deps foo"; // OK
"deps bar"; // OK
}

function data() {
"[0, 0, 0];"; // NOT OK
"[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];"; // NOT OK
}