@linaria/utils
Advanced tools
Comparing version 4.3.2 to 4.3.3
@@ -0,4 +1,44 @@ | ||
/** | ||
* Checks if a given path has been removed from the AST. | ||
*/ | ||
export default function isRemoved(path) { | ||
return path.find(p => p.removed) !== null; | ||
// Check if the input path has already been removed | ||
if (path.removed) { | ||
return true; | ||
} | ||
// Check if any of the parent paths have been removed | ||
let currentPath = path; | ||
while (currentPath) { | ||
const parent = currentPath.parentPath; | ||
if (parent) { | ||
// If the parent path has been removed, return true | ||
if (parent.removed) { | ||
return true; | ||
} | ||
const { | ||
listKey, | ||
key | ||
} = currentPath; | ||
if (listKey) { | ||
// If the current path is part of a list and its node is not the same | ||
// as the node in the parent list at the same index, return true | ||
if (parent.get(listKey)[key].node !== currentPath.node) { | ||
return true; | ||
} | ||
} | ||
// If the current path is not part of a list and its node is not the same | ||
// as the node in the parent object at the same key, return true | ||
else if (parent.get(key).node !== currentPath.node) { | ||
return true; | ||
} | ||
} | ||
// Set the current path to its parent path and continue the loop | ||
currentPath = parent; | ||
} | ||
// If the function has not returned true by this point, return false | ||
return false; | ||
} | ||
//# sourceMappingURL=isRemoved.js.map |
@@ -79,2 +79,15 @@ /* eslint-disable no-restricted-syntax */ | ||
}; | ||
function canFunctionBeDelete(fnPath) { | ||
const fnScope = fnPath.scope; | ||
const parentScope = fnScope.parent; | ||
if (parentScope.parent) { | ||
// It isn't a top-level function, so we can't delete it | ||
return true; | ||
} | ||
if (fnPath.listKey === 'arguments') { | ||
// It is passed as an argument to another function, we can't delete it | ||
return true; | ||
} | ||
return false; | ||
} | ||
export function findActionForNode(path) { | ||
@@ -89,5 +102,22 @@ if (isRemoved(path)) return null; | ||
} | ||
if (parent.isFunction() && path.listKey === 'params') { | ||
// Do not remove params of functions | ||
return null; | ||
if (parent.isFunction()) { | ||
if (path.listKey === 'params') { | ||
// Do not remove params of functions | ||
return null; | ||
} | ||
if (path.isBlockStatement() && isEmptyList(path.get('body')) || path === parent.get('body')) { | ||
if (!canFunctionBeDelete(parent)) { | ||
return ['replace', parent, { | ||
...parent.node, | ||
async: false, | ||
body: { | ||
type: 'BlockStatement', | ||
body: [], | ||
directives: [] | ||
}, | ||
generator: false, | ||
params: [] | ||
}]; | ||
} | ||
} | ||
} | ||
@@ -236,2 +266,13 @@ if (parent.isLogicalExpression({ | ||
} | ||
function applyAction(action) { | ||
mutate(action[1], p => { | ||
if (isRemoved(p)) return; | ||
if (action[0] === 'remove') { | ||
p.remove(); | ||
} | ||
if (action[0] === 'replace') { | ||
p.replaceWith(action[2]); | ||
} | ||
}); | ||
} | ||
function removeWithRelated(paths) { | ||
@@ -251,12 +292,3 @@ if (paths.length === 0) return; | ||
const referencesOfBinding = findIdentifiers(affectedPaths, 'binding').map(i => (i.node && getScope(i).getBinding(i.node.name)) ?? null).filter(isNotNull).reduce((acc, i) => [...acc, ...i.referencePaths.filter(nonType)], []); | ||
actions.forEach(action => { | ||
mutate(action[1], p => { | ||
if (isRemoved(p)) return; | ||
if (action[0] === 'remove') { | ||
p.remove(); | ||
} else if (action[0] === 'replace') { | ||
p.replaceWith(action[2]); | ||
} | ||
}); | ||
}); | ||
actions.forEach(applyAction); | ||
removeWithRelated(referencesOfBinding); | ||
@@ -312,3 +344,3 @@ let clean = false; | ||
} | ||
export { mutate, removeWithRelated }; | ||
export { applyAction, mutate, removeWithRelated }; | ||
//# sourceMappingURL=scopeHelpers.js.map |
@@ -7,5 +7,45 @@ "use strict"; | ||
exports.default = isRemoved; | ||
/** | ||
* Checks if a given path has been removed from the AST. | ||
*/ | ||
function isRemoved(path) { | ||
return path.find(p => p.removed) !== null; | ||
// Check if the input path has already been removed | ||
if (path.removed) { | ||
return true; | ||
} | ||
// Check if any of the parent paths have been removed | ||
let currentPath = path; | ||
while (currentPath) { | ||
const parent = currentPath.parentPath; | ||
if (parent) { | ||
// If the parent path has been removed, return true | ||
if (parent.removed) { | ||
return true; | ||
} | ||
const { | ||
listKey, | ||
key | ||
} = currentPath; | ||
if (listKey) { | ||
// If the current path is part of a list and its node is not the same | ||
// as the node in the parent list at the same index, return true | ||
if (parent.get(listKey)[key].node !== currentPath.node) { | ||
return true; | ||
} | ||
} | ||
// If the current path is not part of a list and its node is not the same | ||
// as the node in the parent object at the same key, return true | ||
else if (parent.get(key).node !== currentPath.node) { | ||
return true; | ||
} | ||
} | ||
// Set the current path to its parent path and continue the loop | ||
currentPath = parent; | ||
} | ||
// If the function has not returned true by this point, return false | ||
return false; | ||
} | ||
//# sourceMappingURL=isRemoved.js.map |
@@ -6,2 +6,3 @@ "use strict"; | ||
}); | ||
exports.applyAction = applyAction; | ||
exports.dereference = dereference; | ||
@@ -94,2 +95,15 @@ exports.findActionForNode = findActionForNode; | ||
}; | ||
function canFunctionBeDelete(fnPath) { | ||
const fnScope = fnPath.scope; | ||
const parentScope = fnScope.parent; | ||
if (parentScope.parent) { | ||
// It isn't a top-level function, so we can't delete it | ||
return true; | ||
} | ||
if (fnPath.listKey === 'arguments') { | ||
// It is passed as an argument to another function, we can't delete it | ||
return true; | ||
} | ||
return false; | ||
} | ||
function findActionForNode(path) { | ||
@@ -104,5 +118,22 @@ if ((0, _isRemoved.default)(path)) return null; | ||
} | ||
if (parent.isFunction() && path.listKey === 'params') { | ||
// Do not remove params of functions | ||
return null; | ||
if (parent.isFunction()) { | ||
if (path.listKey === 'params') { | ||
// Do not remove params of functions | ||
return null; | ||
} | ||
if (path.isBlockStatement() && isEmptyList(path.get('body')) || path === parent.get('body')) { | ||
if (!canFunctionBeDelete(parent)) { | ||
return ['replace', parent, { | ||
...parent.node, | ||
async: false, | ||
body: { | ||
type: 'BlockStatement', | ||
body: [], | ||
directives: [] | ||
}, | ||
generator: false, | ||
params: [] | ||
}]; | ||
} | ||
} | ||
} | ||
@@ -254,2 +285,13 @@ if (parent.isLogicalExpression({ | ||
} | ||
function applyAction(action) { | ||
mutate(action[1], p => { | ||
if ((0, _isRemoved.default)(p)) return; | ||
if (action[0] === 'remove') { | ||
p.remove(); | ||
} | ||
if (action[0] === 'replace') { | ||
p.replaceWith(action[2]); | ||
} | ||
}); | ||
} | ||
function removeWithRelated(paths) { | ||
@@ -275,12 +317,3 @@ if (paths.length === 0) return; | ||
}).filter(_isNotNull.default).reduce((acc, i) => [...acc, ...i.referencePaths.filter(_findIdentifiers.nonType)], []); | ||
actions.forEach(action => { | ||
mutate(action[1], p => { | ||
if ((0, _isRemoved.default)(p)) return; | ||
if (action[0] === 'remove') { | ||
p.remove(); | ||
} else if (action[0] === 'replace') { | ||
p.replaceWith(action[2]); | ||
} | ||
}); | ||
}); | ||
actions.forEach(applyAction); | ||
removeWithRelated(referencesOfBinding); | ||
@@ -287,0 +320,0 @@ let clean = false; |
{ | ||
"name": "@linaria/utils", | ||
"version": "4.3.2", | ||
"version": "4.3.3", | ||
"description": "Blazing fast zero-runtime CSS in JS library", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
import type { NodePath } from '@babel/traverse'; | ||
/** | ||
* Checks if a given path has been removed from the AST. | ||
*/ | ||
export default function isRemoved(path: NodePath): boolean; |
import type { Binding, NodePath } from '@babel/traverse'; | ||
import type { Node, Identifier, JSXIdentifier } from '@babel/types'; | ||
import type { Identifier, JSXIdentifier, Node } from '@babel/types'; | ||
export declare function reference(path: NodePath<Identifier | JSXIdentifier>, referencePath?: NodePath, force?: boolean): void; | ||
@@ -9,4 +9,5 @@ export declare function dereference(path: NodePath<Identifier | JSXIdentifier>): Binding | null; | ||
export declare function findActionForNode(path: NodePath): RemoveAction | ReplaceAction | null; | ||
declare function applyAction(action: ReplaceAction | RemoveAction): void; | ||
declare function removeWithRelated(paths: NodePath[]): void; | ||
declare function mutate<T extends NodePath>(path: T, fn: (p: T) => NodePath[] | void): void; | ||
export { mutate, removeWithRelated }; | ||
export { applyAction, mutate, removeWithRelated }; |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
402652
4221