Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

hooks.macro

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hooks.macro - npm Package Compare versions

Comparing version 0.1.0 to 0.1.1

2

package.json
{
"name": "hooks.macro",
"version": "0.1.0",
"version": "0.1.1",
"author": "Pier Paolo Ramon <ramonpierre@gmail.com>",

@@ -5,0 +5,0 @@ "main": "build/hooks.macro.js",

@@ -1,6 +0,27 @@

const { addNamed } = require('@babel/helper-module-imports');
const { createMacro, MacroError } = require('babel-plugin-macros');
const { addNamed } = require("@babel/helper-module-imports");
const { createMacro, MacroError } = require("babel-plugin-macros");
module.exports = createMacro(memoMacro);
function ensureParentScopeBinding(parentPath, path) {
const parentScope = parentPath.scope;
const name = path.node.name;
if (!parentPath.scope.hasOwnBinding(path.node.name)) {
return false;
}
let scope = path.scope;
while (scope && scope !== parentScope) {
if (scope.hasOwnBinding(name)) {
return false;
}
scope = scope.path.parent.scope;
}
return true;
}
function hookCreateTransform(parentPath, createPath, importedHookName, babel) {

@@ -12,13 +33,15 @@ const { types: t } = babel;

createPath.traverse({
Identifier(path) {
if (
// Excluding "b" in "a.b" form
(path.parentPath.type !== 'MemberExpression' ||
path.parentKey === 'object') &&
// Excluding bindings outside of the component
parentPath.scope.hasOwnBinding(path.node.name)
) {
references.push(path.node);
Expression(path) {
if (t.isIdentifier(path)) {
if (
// Excluding "b" in "a.b" form
(!t.isMemberExpression(path.parentPath) ||
path.parentKey === "object") &&
// Excluding bindings outside of the component
ensureParentScopeBinding(parentPath, path)
) {
references.push(path.node);
}
}
},
}
});

@@ -29,4 +52,4 @@

createPath.node,
t.arrayExpression(references),
]),
t.arrayExpression(references)
])
);

@@ -38,7 +61,7 @@ }

const importedHookName = addNamed(path, hookName, 'react');
const importedHookName = addNamed(path, hookName, "react");
const functionCallPath = path.parentPath;
const argument = functionCallPath.get('arguments.0');
const argument = functionCallPath.get("arguments.0");

@@ -61,4 +84,4 @@ let references = [];

const CONFIGS = [
['useAutoMemo', 'useMemo', true],
['useAutoCallback', 'useCallback', false],
["useAutoMemo", "useMemo", true],
["useAutoCallback", "useCallback", false]
];

@@ -79,3 +102,3 @@

throw new MacroError(
`useAutoMemo can only be used a function, and can not be passed around as an argument.`,
`useAutoMemo can only be used a function, and can not be passed around as an argument.`
);

@@ -82,0 +105,0 @@ }

@@ -7,7 +7,7 @@ const path = require('path');

plugin,
pluginName: 'Hooks macro',
pluginName: 'Hooks macro ›',
snapshot: true,
tests: withFilename([
{
name: 'Throws if not called as function',
title: 'Throws if not called as function',
error: true,

@@ -21,3 +21,3 @@ code: `

{
name: 'Works with null',
title: 'Works with null',
code: `

@@ -32,3 +32,3 @@ import { useAutoMemo } from './hooks.macro'

{
name: 'Works with null returning arrow',
title: 'Works with null returning arrow',
code: `

@@ -43,3 +43,3 @@ import { useAutoMemo } from './hooks.macro'

{
name: 'Works with external value',
title: 'Works with external value',
code: `

@@ -55,3 +55,3 @@ import { useAutoMemo } from './hooks.macro'

{
name: 'Works with external value returning arrow',
title: 'Works with external value returning arrow',
code: `

@@ -67,3 +67,3 @@ import { useAutoMemo } from './hooks.macro'

{
name: 'Works with external obj',
title: 'Works with external obj',
code: `

@@ -79,3 +79,3 @@ import { useAutoMemo } from './hooks.macro'

{
name: 'Works with external obj returning arrow',
title: 'Works with external obj returning arrow',
code: `

@@ -91,3 +91,3 @@ import { useAutoMemo } from './hooks.macro'

{
name: 'Skips out of component bindings',
title: 'Skips out of component bindings',
code: `

@@ -104,3 +104,3 @@ import { useAutoMemo } from './hooks.macro'

{
name: 'Works with function calls',
title: 'Skips internal bindings',
code: `

@@ -110,2 +110,64 @@ import { useAutoMemo } from './hooks.macro'

function FakeComponent() {
const value = [1, 2, 3];
const result = useAutoMemo(() => value.map(v => v * 2));
}
`,
},
{
title: 'Skips internal bindings with omonims',
code: `
import { useAutoMemo } from './hooks.macro'
function FakeComponent() {
const v = 12;
const value = [1, 2, 3];
const result = useAutoMemo(() => value.map(v => v * 2));
}
`,
},
{
title: 'Skips internal destructuring bindings with omonims',
code: `
import { useAutoMemo } from './hooks.macro'
function FakeComponent() {
const v = 12;
const value = [1, 2, 3];
const result = useAutoMemo(() => {
const [v] = value;
return v;
});
}
`,
},
{
title: 'Skips internal const bindings with omonims',
code: `
import { useAutoMemo } from './hooks.macro'
function FakeComponent() {
const v = 12;
const result = useAutoMemo(() => {
const v = 42;
return v;
});
}
`,
},
{
title: 'Skips globals',
code: `
import { useAutoMemo } from './hooks.macro'
function FakeComponent() {
const result = useAutoMemo(() => window.innerHeight);
}
`,
},
{
title: 'Works with function calls',
code: `
import { useAutoMemo } from './hooks.macro'
function FakeComponent() {
function callback() {}

@@ -117,3 +179,3 @@ const result = useAutoMemo(() => callback());

{
name: 'Works with function experssions',
title: 'Works with function experssions',
code: `

@@ -120,0 +182,0 @@ import { useAutoMemo } from './hooks.macro'

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc