当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript estree-walker.walk函数代码示例

本文整理汇总了TypeScript中estree-walker.walk函数的典型用法代码示例。如果您正苦于以下问题:TypeScript walk函数的具体用法?TypeScript walk怎么用?TypeScript walk使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了walk函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: add_indentation

export function add_indentation(code: MagicString, node: Node, levels = 1) {
	const base_indent = code.getIndentString();
	const indent = repeat(base_indent, levels);
	const pattern = /\n/gm;

	const excluded = [];

	walk(node, {
		enter(node) {
			if (node.type === 'TemplateElement') {
				excluded.push(node);
			}
		}
	});

	const str = code.original.slice(node.start, node.end);

	let match;
	while (match = pattern.exec(str)) {
		const index = node.start + match.index;
		while (excluded[0] && excluded[0].end < index) excluded.shift();
		if (excluded[0] && excluded[0].start < index) continue;

		code.appendLeft(index + 1, indent);
	}
}
开发者ID:StevenWeathers,项目名称:svelte,代码行数:26,代码来源:indentation.ts

示例2: usesThisOrArguments

export default function usesThisOrArguments(node: Node) {
	let result = false;

	walk(node, {
		enter(node: Node, parent: Node) {
			if (
				result ||
				node.type === 'FunctionExpression' ||
				node.type === 'FunctionDeclaration'
			) {
				return this.skip();
			}

			if (node.type === 'ThisExpression') {
				result = true;
			}

			if (
				node.type === 'Identifier' &&
				isReference(node, parent) &&
				node.name === 'arguments'
			) {
				result = true;
			}
		},
	});

	return result;
}
开发者ID:kristoferbaxter,项目名称:svelte,代码行数:29,代码来源:usesThisOrArguments.ts

示例3: remove_indentation

export function remove_indentation(code: MagicString, node: Node) {
	const indent = code.getIndentString();
	const pattern = new RegExp(`^${indent}`, 'gm');

	const excluded = [];

	walk(node, {
		enter(node) {
			if (node.type === 'TemplateElement') {
				excluded.push(node);
			}
		}
	});

	const str = code.original.slice(node.start, node.end);

	let match;
	while (match = pattern.exec(str)) {
		const index = node.start + match.index;
		while (excluded[0] && excluded[0].end < index) excluded.shift();
		if (excluded[0] && excluded[0].start < index) continue;

		code.remove(index, index + indent.length);
	}
}
开发者ID:StevenWeathers,项目名称:svelte,代码行数:25,代码来源:indentation.ts

示例4: addSourcemapLocations

	addSourcemapLocations(node: Node) {
		walk(node, {
			enter: (node: Node) => {
				this.code.addSourcemapLocation(node.start);
				this.code.addSourcemapLocation(node.end);
			},
		});
	}
开发者ID:kristoferbaxter,项目名称:svelte,代码行数:8,代码来源:Generator.ts

示例5: getIndentExclusionRanges

function getIndentExclusionRanges(node: Node) {
	const ranges: Node[] = [];
	walk(node, {
		enter(node: Node) {
			if (node.type === 'TemplateElement') ranges.push(node);
		}
	});
	return ranges;
}
开发者ID:kristoferbaxter,项目名称:svelte,代码行数:9,代码来源:Generator.ts

