typescript#Node TypeScript Examples
The following examples show how to use
typescript#Node.
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: regexFlags.ts From elephize with MIT License | 6 votes |
extractRegexFlags = (regex: string, log: LogObj, ctx: Node) => {
let [matched, expression, flags] = regex.match(/^"\/(.*?)\/([a-zA-Z]+)?"$/) || [];
const output = {
phpFlags: '',
globalSearch: false,
expression,
};
if (!matched) {
log.error('Failed to parse regexp: %s', [regex], log.ctx(ctx));
return output;
}
if (!flags) {
flags = '';
}
if (flags.includes('g')) {
output.globalSearch = true;
flags = flags.replace('g', '');
}
if (flags.includes('i')) {
output.phpFlags += 'i';
flags = flags.replace('i', '');
}
if (flags !== '') {
log.error('Unsupported regexp modifiers encountered: %s', [flags], log.ctx(ctx));
}
return output;
}
Example #2
Source File: pluginUtils.d.ts From tsplay.dev with MIT License | 6 votes |
createUtils: (
sb: any,
) => {
/** Use this to make a few dumb element generation funcs */
el: (str: string, el: string, container: Element) => void
/** Get a relative URL for something in your dist folder depending on if you're in dev mode or not */
requireURL: (path: string) => string
/** Returns a div which has an interactive AST a TypeScript AST by passing in the root node */
createASTTree: (node: Node) => HTMLDivElement
}
Example #3
Source File: collect-import-nodes.ts From import-conductor with MIT License | 6 votes |
export function collectImportNodes(rootNode: Node): Node[] {
const importNodes: Node[] = [];
const traverse = (node: Node) => {
if (isImportDeclaration(node)) {
importNodes.push(node);
}
};
rootNode.forEachChild(traverse);
return importNodes;
}
Example #4
Source File: collect-non-import-nodes.ts From import-conductor with MIT License | 6 votes |
export function collectNonImportNodes(rootNode: Node, lastImport: Node): Node[] {
const nonImportNodes: Node[] = [];
let importsEnded = false;
const traverse = (node: Node) => {
importsEnded = importsEnded || node === lastImport;
if (!importsEnded) {
if (!isImportDeclaration(node)) {
nonImportNodes.push(node);
}
}
};
rootNode.forEachChild(traverse);
return nonImportNodes;
}
Example #5
Source File: dangerfile.ts From autocomplete with MIT License | 6 votes |
specTransformer: TransformerFactory<SourceFile> = (context) => {
return (sourceFile) => {
const visitor = (node: Node) => {
if (isPropertyAssignment(node)) {
const typedNode = node as PropertyAssignment;
if (typedNode.name.getText() === "script") {
console.log(typedNode.initializer.getText());
}
}
return visitEachChild(node, visitor, context);
};
return visitNode(sourceFile, visitor);
};
}
Example #6
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,
};
}
Example #7
Source File: measurePerf.ts From DefinitelyTyped-tools with MIT License | 4 votes |
export async function measurePerf({
packageName,
packageVersion,
typeScriptVersion,
definitelyTypedRootPath,
allPackages,
maxRunSeconds = Infinity,
progress,
nProcesses,
iterations,
tsPath,
ts,
batchRunStart,
failOnErrors,
}: MeasurePerfOptions) {
let duration = NaN;
const sourceVersion = execSync("git rev-parse HEAD", { cwd: definitelyTypedRootPath, encoding: "utf8" }).trim();
const observer = new PerformanceObserver((list) => {
const totalMeasurement = list.getEntriesByName("benchmark")[0];
duration = totalMeasurement.duration;
});
observer.observe({ entryTypes: ["measure"] });
performance.mark("benchmarkStart");
const typesPath = path.join(definitelyTypedRootPath, "types");
const typings = allPackages.getTypingsData({
name: packageName,
version: packageVersion,
});
const packagePath = path.join(typesPath, typings.subDirectoryPath);
const typesVersion = getLatestTypesVersionForTypeScriptVersion(typings.typesVersions, typeScriptVersion);
const latestTSTypesDir = path.resolve(packagePath, typesVersion ? `ts${typesVersion}` : ".");
await installDependencies(allPackages, typings.id, typesPath);
const commandLine = getParsedCommandLineForPackage(ts, latestTSTypesDir);
const testPaths = getTestFileNames(commandLine.fileNames);
let done = 0;
let lastUpdate = Date.now();
let languageServiceCrashed = false;
const testMatrix = createLanguageServiceTestMatrix(testPaths, latestTSTypesDir, commandLine.options, iterations);
if (progress) {
updateProgress(
`${toPackageKey(packageName, packageVersion)}: benchmarking over ${nProcesses} processes`,
0,
testMatrix.inputs.length
);
}
await runWithListeningChildProcesses({
inputs: testMatrix.inputs,
commandLineArgs: [],
workerFile: measureLanguageServiceWorkerFilename,
nProcesses,
crashRecovery: !failOnErrors,
cwd: process.cwd(),
softTimeoutMs: maxRunSeconds * 1000,
handleCrash: (input) => {
languageServiceCrashed = true;
console.error("Failed measurement on request:", JSON.stringify(input, undefined, 2));
},
handleOutput: (measurement: LanguageServiceSingleMeasurement) => {
testMatrix.addMeasurement(measurement);
done++;
if (progress) {
updateProgress(
`${toPackageKey(packageName, packageVersion)}: benchmarking over ${nProcesses} processes`,
done,
testMatrix.inputs.length
);
} else if (Date.now() - lastUpdate > 1000 * 60 * 5) {
// Log every 5 minutes or so to make sure Pipelines doesn’t shut us down
console.log(((100 * done) / testMatrix.inputs.length).toFixed(1) + "% done...");
lastUpdate = Date.now();
}
},
});
if (progress && done !== testMatrix.inputs.length) {
updateProgress(`${toPackageKey(packageName, packageVersion)}: timed out`, done, testMatrix.inputs.length);
process.stdout.write(os.EOL);
}
const batchCompilationInput: MeasureBatchCompilationChildProcessArgs = {
tsPath,
fileNames: commandLine.fileNames,
options: commandLine.options,
};
let batchCompilationResult: MeasureBatchCompilationChildProcessResult | undefined;
await runWithChildProcesses({
inputs: [batchCompilationInput],
workerFile: measureBatchCompilationWorkerFilename,
commandLineArgs: [],
nProcesses: 1,
handleOutput: (result: MeasureBatchCompilationChildProcessResult) => {
batchCompilationResult = result;
},
});
if (!batchCompilationResult) {
throw new Error("Failed to get batch compilation metrics");
}
performance.mark("benchmarkEnd");
performance.measure("benchmark", "benchmarkStart", "benchmarkEnd");
const measurement: PackageBenchmark = {
...batchCompilationResult,
benchmarkDuration: duration,
sourceVersion,
packageName,
packageVersionMajor: packageVersion.major,
packageVersionMinor: packageVersion.minor,
typeScriptVersion,
typeScriptVersionMajorMinor: ts.versionMajorMinor,
languageServiceBenchmarks: testMatrix.getAllBenchmarks(),
requestedLanguageServiceTestIterations: iterations,
languageServiceCrashed,
testIdentifierCount: testMatrix.uniquePositionCount,
batchRunStart,
};
return measurement;
function getIdentifiers(sourceFile: SourceFile) {
const identifiers: Node[] = [];
ts.forEachChild(sourceFile, function visit(node) {
if (ts.isIdentifier(node)) {
identifiers.push(node);
} else {
ts.forEachChild(node, visit);
}
});
return identifiers;
}
function getTestFileNames(fileNames: readonly string[]) {
return fileNames.filter((name) => {
const ext = path.extname(name);
return (ext === Extension.Ts || ext === Extension.Tsx) && !name.endsWith(Extension.Dts);
});
}
function createLanguageServiceTestMatrix(
testPaths: string[],
packageDirectory: string,
compilerOptions: CompilerOptions,
iterations: number
) {
const fileMap = new Map<string, Map<number, LanguageServiceBenchmark>>();
const inputs: MeasureLanguageServiceChildProcessArgs[] = [];
let uniquePositionCount = 0;
for (const testPath of testPaths) {
const positionMap = new Map<number, LanguageServiceBenchmark>();
fileMap.set(testPath, positionMap);
const sourceFile = ts.createSourceFile(
testPath,
ts.sys.readFile(testPath)!,
compilerOptions.target || ts.ScriptTarget.Latest
);
// Reverse: more complex examples are usually near the end of test files,
// so prioritize those.
const identifiers = getIdentifiers(sourceFile).reverse();
uniquePositionCount += identifiers.length;
// Do the loops in this order so that a single child process doesn’t
// run iterations of the same exact measurement back-to-back to avoid
// v8 optimizing a significant chunk of the work away.
for (let i = 0; i < iterations; i++) {
for (const identifier of identifiers) {
const start = identifier.getStart(sourceFile);
if (i === 0) {
const lineAndCharacter = ts.getLineAndCharacterOfPosition(sourceFile, start);
const benchmark: LanguageServiceBenchmark = {
fileName: path.relative(definitelyTypedRootPath, testPath),
start,
end: identifier.getEnd(),
identifierText: identifier.getText(sourceFile),
line: lineAndCharacter.line + 1,
offset: lineAndCharacter.character + 1,
completionsDurations: [],
quickInfoDurations: [],
};
positionMap.set(start, benchmark);
}
inputs.push({
fileName: testPath,
start,
packageDirectory,
tsPath,
});
}
}
}
return {
inputs,
uniquePositionCount,
addMeasurement: (measurement: LanguageServiceSingleMeasurement) => {
const benchmark = fileMap.get(measurement.fileName)!.get(measurement.start)!;
benchmark.completionsDurations.push(measurement.completionsDuration);
benchmark.quickInfoDurations.push(measurement.quickInfoDuration);
},
getAllBenchmarks: () => {
return Array.prototype.concat
.apply(
[],
Array.from(fileMap.values()).map((map) => Array.from(map.values()))
)
.filter(
(benchmark: LanguageServiceBenchmark) =>
benchmark.completionsDurations.length > 0 || benchmark.quickInfoDurations.length > 0
);
},
};
}
}