How to use the @ts-morph/common.SyntaxKind.SyntaxList function in @ts-morph/common

To help you get started, we’ve selected a few @ts-morph/common examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github dsherret / ts-morph / packages / ts-morph / src / manipulation / nodeHandlers / NodeHandlerHelper.ts View on Github external
handleForValues(handler: NodeHandler, currentNode: ts.Node, newNode: ts.Node, newSourceFile: ts.SourceFile) {
        if (this.compilerFactory.hasCompilerNode(currentNode))
            handler.handleNode(this.compilerFactory.getExistingNodeFromCompilerNode(currentNode)!, newNode, newSourceFile);
        else if (currentNode.kind === SyntaxKind.SyntaxList) {
            // always handle syntax lists because their children might be in the cache
            // todo: pass this in for performance reasons
            const sourceFile = this.compilerFactory.getExistingNodeFromCompilerNode(currentNode.getSourceFile())! as SourceFile;
            handler.handleNode(this.compilerFactory.getNodeFromCompilerNode(currentNode, sourceFile), newNode, newSourceFile);
        }
    }
github dsherret / ts-morph / packages / ts-morph / src / compiler / ast / base / export / ExportableNode.ts View on Github external
if (value && !TypeGuards.isSourceFile(this.getParentOrThrow()))
                throw new errors.InvalidOperationError("The parent must be a source file in order to set this node as a default export.");

            // remove any existing default export
            const sourceFile = this.getSourceFile();
            const fileDefaultExportSymbol = sourceFile.getDefaultExportSymbol();

            if (fileDefaultExportSymbol != null)
                sourceFile.removeDefaultExport(fileDefaultExportSymbol);

            if (!value)
                return this;

            // set this node as the one to default export
            if (TypeGuards.hasName(this) && shouldWriteAsSeparateStatement.call(this)) {
                const parentSyntaxList = this.getFirstAncestorByKindOrThrow(SyntaxKind.SyntaxList);
                const name = this.getName();

                parentSyntaxList.insertChildText(this.getChildIndex() + 1, writer => {
                    writer.newLine().write(`export default ${name};`);
                });
            }
            else {
                this.addModifier("export");
                this.addModifier("default");
            }

            return this;

            function shouldWriteAsSeparateStatement(this: Node) {
                if (TypeGuards.isEnumDeclaration(this) || TypeGuards.isNamespaceDeclaration(this) || TypeGuards.isTypeAliasDeclaration(this))
                    return true;
github dsherret / ts-morph / packages / ts-morph / src / compiler / ast / jsx / base / JsxAttributedNode.ts View on Github external
insertAttributes(index: number, structures: ReadonlyArray | JsxSpreadAttributeStructure>) {
            if (structures.length === 0)
                return [];

            const originalChildrenCount = this.compilerNode.attributes.properties.length;
            index = verifyAndGetIndex(index, originalChildrenCount);

            const insertPos = index === 0 ? this.getTagNameNode().getEnd() : this.getAttributes()[index - 1].getEnd();
            const writer = this._getWriterWithQueuedChildIndentation();
            const structuresPrinter = new SpaceFormattingStructuresPrinter(this._context.structurePrinterFactory.forJsxAttributeDecider());
            structuresPrinter.printText(writer, structures);

            insertIntoParentTextRange({
                insertPos,
                newText: " " + writer.toString(),
                parent: this.getNodeProperty("attributes").getFirstChildByKindOrThrow(SyntaxKind.SyntaxList)
            });

            return getNodesToReturn(originalChildrenCount, this.getAttributes(), index, false);
        }
github dsherret / ts-morph / packages / ts-morph / src / utils / TypeGuards.ts View on Github external
static isSuperExpression(node: compiler.Node): node is compiler.SuperExpression {
        return node.getKind() === SyntaxKind.SuperKeyword;
    }

    /**
     * Gets if the node is a SwitchStatement.
     */
    static readonly isSwitchStatement: (node: compiler.Node) => node is compiler.SwitchStatement = TypeGuards.is(SyntaxKind.SwitchStatement);
    /**
     * Gets if the node is a SymbolKeyword.
     */
    static readonly isSymbolKeyword: (node: compiler.Node) => node is compiler.Expression = TypeGuards.is(SyntaxKind.SymbolKeyword);
    /**
     * Gets if the node is a SyntaxList.
     */
    static readonly isSyntaxList: (node: compiler.Node) => node is compiler.SyntaxList = TypeGuards.is(SyntaxKind.SyntaxList);
    /**
     * Gets if the node is a TaggedTemplateExpression.
     */
    static readonly isTaggedTemplateExpression: (node: compiler.Node) => node is compiler.TaggedTemplateExpression = TypeGuards.is(SyntaxKind.TaggedTemplateExpression);
    /**
     * Gets if the node is a TemplateExpression.
     */
    static readonly isTemplateExpression: (node: compiler.Node) => node is compiler.TemplateExpression = TypeGuards.is(SyntaxKind.TemplateExpression);
    /**
     * Gets if the node is a TemplateHead.
     */
    static readonly isTemplateHead: (node: compiler.Node) => node is compiler.TemplateHead = TypeGuards.is(SyntaxKind.TemplateHead);
    /**
     * Gets if the node is a TemplateMiddle.
     */
    static readonly isTemplateMiddle: (node: compiler.Node) => node is compiler.TemplateMiddle = TypeGuards.is(SyntaxKind.TemplateMiddle);
github dsherret / ts-morph / packages / ts-morph / src / compiler / ast / base / TypeParameteredNode.ts View on Github external
const writer = this._getWriterWithQueuedChildIndentation();
            const structurePrinter = this._context.structurePrinterFactory.forTypeParameterDeclaration();
            index = verifyAndGetIndex(index, typeParameters.length);

            structurePrinter.printTexts(writer, structures);

            if (typeParameters.length === 0) {
                insertIntoParentTextRange({
                    insertPos: getInsertPos(this),
                    parent: this,
                    newText: `<${writer.toString()}>`
                });
            }
            else {
                insertIntoCommaSeparatedNodes({
                    parent: this.getFirstChildByKindOrThrow(SyntaxKind.LessThanToken).getNextSiblingIfKindOrThrow(SyntaxKind.SyntaxList),
                    currentNodes: typeParameters,
                    insertIndex: index,
                    newText: writer.toString(),
                    useTrailingCommas: false
                });
            }

            return getNodesToReturn(typeParameters, this.getTypeParameters(), index, false);
        }