示例6: annotateWithScopes

		const contextualise = (
			node: Node, contextDependencies: Map<string, string[]>,
			indexes: Set<string>,
			isEventHandler: boolean
		) => {
			this.addSourcemapLocations(node); // TODO this involves an additional walk — can we roll it in somewhere else?
			let { scope } = annotateWithScopes(node);

			const dependencies: Set<string> = new Set();

			walk(node, {
				enter(node: Node, parent: Node) {
					code.addSourcemapLocation(node.start);
					code.addSourcemapLocation(node.end);

					if (node._scope) {
						scope = node._scope;
						return;
					}

					if (isReference(node, parent)) {
						const { name } = flattenReference(node);
						if (scope && scope.has(name) || helpers.has(name) || (name === 'event' && isEventHandler)) return;

						if (contextDependencies.has(name)) {
							contextDependencies.get(name).forEach(dependency => {
								dependencies.add(dependency);
							});
						} else if (!indexes.has(name)) {
							dependencies.add(name);
						}

						this.skip();
					}
				},

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

			dependencies.forEach(dependency => {
				expectedProperties.add(dependency);
			});

			return {
				snippet: `[✂${node.start}-${node.end}✂]`,
				dependencies: Array.from(dependencies)
			};
		}
开发者ID:kristoferbaxter,项目名称:svelte,代码行数:50,代码来源:Generator.ts

示例7: 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

示例8: walkThroughTopFunctionScope

export default function walkThroughTopFunctionScope(body: Node, callback: Function) {
  let lexicalDepth = 0;
  walk(body, {
    enter(node: Node) {
      if (/^Function/.test(node.type)) {
        lexicalDepth += 1;
      } else if (lexicalDepth === 0) {
        callback(node)
      }
    },

    leave(node: Node) {
      if (/^Function/.test(node.type)) {
        lexicalDepth -= 1;
      }
    },
  });
}
开发者ID:kristoferbaxter,项目名称:svelte,代码行数:18,代码来源:walkThroughTopFunctionScope.ts

示例9: annotateWithScopes

export default function annotateWithScopes(expression: Node) {
	const globals = new Set();
	let scope = new Scope(null, false);

	walk(expression, {
		enter(node: Node, parent: Node) {
			if (/Function/.test(node.type)) {
				if (node.type === 'FunctionDeclaration') {
					scope.declarations.add(node.id.name);
				} else {
					node._scope = scope = new Scope(scope, false);
					if (node.id) scope.declarations.add(node.id.name);
				}

				node.params.forEach((param: Node) => {
					extractNames(param).forEach(name => {
						scope.declarations.add(name);
					});
				});
			} else if (/For(?:In|Of)Statement/.test(node.type)) {
				node._scope = scope = new Scope(scope, true);
			} else if (node.type === 'BlockStatement') {
				node._scope = scope = new Scope(scope, true);
			} else if (/(Function|Class|Variable)Declaration/.test(node.type)) {
				scope.addDeclaration(node);
			} else if (isReference(node, parent)) {
				if (!scope.has(node.name)) {
					globals.add(node.name);
				}
			}
		},

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

	return { scope, globals };
}
开发者ID:kristoferbaxter,项目名称:svelte,代码行数:41,代码来源:annotateWithScopes.ts

示例10: attachScopes

export default function attachScopes(ast: Node, propertyName: string = 'scope'): Scope {
	let scope = new Scope();

	walk(ast, {
		enter(node, parent) {
			// function foo () {...}
			// class Foo {...}
			if (/(Function|Class)Declaration/.test(node.type)) {
				scope.addDeclaration(node, false, false);
			}

			// var foo = 1
			if (node.type === 'VariableDeclaration') {
				const kind: keyof typeof blockDeclarations = node.kind;
				const isBlockDeclaration = blockDeclarations[kind];

				node.declarations.forEach((declaration: Node) => {
					scope.addDeclaration(declaration, isBlockDeclaration, true);
				});
			}

			let newScope: Scope | undefined;

			// create new function scope
			if (/Function/.test(node.type)) {
				newScope = new Scope({
					parent: scope,
					block: false,
					params: node.params
				});

				// named function expressions - the name is considered
				// part of the function's scope
				if (node.type === 'FunctionExpression' && node.id) {
					newScope.addDeclaration(node, false, false);
				}
			}

			// create new block scope
			if (node.type === 'BlockStatement' && !/Function/.test(parent!.type)) {
				newScope = new Scope({
					parent: scope,
					block: true
				});
			}

			// catch clause has its own block scope
			if (node.type === 'CatchClause') {
				newScope = new Scope({
					parent: scope,
					params: [node.param],
					block: true
				});
			}

			if (newScope) {
				Object.defineProperty(node, propertyName, {
					value: newScope,
					configurable: true
				});

				scope = newScope;
			}
		},
		leave(node) {
			if (node[propertyName]) scope = scope.parent!;
		}
	});

	return scope;
}
开发者ID:NikkiKoole,项目名称:nikkikoole.github.io,代码行数:71,代码来源:attachScopes.ts


注:本文中的estree-walker.walk函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。