当前位置: 首页>>代码示例>>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;未经允许,请勿转载。