當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。