Skip to content

Commit

Permalink
feat: also show URLs in danger comment
Browse files Browse the repository at this point in the history
  • Loading branch information
cstrnt committed Apr 25, 2021
1 parent 0123f1d commit 166a1cb
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions dangerfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
isFunctionExpression,
isPropertyAssignment,
isStringLiteral,
isTemplateExpression,
Node,
PropertyAssignment,
ScriptTarget,
Expand All @@ -15,6 +16,8 @@ import {
visitNode,
} from "typescript";

const URL_REGEXP = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/;

export const specTransformer: TransformerFactory<SourceFile> = (context) => {
return (sourceFile) => {
const visitor = (node: Node) => {
Expand All @@ -34,11 +37,25 @@ const getFileContent = (fileContent: Node) => {
const scripts: string[] = [];
const functions: [string, string][] = [];
const pairs: [string, [string, string]][] = [];

const urls: string[] = [];
let isLastScript = false;
let lastScript: string;

const visitor = (node: Node) => {
if (isStringLiteral(node)) {
const text = node.text;
if (URL_REGEXP.test(text)) {
const matches = text.match(URL_REGEXP);
urls.push(matches[0]);
}
}
if (isTemplateExpression(node)) {
const text = fileContent.getFullText().slice(node.pos, node.end);
if (URL_REGEXP.test(text)) {
const matches = text.match(URL_REGEXP);
urls.push(matches[0]);
}
}
// PropertyAssignment === Key-Value pair in object
if (isPropertyAssignment(node)) {
const propertyKey: string = (node.name as any).escapedText;
Expand Down Expand Up @@ -81,6 +98,7 @@ const getFileContent = (fileContent: Node) => {
scripts,
functions,
pairs,
urls,
};
};

Expand Down Expand Up @@ -121,7 +139,6 @@ schedule(async () => {

const sourceFile = createSourceFile("temp", content, ScriptTarget.Latest);
const fileContent = getFileContent(sourceFile);

// START MESSAGE
message += `## ${fileName}:
### Info:
Expand Down Expand Up @@ -156,6 +173,12 @@ ${value}
.join("\n")}`
: ""
}
${
fileContent.urls.length > 0
? `### URLs:
${fileContent.urls.map((s) => `- \`${s}\``).join("\n")}`
: ""
}
`;
// END MESSAGE
// END LOOP
Expand Down

0 comments on commit 166a1cb

Please sign in to comment.