From ac5434b3f33dcc6c8a4b93d7c53782352487962b Mon Sep 17 00:00:00 2001 From: tiferet Date: Tue, 13 Dec 2022 11:28:55 -0800 Subject: [PATCH] Minor prompt improvements: Remove spaces that break the code syntax or make for strange code styling. --- .../adaptivethreatmodeling/ModelPrompt.qll | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/javascript/ql/experimental/adaptivethreatmodeling/lib/experimental/adaptivethreatmodeling/ModelPrompt.qll b/javascript/ql/experimental/adaptivethreatmodeling/lib/experimental/adaptivethreatmodeling/ModelPrompt.qll index 0b42113f61d..9a0266458d6 100644 --- a/javascript/ql/experimental/adaptivethreatmodeling/lib/experimental/adaptivethreatmodeling/ModelPrompt.qll +++ b/javascript/ql/experimental/adaptivethreatmodeling/lib/experimental/adaptivethreatmodeling/ModelPrompt.qll @@ -61,21 +61,13 @@ module ModelPrompt { /** * Gets the reconstructed source code text for a range of locations. * TODO: This excludes comments - * TODO: Don't add a space if the current or previous token is a period. */ string tokenize(Location location) { result = strictconcat(Token token | location.containsLoosely(token.getLocation()) | - token - .getValue() - .replaceAll("\"", " ") - .replaceAll("\\", " ") - .replaceAll("\n", " ") - .replaceAll("\r", " ") - .replaceAll("|", " ") - .replaceAll("`", " ") + + token.getValue() + // Use space as the separator, since that is most likely. // May not be an exact reconstruction, e.g. if the code // had newlines between successive tokens. @@ -83,6 +75,21 @@ module ModelPrompt { order by token.getLocation().getStartLine(), token.getLocation().getStartColumn() ) + // Remove some characters that have special meaning in the markdown table prompt, or that are not allowed in + //the HOP: + .replaceAll("\"", " ") + .replaceAll("\\", " ") + .replaceAll("\n", " ") + .replaceAll("\r", " ") + .replaceAll("|", " ") + .replaceAll("`", " ") + // Remove spaces that break the code syntax or make for strange code styling: + .replaceAll(" . ", ".") + .replaceAll(" :", ":") + .replaceAll(" ,", ",") + .replaceAll(" (", "(") + .replaceAll(" )", ")") + .replaceAll("( ", "(") } /** @@ -106,7 +113,7 @@ module ModelPrompt { loc.containsLoosely(node.getAstNode().getLocation()) and loc.getStartLine() >= node.getAstNode().getLocation().getStartLine() - neighborhoodSize and loc.getEndLine() <= node.getAstNode().getLocation().getEndLine() + neighborhoodSize - | + | loc order by loc.getNumLines(), loc.getEndColumn() - loc.getStartColumn(), loc.getEndColumn(),