Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'is-reference' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
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);
}
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;
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)) {
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'`
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);
}
}
},
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();
}
}
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;
}
},
});
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);
}
}
}