How to use the ts-morph.VariableDeclarationKind 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 roblox-ts / roblox-ts / src / compiler / identifier.ts View on Github external
}

	const definitions = isDefinition ? [node] : node.getDefinitions().map(def => def.getNode());

	for (const definition of definitions) {
		// I have no idea why, but getDefinitionNodes() cannot replace this
		if (definition.getSourceFile() === node.getSourceFile()) {
			let parent = definition;

			do {
				if (ts.TypeGuards.isVariableStatement(parent)) {
					if (parent.hasExportKeyword()) {
						const declarationKind = parent.getDeclarationKind();
						if (declarationKind === ts.VariableDeclarationKind.Let) {
							return state.getExportContextName(parent) + "." + name;
						} else if (declarationKind === ts.VariableDeclarationKind.Const) {
							const idContext = node.getFirstAncestorByKind(ts.SyntaxKind.ModuleDeclaration);
							const defContext = parent.getFirstAncestorByKind(ts.SyntaxKind.ModuleDeclaration);

							if (idContext && defContext && idContext !== defContext) {
								state.pushHoistStack(`local ${name} = ${state.getNameForContext(defContext)}.${name}`);
							}
						}
					}
					break;
				} else if (ts.TypeGuards.isNamespaceDeclaration(parent)) {
					// If within a namespace, scope it. If it is a namespace, don't
					if (parent !== definition.getParent()) {
						const parentName = state.namespaceStack.get(parent.getName());
						if (parentName) {
							return parentName + "." + name;
						}
github roblox-ts / roblox-ts / src / compiler / identifier.ts View on Github external
if (name === PKG_VERSION_ID) {
		return luaStringify(state.pkgVersion);
	}

	const definitions = isDefinition ? [node] : node.getDefinitions().map(def => def.getNode());

	for (const definition of definitions) {
		// I have no idea why, but getDefinitionNodes() cannot replace this
		if (definition.getSourceFile() === node.getSourceFile()) {
			let parent = definition;

			do {
				if (ts.TypeGuards.isVariableStatement(parent)) {
					if (parent.hasExportKeyword()) {
						const declarationKind = parent.getDeclarationKind();
						if (declarationKind === ts.VariableDeclarationKind.Let) {
							return state.getExportContextName(parent) + "." + name;
						} else if (declarationKind === ts.VariableDeclarationKind.Const) {
							const idContext = node.getFirstAncestorByKind(ts.SyntaxKind.ModuleDeclaration);
							const defContext = parent.getFirstAncestorByKind(ts.SyntaxKind.ModuleDeclaration);

							if (idContext && defContext && idContext !== defContext) {
								state.pushHoistStack(`local ${name} = ${state.getNameForContext(defContext)}.${name}`);
							}
						}
					}
					break;
				} else if (ts.TypeGuards.isNamespaceDeclaration(parent)) {
					// If within a namespace, scope it. If it is a namespace, don't
					if (parent !== definition.getParent()) {
						const parentName = state.namespaceStack.get(parent.getName());
						if (parentName) {
github roblox-ts / roblox-ts / src / compiler / indexed.ts View on Github external
export function isIdentifierDefinedInExportLet(exp: ts.Identifier) {
	// I have no idea why, but getDefinitionNodes() cannot replace this
	for (const def of exp.getDefinitions()) {
		const definition = def.getNode().getFirstAncestorByKind(ts.SyntaxKind.VariableStatement);
		if (
			definition &&
			definition.hasExportKeyword() &&
			definition.getDeclarationKind() === ts.VariableDeclarationKind.Let
		) {
			return true;
		}
	}
	return false;
}
github roblox-ts / roblox-ts / src / compiler / for.ts View on Github external
condValue.getDefinitions().every(a => {
				const declNode = a.getDeclarationNode();
				if (declNode && ts.TypeGuards.isVariableDeclaration(declNode)) {
					const declParent = declNode.getParent();
					if (ts.TypeGuards.isVariableDeclarationList(declParent)) {
						return declParent.getDeclarationKind() === ts.VariableDeclarationKind.Const;
					}
				}
				return false;
			}))
	);
github roblox-ts / roblox-ts / src / compiler / variable.ts View on Github external
.every(v => ts.TypeGuards.isIdentifier(v.getChildAtIndex(0)));
		if (isFlatBinding && rhs && ts.TypeGuards.isCallExpression(rhs) && isTupleType(rhs.getType())) {
			const names = new Array();
			const values = new Array();
			for (const element of lhs.getElements()) {
				if (ts.TypeGuards.isBindingElement(element)) {
					const nameNode = element.getNameNode();
					if (ts.TypeGuards.isIdentifier(nameNode)) {
						names.push(compileExpression(state, nameNode));
					}
				} else if (ts.TypeGuards.isOmittedExpression(element)) {
					names.push("_");
				}
			}
			values.push(compileCallExpression(state, rhs, true));
			if (isExported && decKind === ts.VariableDeclarationKind.Let) {
				let returnValue: string | undefined;
				concatNamesAndValues(state, names, values, false, str => (returnValue = str));
				return state.exitPrecedingStatementContextAndJoin() + returnValue || "";
			} else {
				if (isExported && ts.TypeGuards.isVariableStatement(grandParent)) {
					names.forEach(name => state.pushExport(name, grandParent));
				}
				let returnValue: string | undefined;
				concatNamesAndValues(state, names, values, true, str => (returnValue = str));
				return state.exitPrecedingStatementContextAndJoin() + returnValue || "";
			}
		}
	}

	let result = "";
	if (ts.TypeGuards.isIdentifier(lhs)) {
github roblox-ts / roblox-ts / src / compiler / variable.ts View on Github external
export function compileVariableDeclaration(state: CompilerState, node: ts.VariableDeclaration) {
	state.enterPrecedingStatementContext();
	const lhs = node.getNameNode();
	const rhs = getNonNullExpressionDownwards(node.getInitializer());

	const parent = node.getParent();
	const grandParent = parent.getParent();
	const isExported = ts.TypeGuards.isVariableStatement(grandParent) && grandParent.isExported();

	let decKind = ts.VariableDeclarationKind.Const;
	if (ts.TypeGuards.isVariableDeclarationList(parent)) {
		decKind = parent.getDeclarationKind();
	}

	if (ts.TypeGuards.isArrayBindingPattern(lhs)) {
		const isFlatBinding = lhs
			.getElements()
			.filter(v => ts.TypeGuards.isBindingElement(v))
			.every(v => ts.TypeGuards.isIdentifier(v.getChildAtIndex(0)));
		if (isFlatBinding && rhs && ts.TypeGuards.isCallExpression(rhs) && isTupleType(rhs.getType())) {
			const names = new Array();
			const values = new Array();
			for (const element of lhs.getElements()) {
				if (ts.TypeGuards.isBindingElement(element)) {
					const nameNode = element.getNameNode();
					if (ts.TypeGuards.isIdentifier(nameNode)) {
github roblox-ts / roblox-ts / src / compiler / variable.ts View on Github external
export function compileVariableDeclarationList(state: CompilerState, node: ts.VariableDeclarationList) {
	const declarationKind = node.getDeclarationKind();
	if (declarationKind === ts.VariableDeclarationKind.Var) {
		throw new CompilerError(
			"'var' keyword is not supported! Use 'let' or 'const' instead.",
			node,
			CompilerErrorType.NoVarKeyword,
		);
	}

	return node
		.getDeclarations()
		.reduce((result, declaration) => result + compileVariableDeclaration(state, declaration), "");
}
github roblox-ts / roblox-ts / src / compiler / variable.ts View on Github external
!isIterableIterator(rhsType, rhs) &&
				!isIterableFunction(rhsType) &&
				(isObjectType(rhsType) || ts.TypeGuards.isThisExpression(rhs))
			) {
				state.usesTSLibrary = true;
				rhsStr = removeBalancedParenthesisFromStringBorders(rhsStr);
				const id = state.getNewId();
				preStatements.push(`local ${id} = ${rhsStr}[TS.Symbol_iterator](${rhsStr});`);
				rhsStr = id;
			}
		}

		getBindingData(state, names, values, preStatements, postStatements, lhs, rhsStr);
		preStatements.forEach(statementStr => (result += state.indent + statementStr + "\n"));
		if (values.length > 0) {
			if (isExported && decKind === ts.VariableDeclarationKind.Let) {
				concatNamesAndValues(state, names, values, false, str => (result += str));
			} else {
				if (isExported && ts.TypeGuards.isVariableStatement(grandParent)) {
					names.forEach(name => state.pushExport(name, grandParent));
				}
				concatNamesAndValues(state, names, values, true, str => (result += str));
			}
		}
		postStatements.forEach(statementStr => (result += state.indent + statementStr + "\n"));
	}

	return state.exitPrecedingStatementContextAndJoin() + result;
}
github roblox-ts / roblox-ts / src / compiler / loop.ts View on Github external
condValue.getDefinitions().every(a => {
				const declNode = a.getDeclarationNode();
				if (declNode && ts.TypeGuards.isVariableDeclaration(declNode)) {
					const declParent = declNode.getParent();
					if (ts.TypeGuards.isVariableDeclarationList(declParent)) {
						return declParent.getDeclarationKind() === ts.VariableDeclarationKind.Const;
					}
				}
				return false;
			}))
	);