How to use is-reference - 8 common examples

To help you get started, we’ve selected a few is-reference 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 sveltejs / svelte / src / compiler / compile / nodes / shared / Expression.ts View on Github external
enter(node: any, parent: any, key: string) {
				// don't manipulate shorthand props twice
				if (key === 'value' && parent.shorthand) return;

				if (map.has(node)) {
					scope = map.get(node);
				}

				if (!function_expression && /FunctionExpression/.test(node.type)) {
					function_expression = node;
				}

				if (is_reference(node, parent)) {
					const { name, nodes } = flatten_reference(node);

					if (scope.has(name)) return;

					if (name[0] === '$' && template_scope.names.has(name.slice(1))) {
						component.error(node, {
							code: `contextual-store`,
							message: `Stores must be declared at the top level of the component (this may change in a future version of Svelte)`
						});
					}

					if (template_scope.is_let(name)) {
						if (!function_expression) { // TODO should this be `!lazy` ?
							contextual_dependencies.add(name);
							dependencies.add(name);
						}
github rollup / rollup / src / ast / nodes / shared / callHasEffects.js View on Github external
export default function callHasEffects ( scope, callee, isNew ) {
	const values = new Set( [ callee ] );

	for ( const node of values ) {
		if ( node.type === 'UNKNOWN' ) return true; // err on side of caution

		if ( /Function/.test( node.type ) ) {
			if ( fnHasEffects( node, isNew && isES5Function( node ) ) ) return true;
		}

		else if ( /Class/.test( node.type ) ) {
			// TODO find constructor (may belong to a superclass)
			return true;
		}

		else if ( isReference( node ) ) {
			const flattened = flatten( node );
			const variable = scope.findVariable( flattened.name );

			if ( variable.isGlobal ) {
				if ( !pureFunctions[ flattened.keypath ] ) return true;
			}

			else if ( variable.isExternal ) {
				return true; // TODO make this configurable? e.g. `path.[whatever]`
			}

			else {
				if ( node.variable ) {
					node.variable.gatherPossibleValues( values );
				} else {
					return true;
github sveltejs / svelte / src / compiler / compile / nodes / shared / Expression.ts View on Github external
enter(node: any, parent: any) {
				if (node.type === 'Property' && node.shorthand) {
					node.value = JSON.parse(JSON.stringify(node.value));
					node.shorthand = false;
				}

				if (map.has(node)) {
					scope = map.get(node);
				}

				if (is_reference(node, parent)) {
					const { name } = flatten_reference(node);

					if (scope.has(name)) return;

					if (function_expression) {
						if (template_scope.names.has(name)) {
							contextual_dependencies.add(name);

							template_scope.dependencies_for_name.get(name).forEach(dependency => {
								dependencies.add(dependency);
							});
						} else {
							dependencies.add(name);
							component.add_reference(name); // TODO is this redundant/misplaced?
						}
					} else if (is_contextual(component, template_scope, name)) {
github sveltejs / svelte / src / compiler / compile / utils / is_used_as_reference.ts View on Github external
export default function is_used_as_reference(
	node: Node,
	parent: Node
): boolean {
	if (!is_reference(node, parent)) {
		return false;
	}
	if (!parent) {
		return true;
	}

	/* eslint-disable no-fallthrough */
	switch (parent.type) {
		// disregard the `foo` in `const foo = bar`
		case 'VariableDeclarator':
			return node !== parent.id;
		// disregard the `foo`, `bar` in `function foo(bar){}`
		case 'FunctionDeclaration':
		// disregard the `foo` in `import { foo } from 'foo'`
		case 'ImportSpecifier':
		// disregard the `foo` in `import foo from 'foo'`
github sveltejs / svelte / src / utils / annotateWithScopes.ts View on Github external
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);
				}
			}
		},
github rollup / rollup / src / ast / nodes / Identifier.ts View on Github external
bind() {
		if (this.bound) return;
		this.bound = true;
		if (this.variable === null && isReference(this, this.parent)) {
			this.variable = this.scope.findVariable(this.name);
			this.variable.addReference(this);
		}
		if (
			this.variable !== null &&
			this.variable instanceof LocalVariable &&
			this.variable.additionalInitializers !== null
		) {
			this.variable.consolidateInitializers();
		}
	}
github sveltejs / svelte / src / validate / js / utils / usesThisOrArguments.ts View on Github external
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;
			}
		},
	});
github sveltejs / svelte / src / compiler / compile / Component.ts View on Github external
warn_on_undefined_store_value_references(node, parent, scope) {
		if (
			node.type === 'LabeledStatement' &&
			node.label.name === '$' &&
			parent.type !== 'Program'
		) {
			this.warn(node as any, {
				code: 'non-top-level-reactive-declaration',
				message: '$: has no effect outside of the top-level',
			});
		}

		if (is_reference(node as Node, parent as Node)) {
			const object = get_object(node);
			const { name } = object;

			if (name[0] === '$' && !scope.has(name)) {
				this.warn_if_undefined(name, object, null);
			}
		}
	}

is-reference

Determine whether an AST node is a reference

MIT
Latest version published 10 days ago

Package Health Score

80 / 100
Full package analysis

Popular is-reference functions