github dsherret / ts-morph / packages / ts-morph / src / factories / kindToWrapperMappings.ts View on Github external
[SyntaxKind.PartiallyEmittedExpression]: compiler.PartiallyEmittedExpression,
    [SyntaxKind.PostfixUnaryExpression]: compiler.PostfixUnaryExpression,
    [SyntaxKind.PrefixUnaryExpression]: compiler.PrefixUnaryExpression,
    [SyntaxKind.PropertyAccessExpression]: compiler.PropertyAccessExpression,
    [SyntaxKind.PropertyAssignment]: compiler.PropertyAssignment,
    [SyntaxKind.PropertyDeclaration]: compiler.PropertyDeclaration,
    [SyntaxKind.PropertySignature]: compiler.PropertySignature,
    [SyntaxKind.RegularExpressionLiteral]: compiler.RegularExpressionLiteral,
    [SyntaxKind.ReturnStatement]: compiler.ReturnStatement,
    [SyntaxKind.SetAccessor]: compiler.SetAccessorDeclaration,
    [SyntaxKind.ShorthandPropertyAssignment]: compiler.ShorthandPropertyAssignment,
    [SyntaxKind.SpreadAssignment]: compiler.SpreadAssignment,
    [SyntaxKind.SpreadElement]: compiler.SpreadElement,
    [SyntaxKind.StringLiteral]: compiler.StringLiteral,
    [SyntaxKind.SwitchStatement]: compiler.SwitchStatement,
    [SyntaxKind.SyntaxList]: compiler.SyntaxList,
    [SyntaxKind.TaggedTemplateExpression]: compiler.TaggedTemplateExpression,
    [SyntaxKind.TemplateExpression]: compiler.TemplateExpression,
    [SyntaxKind.TemplateHead]: compiler.TemplateHead,
    [SyntaxKind.TemplateMiddle]: compiler.TemplateMiddle,
    [SyntaxKind.TemplateSpan]: compiler.TemplateSpan,
    [SyntaxKind.TemplateTail]: compiler.TemplateTail,
    [SyntaxKind.ThisType]: compiler.ThisTypeNode,
    [SyntaxKind.ThrowStatement]: compiler.ThrowStatement,
    [SyntaxKind.TryStatement]: compiler.TryStatement,
    [SyntaxKind.TupleType]: compiler.TupleTypeNode,
    [SyntaxKind.TypeAliasDeclaration]: compiler.TypeAliasDeclaration,
    [SyntaxKind.TypeAssertionExpression]: compiler.TypeAssertion,
    [SyntaxKind.TypeLiteral]: compiler.TypeLiteralNode,
    [SyntaxKind.TypeParameter]: compiler.TypeParameterDeclaration,
    [SyntaxKind.TypePredicate]: compiler.TypePredicateNode,
    [SyntaxKind.TypeReference]: compiler.TypeReferenceNode,
github dsherret / ts-morph / packages / ts-morph / src / compiler / ast / base / ParameteredNode.ts View on Github external
insertParameters(index: number, structures: ReadonlyArray>) {
            if (ArrayUtils.isNullOrEmpty(structures))
                return [];

            const parameters = this.getParameters();
            const syntaxList = this.getFirstChildByKindOrThrow(SyntaxKind.OpenParenToken).getNextSiblingIfKindOrThrow(SyntaxKind.SyntaxList);
            index = verifyAndGetIndex(index, parameters.length);

            const writer = this._getWriterWithQueuedChildIndentation();
            const structurePrinter = this._context.structurePrinterFactory.forParameterDeclaration();

            structurePrinter.printTexts(writer, structures);

            insertIntoCommaSeparatedNodes({
                parent: syntaxList,
                currentNodes: parameters,
                insertIndex: index,
                newText: writer.toString(),
                useTrailingCommas: false
            });

            return getNodesToReturn(parameters, this.getParameters(), index, false);
github dsherret / ts-morph / packages / ts-morph / src / compiler / ast / utils / CommentNodeParser.ts View on Github external
function isSyntaxList(node: ts.Node): node is ts.SyntaxList {
    return node.kind === SyntaxKind.SyntaxList;
}