Socket
Socket
Sign inDemoInstall

@linaria/shaker

Package Overview
Dependencies
204
Maintainers
4
Versions
49
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 3.0.0-beta.17 to 3.0.0-beta.18

12

CHANGELOG.md

@@ -6,2 +6,14 @@ # Change Log

# [3.0.0-beta.18](https://github.com/callstack/linaria/compare/v3.0.0-beta.17...v3.0.0-beta.18) (2022-04-01)
### Bug Fixes
* **shaker:** fix edge case with polyfilled defineProperty ([#951](https://github.com/callstack/linaria/issues/951)) ([38a5541](https://github.com/callstack/linaria/commit/38a5541d26142cafa859ceffa6922ef559c57100))
* **shaker:** fix some edge cases related to export patterns ([#951](https://github.com/callstack/linaria/issues/951)) ([18ca481](https://github.com/callstack/linaria/commit/18ca481f1f85ebcdc2704cc4af2173dcf9a4bb7f))
# [3.0.0-beta.17](https://github.com/callstack/linaria/compare/v3.0.0-beta.16...v3.0.0-beta.17) (2021-12-27)

@@ -8,0 +20,0 @@

45

esm/graphBuilder.js

@@ -31,3 +31,3 @@ import { types as t } from '@babel/core';

isExportsAssigment(node) {
isExportsAssignment(node) {
if (node && t.isAssignmentExpression(node) && t.isMemberExpression(node.left)) {

@@ -104,3 +104,3 @@ if (this.isExportsIdentifier(node.left)) {

if (this.isExportsAssigment(node) && !this.isExportsAssigment(node.right) && !isVoid(node.right)) {
if (this.isExportsAssignment(node) && !this.isExportsAssignment(node.right) && !isVoid(node.right)) {
if (t.isMemberExpression(node.left) && (t.isIdentifier(node.left.property) || t.isStringLiteral(node.left.property))) {

@@ -124,8 +124,10 @@ if (t.isIdentifier(node.left.object) && node.left.object.name === 'module') {

this.graph.addEdge(node.right, node);
this.graph.addEdge(node, node.left); // We have done all the required work, so stop here
return;
this.graph.addEdge(node, node.left);
} else {
this.graph.addExport('default', node);
}
this.graph.addEdge(node, node.left);
} // Regardless of whether the node.right is an object expression, this may also be the default export
this.graph.addExport('default', node);
} else {

@@ -141,2 +143,33 @@ // it can be either `exports.name` or `exports["name"]`

this.graph.addEdge(node, identifier);
} else if (t.isVariableDeclaration(node)) {
// We might be assigning to the exports, eg. `var Padding = exports.Padding = ...`
// or it might be a sequence and look like var foo = 1, var Name = exports.name = ...
node.declarations.forEach(declaration => {
if (t.isVariableDeclarator(declaration) && t.isAssignmentExpression(declaration.init)) {
let currentAssignmentExpression = declaration.init;
let addedExport = false;
let edgesToAdd = []; // loop through the assignments looking for possible exports
while (t.isAssignmentExpression(currentAssignmentExpression)) {
edgesToAdd.push(currentAssignmentExpression);
if (this.isExportsAssignment(currentAssignmentExpression) && t.isMemberExpression(currentAssignmentExpression.left) && (t.isIdentifier(currentAssignmentExpression.left.property) || t.isStringLiteral(currentAssignmentExpression.left.property))) {
const nameNode = currentAssignmentExpression.left.property;
this.graph.addExport(t.isStringLiteral(nameNode) ? nameNode.value : nameNode.name, node);
addedExport = true;
edgesToAdd.push(declaration);
edgesToAdd.push(currentAssignmentExpression.left);
edgesToAdd.push(currentAssignmentExpression.right);
}
currentAssignmentExpression = currentAssignmentExpression.right;
}
if (addedExport) {
edgesToAdd.forEach(edge => {
this.graph.addEdge(node, edge);
});
}
}
});
}

@@ -143,0 +176,0 @@

@@ -97,2 +97,21 @@ import { types as t } from '@babel/core';

}
function isMethodWithSideEffect(callee, state) {
const methods = ['assign', 'defineProperty', 'defineProperties', 'freeze', 'observe'];
if (t.isMemberExpression(callee) && isIdentifier(callee.object, 'Object') && isIdentifier(callee.property, methods)) {
// It's something like Object.defineProperty
return true;
}
if (t.isMemberExpression(callee) && isIdentifier(callee.property, 'default') && isIdentifier(callee.object)) {
// It looks like a call of imported method. Maybe it's a polyfill for Object's methods?
const declaration = state.scope.getDeclaration(callee.object);
if (!declaration || !isIdentifier(declaration)) return false;
const source = state.graph.importAliases.get(declaration);
return methods.some(method => `@babel/runtime/helpers/${method}` === source);
}
return false;
}
/*

@@ -107,3 +126,3 @@ * Returns nodes which are implicitly affected by specified node

if (t.isCallExpression(node) && t.isMemberExpression(callee) && isIdentifier(callee.object, 'Object') && isIdentifier(callee.property, ['assign', 'defineProperty', 'defineProperties', 'freeze', 'observe'])) {
if (t.isCallExpression(node) && isMethodWithSideEffect(callee, state)) {
const [obj, property] = node.arguments;

@@ -110,0 +129,0 @@

@@ -44,3 +44,3 @@ "use strict";

isExportsAssigment(node) {
isExportsAssignment(node) {
if (node && _core.types.isAssignmentExpression(node) && _core.types.isMemberExpression(node.left)) {

@@ -119,3 +119,3 @@ if (this.isExportsIdentifier(node.left)) {

if (this.isExportsAssigment(node) && !this.isExportsAssigment(node.right) && !isVoid(node.right)) {
if (this.isExportsAssignment(node) && !this.isExportsAssignment(node.right) && !isVoid(node.right)) {
if (_core.types.isMemberExpression(node.left) && (_core.types.isIdentifier(node.left.property) || _core.types.isStringLiteral(node.left.property))) {

@@ -139,8 +139,10 @@ if (_core.types.isIdentifier(node.left.object) && node.left.object.name === 'module') {

this.graph.addEdge(node.right, node);
this.graph.addEdge(node, node.left); // We have done all the required work, so stop here
return;
this.graph.addEdge(node, node.left);
} else {
this.graph.addExport('default', node);
}
this.graph.addEdge(node, node.left);
} // Regardless of whether the node.right is an object expression, this may also be the default export
this.graph.addExport('default', node);
} else {

@@ -156,2 +158,33 @@ // it can be either `exports.name` or `exports["name"]`

this.graph.addEdge(node, identifier);
} else if (_core.types.isVariableDeclaration(node)) {
// We might be assigning to the exports, eg. `var Padding = exports.Padding = ...`
// or it might be a sequence and look like var foo = 1, var Name = exports.name = ...
node.declarations.forEach(declaration => {
if (_core.types.isVariableDeclarator(declaration) && _core.types.isAssignmentExpression(declaration.init)) {
let currentAssignmentExpression = declaration.init;
let addedExport = false;
let edgesToAdd = []; // loop through the assignments looking for possible exports
while (_core.types.isAssignmentExpression(currentAssignmentExpression)) {
edgesToAdd.push(currentAssignmentExpression);
if (this.isExportsAssignment(currentAssignmentExpression) && _core.types.isMemberExpression(currentAssignmentExpression.left) && (_core.types.isIdentifier(currentAssignmentExpression.left.property) || _core.types.isStringLiteral(currentAssignmentExpression.left.property))) {
const nameNode = currentAssignmentExpression.left.property;
this.graph.addExport(_core.types.isStringLiteral(nameNode) ? nameNode.value : nameNode.name, node);
addedExport = true;
edgesToAdd.push(declaration);
edgesToAdd.push(currentAssignmentExpression.left);
edgesToAdd.push(currentAssignmentExpression.right);
}
currentAssignmentExpression = currentAssignmentExpression.right;
}
if (addedExport) {
edgesToAdd.forEach(edge => {
this.graph.addEdge(node, edge);
});
}
}
});
}

@@ -158,0 +191,0 @@

@@ -110,2 +110,21 @@ "use strict";

}
function isMethodWithSideEffect(callee, state) {
const methods = ['assign', 'defineProperty', 'defineProperties', 'freeze', 'observe'];
if (_core.types.isMemberExpression(callee) && isIdentifier(callee.object, 'Object') && isIdentifier(callee.property, methods)) {
// It's something like Object.defineProperty
return true;
}
if (_core.types.isMemberExpression(callee) && isIdentifier(callee.property, 'default') && isIdentifier(callee.object)) {
// It looks like a call of imported method. Maybe it's a polyfill for Object's methods?
const declaration = state.scope.getDeclaration(callee.object);
if (!declaration || !isIdentifier(declaration)) return false;
const source = state.graph.importAliases.get(declaration);
return methods.some(method => `@babel/runtime/helpers/${method}` === source);
}
return false;
}
/*

@@ -120,3 +139,3 @@ * Returns nodes which are implicitly affected by specified node

if (_core.types.isCallExpression(node) && _core.types.isMemberExpression(callee) && isIdentifier(callee.object, 'Object') && isIdentifier(callee.property, ['assign', 'defineProperty', 'defineProperties', 'freeze', 'observe'])) {
if (_core.types.isCallExpression(node) && isMethodWithSideEffect(callee, state)) {
const [obj, property] = node.arguments;

@@ -123,0 +142,0 @@

8

package.json
{
"name": "@linaria/shaker",
"version": "3.0.0-beta.17",
"version": "3.0.0-beta.18",
"publishConfig": {

@@ -50,5 +50,5 @@ "access": "public"

"@babel/preset-env": ">=7",
"@linaria/babel-preset": "^3.0.0-beta.17",
"@linaria/babel-preset": "^3.0.0-beta.18",
"@linaria/logger": "^3.0.0-beta.15",
"@linaria/preeval": "^3.0.0-beta.17",
"@linaria/preeval": "^3.0.0-beta.18",
"babel-plugin-transform-react-remove-prop-types": "^0.4.24",

@@ -60,3 +60,3 @@ "ts-invariant": "^0.9.0"

},
"gitHead": "5084f5e4421d3ea60659e767410526875c50f7ba"
"gitHead": "c3f093a3a7fb4e7c82d23e44adb19a94438da68c"
}

@@ -10,3 +10,3 @@ import type { Node } from '@babel/types';

private isExportsIdentifier;
private isExportsAssigment;
private isExportsAssignment;
private isTSExporterCall;

@@ -13,0 +13,0 @@ baseVisit<TNode extends Node>(node: TNode, ignoreDeps?: boolean): void;

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc