當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript acorn.parseExpressionAt函數代碼示例

本文整理匯總了TypeScript中acorn.parseExpressionAt函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript parseExpressionAt函數的具體用法?TypeScript parseExpressionAt怎麽用?TypeScript parseExpressionAt使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了parseExpressionAt函數的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: MagicString

		usedHelpers.forEach(key => {
			const str = shared[key];
			const code = new MagicString(str);
			const expression = parseExpressionAt(str, 0);

			let { scope } = annotateWithScopes(expression);

			walk(expression, {
				enter(node: Node, parent: Node) {
					if (node._scope) scope = node._scope;

					if (
						node.type === 'Identifier' &&
						isReference(node, parent) &&
						!scope.has(node.name)
					) {
						if (node.name in shared) {
							// this helper function depends on another one
							const dependency = node.name;
							usedHelpers.add(dependency);

							const alias = generator.alias(dependency);
							if (alias !== node.name)
								code.overwrite(node.start, node.end, alias);
						}
					}
				},

				leave(node: Node) {
					if (node._scope) scope = scope.parent;
				},
			});

			if (key === 'transitionManager') {
				// special case
				const global = `_svelteTransitionManager`;

				inlineHelpers += `\n\nvar ${generator.alias('transitionManager')} = window.${global} || (window.${global} = ${code});\n\n`;
			} else {
				const alias = generator.alias(expression.id.name);
				if (alias !== expression.id.name)
					code.overwrite(expression.id.start, expression.id.end, alias);

				inlineHelpers += `\n\n${code}`;
			}
		});
開發者ID:kristoferbaxter,項目名稱:svelte,代碼行數:46,代碼來源:index.ts

示例2: readExpression

function readExpression(parser: Parser, start: number, quoteMark: string|null) {
	let str = '';
	let escaped = false;

	for (let i = start; i < parser.template.length; i += 1) {
		const char = parser.template[i];

		if (quoteMark) {
			if (char === quoteMark) {
				if (escaped) {
					str += quoteMark;
				} else {
					break;
				}
			} else if (escaped) {
				str += '\\' + char;
				escaped = false;
			} else if (char === '\\') {
				escaped = true;
			} else {
				str += char;
			}
		} else if (/\s/.test(char)) {
			break;
		} else {
			str += char;
		}
	}

	const expression = parseExpressionAt(repeat(' ', start) + str, start, {
		ecmaVersion: 9,
	});
	parser.index = expression.end;

	parser.allowWhitespace();
	if (quoteMark) parser.eat(quoteMark, true);

	return expression;
}
開發者ID:kristoferbaxter,項目名稱:svelte,代碼行數:39,代碼來源:directives.ts

示例3: readExpression

export default function readExpression(parser: Parser) {
	const start = parser.index;

	const name = parser.readUntil(/\s*}}/);
	if (name && /^[a-z]+$/.test(name)) {
		const end = start + name.length;

		if (literals.has(name)) {
			return {
				type: 'Literal',
				start,
				end,
				value: literals.get(name),
				raw: name,
			};
		}

		return {
			type: 'Identifier',
			start,
			end: start + name.length,
			name,
		};
	}

	parser.index = start;

	try {
		const node = parseExpressionAt(parser.template, parser.index, {
			ecmaVersion: 9,
			preserveParens: true,
		});
		parser.index = node.end;

		return node;
	} catch (err) {
		parser.acornError(err);
	}
}
開發者ID:kristoferbaxter,項目名稱:svelte,代碼行數:39,代碼來源:expression.ts

示例4: readBindingDirective

export function readBindingDirective(
	parser: Parser,
	start: number,
	name: string
) {
	let value;

	if (parser.eat('=')) {
		const quoteMark = parser.eat(`'`) ? `'` : parser.eat(`"`) ? `"` : null;

		const a = parser.index;

		if (parser.eat('{{')) {
			let message = 'bound values should not be wrapped';
			const b = parser.template.indexOf('}}', a);
			if (b !== -1) {
				const value = parser.template.slice(parser.index, b);
				message += ` — use '${value}', not '{{${value}}}'`;
			}

			parser.error(message, a);
		}

		// this is a bit of a hack so that we can give Acorn something parseable
		let b;
		if (quoteMark) {
			b = parser.index = parser.template.indexOf(quoteMark, parser.index);
		} else {
			parser.readUntil(/[\s\r\n\/>]/);
			b = parser.index;
		}

		const source = repeat(' ', a) + parser.template.slice(a, b);
		value = parseExpressionAt(source, a, { ecmaVersion: 9 });

		if (value.type !== 'Identifier' && value.type !== 'MemberExpression') {
			parser.error(`Cannot bind to rvalue`, value.start);
		}

		parser.allowWhitespace();

		if (quoteMark) {
			parser.eat(quoteMark, true);
		}
	} else {
		// shorthand – bind:foo equivalent to bind:foo='foo'
		value = {
			type: 'Identifier',
			start: start + 5,
			end: parser.index,
			name,
		};
	}

	return {
		start,
		end: parser.index,
		type: 'Binding',
		name,
		value,
	};
}
開發者ID:kristoferbaxter,項目名稱:svelte,代碼行數:62,代碼來源:directives.ts


注:本文中的acorn.parseExpressionAt函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。