Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function getSourceFile() {
if (isFirstOverload) {
if (node.kind === SyntaxKind.SourceFile)
return undefined;
const topParent = getNodeSourceFile();
if (topParent == null) {
// create a result file (see https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API)
const scriptKind = getScriptKind();
return ts.createSourceFile(`print.${getFileExt(scriptKind)}`, "", ScriptTarget.Latest, false, scriptKind);
}
return topParent;
}
return sourceFileOrOptions as ts.SourceFile;
function getScriptKind() {
return options.scriptKind ?? ScriptKind.TSX;
}
import { SyntaxKind } from "@ts-morph/common";
import * as compiler from "../compiler";
// when changing this, make sure to run `yarn code-generate`.
// that will automatically update all other parts of the application that need to be updated when this changes.
// using an "unknown" type here because I couldn't figure out a way of getting the typescript compiler to understand this
export const kindToWrapperMappings: { [key: number]: unknown; } = {
[SyntaxKind.SourceFile]: compiler.SourceFile,
[SyntaxKind.ArrayBindingPattern]: compiler.ArrayBindingPattern,
[SyntaxKind.ArrayLiteralExpression]: compiler.ArrayLiteralExpression,
[SyntaxKind.ArrayType]: compiler.ArrayTypeNode,
[SyntaxKind.ArrowFunction]: compiler.ArrowFunction,
[SyntaxKind.AsExpression]: compiler.AsExpression,
[SyntaxKind.AwaitExpression]: compiler.AwaitExpression,
[SyntaxKind.BindingElement]: compiler.BindingElement,
[SyntaxKind.BinaryExpression]: compiler.BinaryExpression,
[SyntaxKind.Block]: compiler.Block,
[SyntaxKind.BreakStatement]: compiler.BreakStatement,
[SyntaxKind.CallExpression]: compiler.CallExpression,
[SyntaxKind.CallSignature]: compiler.CallSignatureDeclaration,
[SyntaxKind.CaseBlock]: compiler.CaseBlock,
[SyntaxKind.CaseClause]: compiler.CaseClause,
[SyntaxKind.CatchClause]: compiler.CatchClause,
[SyntaxKind.ClassDeclaration]: compiler.ClassDeclaration,
case SyntaxKind.ConstructSignature:
case SyntaxKind.IndexSignature:
case SyntaxKind.InterfaceDeclaration:
case SyntaxKind.MethodSignature:
case SyntaxKind.PropertySignature:
case SyntaxKind.JsxAttribute:
case SyntaxKind.JsxElement:
case SyntaxKind.JsxSelfClosingElement:
case SyntaxKind.JsxSpreadAttribute:
case SyntaxKind.ExportAssignment:
case SyntaxKind.ExportDeclaration:
case SyntaxKind.ExportSpecifier:
case SyntaxKind.ImportDeclaration:
case SyntaxKind.ImportSpecifier:
case SyntaxKind.ModuleDeclaration:
case SyntaxKind.SourceFile:
case SyntaxKind.VariableStatement:
case SyntaxKind.TypeAliasDeclaration:
case SyntaxKind.TypeParameter:
case SyntaxKind.VariableDeclaration:
case SyntaxKind.PropertyAssignment:
case SyntaxKind.ShorthandPropertyAssignment:
case SyntaxKind.SpreadAssignment:
return true;
default:
return false;
}
}
}
case SyntaxKind.FunctionExpression:
case SyntaxKind.CallSignature:
case SyntaxKind.ConstructSignature:
case SyntaxKind.MethodSignature:
case SyntaxKind.ConstructorType:
case SyntaxKind.FunctionType:
return true;
default:
return false;
}
}
/**
* Gets if the node is a SourceFile.
*/
static readonly isSourceFile: (node: compiler.Node) => node is compiler.SourceFile = TypeGuards.is(SyntaxKind.SourceFile);
/**
* Gets if the node is a SpreadAssignment.
*/
static readonly isSpreadAssignment: (node: compiler.Node) => node is compiler.SpreadAssignment = TypeGuards.is(SyntaxKind.SpreadAssignment);
/**
* Gets if the node is a SpreadElement.
*/
static readonly isSpreadElement: (node: compiler.Node) => node is compiler.SpreadElement = TypeGuards.is(SyntaxKind.SpreadElement);
/**
* Gets if the node is a Statement.
* @param node - Node to check.
*/
static isStatement(node: compiler.Node): node is compiler.Statement {
switch (node.getKind()) {
case SyntaxKind.ClassDeclaration:
function getSourceFileFromNode(compilerNode: ts.Node) {
if (compilerNode.kind === SyntaxKind.SourceFile)
return compilerNode as ts.SourceFile;
if (compilerNode.parent == null)
throw new errors.InvalidOperationError("Please ensure the node was created from a source file with 'setParentNodes' set to 'true'.");
let parent = compilerNode;
while (parent.parent != null)
parent = parent.parent;
if (parent.kind !== SyntaxKind.SourceFile)
throw new errors.NotImplementedError("For some reason the top parent was not a source file.");
return parent as ts.SourceFile;
}
}
export function printNode(node: ts.Node, sourceFileOrOptions?: PrintNodeOptions | ts.SourceFile, secondOverloadOptions?: PrintNodeOptions) {
const isFirstOverload = sourceFileOrOptions == null || (sourceFileOrOptions as ts.SourceFile).kind !== SyntaxKind.SourceFile;
const options = getOptions();
const sourceFile = getSourceFile();
const printer = ts.createPrinter({
newLine: options.newLineKind ?? NewLineKind.LineFeed,
removeComments: options.removeComments || false
});
if (sourceFile == null)
return printer.printFile(node as ts.SourceFile);
else
return printer.printNode(options.emitHint ?? EmitHint.Unspecified, node, sourceFile);
function getSourceFile() {
if (isFirstOverload) {
if (node.kind === SyntaxKind.SourceFile)
function getSourceFileFromNode(compilerNode: ts.Node) {
if (compilerNode.kind === SyntaxKind.SourceFile)
return compilerNode as ts.SourceFile;
if (compilerNode.parent == null)
throw new errors.InvalidOperationError("Please ensure the node was created from a source file with 'setParentNodes' set to 'true'.");
let parent = compilerNode;
while (parent.parent != null)
parent = parent.parent;
if (parent.kind !== SyntaxKind.SourceFile)
throw new errors.NotImplementedError("For some reason the top parent was not a source file.");
return parent as ts.SourceFile;
}
}
| ts.ModuleBlock
| ts.CaseClause
| ts.DefaultClause;
export type ContainerNodes = StatementContainerNodes
| ts.ClassDeclaration
| ts.InterfaceDeclaration
| ts.EnumDeclaration
| ts.ClassExpression
| ts.TypeLiteralNode
| ts.ObjectLiteralExpression;
type CommentSyntaxKinds = SyntaxKind.SingleLineCommentTrivia | SyntaxKind.MultiLineCommentTrivia;
const childrenSaver = new WeakMap();
const commentNodeParserKinds = new Set([
SyntaxKind.SourceFile,
SyntaxKind.Block,
SyntaxKind.ModuleBlock,
SyntaxKind.CaseClause,
SyntaxKind.DefaultClause,
SyntaxKind.ClassDeclaration,
SyntaxKind.InterfaceDeclaration,
SyntaxKind.EnumDeclaration,
SyntaxKind.ClassExpression,
SyntaxKind.TypeLiteral,
SyntaxKind.ObjectLiteralExpression
]);
export class CommentNodeParser {
private constructor() {
}