typescript#isStringLiteral TypeScript Examples
The following examples show how to use
typescript#isStringLiteral.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: dangerfile.ts From autocomplete with MIT License | 6 votes |
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;
// Find all scripts
if (propertyKey === "script" && isStringLiteral(node.initializer)) {
scripts.push(node.initializer.text);
lastScript = node.initializer.text;
isLastScript = true;
}
// Find all functions
if (isFunctionExpression(node.initializer)) {
if (isLastScript) {
scripts.pop();
pairs.push([
lastScript,
[
propertyKey,
fileContent
.getFullText()
.slice(node.initializer.pos, node.initializer.end),
],
]);
} else {
functions.push([
propertyKey,
fileContent
.getFullText()
.slice(node.initializer.pos, node.initializer.end),
]);
}
}
}
forEachChild(node, visitor);
};
visitor(fileContent);
return {
scripts,
functions,
pairs,
urls,
};
}