How to use the ts-morph.SyntaxKind function in ts-morph

To help you get started, we’ve selected a few ts-morph 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 terotests / ts2redux / dist / src / index.js View on Github external
m.getBody().forEachDescendant(function (node, traversal) {
                                            switch (node.getKind()) {
                                                case ts_morph_1.SyntaxKind.PropertyAccessExpression:
                                                    // could be this.
                                                    if (node.getFirstChild().getKind() === ts_morph_1.SyntaxKind.ThisKeyword) {
                                                        inputSet_1[node.getChildAtIndex(2).print()] = node;
                                                    }
                                                    break;
                                            }
                                        });
                                        var properties_1 = {};
github roblox-ts / roblox-ts / src / utility / type.ts View on Github external
			.some(ancestor => ancestor.getKind() === ts.SyntaxKind.TypeQuery || isTypeStatement(ancestor)) ||
		// if it is a const enum, it is always a type, even if it isn't a type >:)
github roblox-ts / roblox-ts / src / compiler / binary.ts View on Github external
export function compileBinaryExpression(state: CompilerState, node: ts.BinaryExpression) {
	// @ts-ignore;
	const x = node.getText();

	const nodeParent = getNonNullUnParenthesizedExpressionUpwards(node.getParentOrThrow());
	const parentKind = nodeParent.getKind();
	const isStatement = parentKind === ts.SyntaxKind.ExpressionStatement || parentKind === ts.SyntaxKind.ForStatement;

	const opToken = node.getOperatorToken();
	const opKind = opToken.getKind();
	const isEqualsOperation = opKind === ts.SyntaxKind.EqualsToken;

	const lhs = node.getLeft();
	const rhs = getNonNullExpressionDownwards(node.getRight());
	let lhsStr: string;
	let rhsStr: string;

	if (!isEqualsOperation) {
		checkNonAny(lhs);
		checkNonAny(rhs);
	}

	// binding patterns
github roblox-ts / roblox-ts / src / compiler / binary.ts View on Github external
export function isSetToken(opKind: ts.ts.SyntaxKind) {
	return (
		opKind === ts.SyntaxKind.EqualsToken ||
		opKind === ts.SyntaxKind.BarEqualsToken ||
		opKind === ts.SyntaxKind.AmpersandEqualsToken ||
		opKind === ts.SyntaxKind.CaretEqualsToken ||
		opKind === ts.SyntaxKind.LessThanLessThanEqualsToken ||
		opKind === ts.SyntaxKind.GreaterThanGreaterThanEqualsToken ||
		opKind === ts.SyntaxKind.GreaterThanGreaterThanGreaterThanEqualsToken ||
		opKind === ts.SyntaxKind.PlusEqualsToken ||
		opKind === ts.SyntaxKind.MinusEqualsToken ||
		opKind === ts.SyntaxKind.AsteriskEqualsToken ||
		opKind === ts.SyntaxKind.SlashEqualsToken ||
		opKind === ts.SyntaxKind.AsteriskAsteriskEqualsToken ||
		opKind === ts.SyntaxKind.PercentEqualsToken
	);
}
github roblox-ts / roblox-ts / src / compiler / new.ts View on Github external
const typeArgument = node.getType().getTypeArguments()[0];

	if (typeArgument.isNullable() || typeArgument.isUndefined()) {
		throw new CompilerError(
			`Cannot create a ${type} with a nullable index!`,
			node,
			CompilerErrorType.NullableIndexOnMapOrSet,
		);
	}

	const firstParam = args[0];

	if (
		firstParam &&
		(!ts.TypeGuards.isArrayLiteralExpression(firstParam) ||
			firstParam.getChildrenOfKind(ts.SyntaxKind.SpreadElement).length > 0)
	) {
		state.usesTSLibrary = true;
		return `TS.${type}_new(${compileCallArgumentsAndJoin(state, args)})`;
	} else {
		let id = "";
		const lines = new Array();
		let hasContext = false;

		const compileElement = compileMapSetElement.get(type)!;

		let exp: ts.Node = node;
		let parent = getNonNullUnParenthesizedExpressionUpwards(node.getParent());
		const addMethodName = addKeyMethodNames.get(type)!;

		while (ts.TypeGuards.isPropertyAccessExpression(parent) && addMethodName === parent.getName()) {
			const grandparent = getNonNullUnParenthesizedExpressionUpwards(parent.getParent());
github roblox-ts / roblox-ts / src / compiler / unary.ts View on Github external
function getIncrementString(opKind: ts.ts.PrefixUnaryOperator, expStr: string, node: ts.Node, varName: string) {
	const op =
		opKind === ts.SyntaxKind.PlusPlusToken
			? " + "
			: opKind === ts.SyntaxKind.MinusMinusToken
			? " - "
			: (() => {
					throw new CompilerError(
						`Bad unary expression! (${opKind})`,
						node,
						CompilerErrorType.BadPrefixUnaryExpression,
					);
			  })();

	return `${varName ? `${varName} = ` : ""}${expStr}${op}1`;
}
github roblox-ts / roblox-ts / src / compiler / module.ts View on Github external
if (node.hasModuleSpecifier()) {
		const moduleFile = node.getModuleSpecifierSourceFile();
		if (!moduleFile) {
			const specifier = node.getModuleSpecifier();
			const text = specifier ? specifier.getText() : "unknown";
			throw new CompilerError(
				`Could not find file for '${text}'. Did you forget to "npm install"?`,
				node,
				CompilerErrorType.MissingModuleFile,
			);
		}
		luaPath = getImportPath(state, node.getSourceFile(), moduleFile, node);
	}

	const ancestor =
		node.getFirstAncestorByKind(ts.SyntaxKind.ModuleDeclaration) ||
		node.getFirstAncestorByKind(ts.SyntaxKind.SourceFile);

	if (!ancestor) {
		throw new CompilerError("Could not find export ancestor!", node, CompilerErrorType.BadAncestor, true);
	}

	const lhs = new Array();
	const rhs = new Array();

	if (node.isNamespaceExport()) {
		if (node.getSourceFile().isDeclarationFile()) {
			throw new CompilerError(
				"Namespace exports are not supported for .d.ts files!",
				node,
				CompilerErrorType.BadNamespaceExport,
			);
github roblox-ts / roblox-ts / src / compiler / new.ts View on Github external
if (ts.TypeGuards.isCallExpression(grandparent)) {
			exp = grandparent;
			parent = skipNodesUpwards(grandparent.getParent()!);
		} else {
			break;
		}
	}

	const pushCondition = ts.TypeGuards.isNewExpression(exp)
		? () => true
		: (declaration: DeclarationContext) => declaration.isIdentifier;

	if (
		firstParam &&
		(!ts.TypeGuards.isArrayLiteralExpression(firstParam) ||
			firstParam.getChildrenOfKind(ts.SyntaxKind.SpreadElement).length > 0)
	) {
		state.usesTSLibrary = true;
		const id = state.pushToDeclarationOrNewId(
			exp,
			preDeclaration + `TS.${type}_new(${compileCallArgumentsAndJoin(state, args)})` + postDeclaration,
			pushCondition,
		);
		return id;
	} else {
		let id = "";
		const lines = new Array();
		let hasContext = false;

		if (firstParam) {
			for (let element of firstParam.getElements()) {
				element = skipNodesDownwards(element);
github roblox-ts / roblox-ts / src / CompilerState.ts View on Github external
public getExportContextName(node: ts.VariableStatement | ts.Node): string {
		return this.getNameForContext(node.getFirstAncestorByKind(ts.SyntaxKind.ModuleDeclaration));
	}
github roblox-ts / roblox-ts / src / compiler / roact.ts View on Github external
export function generateRoactSymbolAttribute(
	state: CompilerState,
	roactSymbol: string,
	attributeNode: ts.JsxAttribute,
	attributesStack: Array,
) {
	const expr = attributeNode.getChildrenOfKind(ts.SyntaxKind.JsxExpression);

	for (const expression of expr) {
		const innerExpression = expression.getExpressionOrThrow();

		if (
			roactSymbol === "event" ||
			roactSymbol === "change" ||
			roactSymbol === "Event" ||
			roactSymbol === "Change"
		) {
			if (ts.TypeGuards.isObjectLiteralExpression(innerExpression)) {
				const properties = innerExpression.getProperties();
				getInlineObjectProperties(state, roactSymbol, properties, attributesStack);
			} else if (ts.TypeGuards.isIdentifier(innerExpression)) {
				getIdentifierObjectProperties(state, roactSymbol, innerExpression, attributesStack);
			} else {