New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@atlaskit/codemod-utils

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@atlaskit/codemod-utils - npm Package Compare versions

Comparing version

to
3.3.0

6

CHANGELOG.md
# @atlaskit/codemod-utils
## 3.3.0
### Minor Changes
- [`9729143f07b`](https://bitbucket.org/atlassian/atlassian-frontend/commits/9729143f07b) - Add support functions `getDynamicImportName`, `isCallExpressionCalleeImportType`, `isCallExpressionArgumentStringLiteralType`, `isCallExpressionArgumentValueMatches` and `addDynamicImport`.
## 3.2.2

@@ -4,0 +10,0 @@

@@ -138,2 +138,32 @@ "use strict";

});
Object.defineProperty(exports, "removeImport", {
enumerable: true,
get: function get() {
return _support.removeImport;
}
});
Object.defineProperty(exports, "getDynamicImportName", {
enumerable: true,
get: function get() {
return _support.getDynamicImportName;
}
});
Object.defineProperty(exports, "addDynamicImport", {
enumerable: true,
get: function get() {
return _support.addDynamicImport;
}
});
Object.defineProperty(exports, "tryCreateImport", {
enumerable: true,
get: function get() {
return _support.tryCreateImport;
}
});
Object.defineProperty(exports, "addToImport", {
enumerable: true,
get: function get() {
return _support.addToImport;
}
});

@@ -140,0 +170,0 @@ var _utils = require("./utils");

52

dist/cjs/utils/support.js

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

exports.getSafeImportName = getSafeImportName;
exports.testMethodVariantEach = exports.callExpressionArgMatchesString = exports.debug = exports.isEmpty = exports.shiftDefaultImport = exports.doesIdentifierExist = exports.addCommentToStartOfFile = exports.hasImportDeclaration = exports.hasJSXAttributesByName = exports.getJSXAttributesByName = exports.getDefaultSpecifier = void 0;
exports.addDynamicImport = exports.getDynamicImportName = exports.testMethodVariantEach = exports.callExpressionArgMatchesString = exports.debug = exports.isEmpty = exports.shiftDefaultImport = exports.doesIdentifierExist = exports.addCommentToStartOfFile = exports.hasImportDeclaration = exports.hasJSXAttributesByName = exports.getJSXAttributesByName = exports.getDefaultSpecifier = void 0;

@@ -32,5 +32,51 @@ var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));

return specifiers.nodes()[0].local.name;
} // not replacing newlines (which \s does)
}
var getDynamicImportName = function getDynamicImportName(j, source, importPath) {
var dynamicImports = source.find(j.VariableDeclarator).filter(function (variableDeclaratorPath) {
return j(variableDeclaratorPath).find(j.CallExpression).filter(function (callExpressionPath) {
var _callExpressionPath$n = callExpressionPath.node,
callee = _callExpressionPath$n.callee,
callExpressionArguments = _callExpressionPath$n.arguments;
return !!(isCallExpressionCalleeImportType(callee) && isCallExpressionArgumentStringLiteralType(callExpressionArguments) && isCallExpressionArgumentValueMatches(callExpressionArguments[0], j, importPath));
}).length > 0;
});
if (!dynamicImports.length) {
return null;
}
var id = dynamicImports.nodes()[0].id;
if (id.type !== 'Identifier') {
return null;
}
return id.name;
};
exports.getDynamicImportName = getDynamicImportName;
var isCallExpressionCalleeImportType = function isCallExpressionCalleeImportType(callee) {
return callee && callee.type === 'Import';
};
var isCallExpressionArgumentStringLiteralType = function isCallExpressionArgumentStringLiteralType(callExpressionArguments) {
return callExpressionArguments && callExpressionArguments.length && callExpressionArguments[0].type === 'StringLiteral';
};
var isCallExpressionArgumentValueMatches = function isCallExpressionArgumentValueMatches(callExpressionArgument, j, value) {
return j(callExpressionArgument).some(function (path) {
return path.node.value === value;
});
};
var addDynamicImport = function addDynamicImport(j, target, name, packageEndpoint) {
var node = j.variableDeclaration('const', [j.variableDeclarator(j.identifier(name), j.callExpression(j.memberExpression(j.identifier('React'), j.identifier('lazy')), [j.arrowFunctionExpression([], j.callExpression(j.import(), [j.stringLiteral(packageEndpoint)]))]))]);
target.insertAfter(node);
addCommentBefore(j, j(node), 'We have added "React.lazy" here. Feel free to change it to "lazy" or other named import depending upon how you imported.');
}; // not replacing newlines (which \s does)
exports.addDynamicImport = addDynamicImport;
var spacesAndTabs = /[ \t]{2,}/g;

@@ -91,3 +137,3 @@ var lineStartWithSpaces = /^[ \t]*/gm;

var getJSXAttributesByName = function getJSXAttributesByName(j, element, attributeName) {
return j(element).find(j.JSXOpeningElement).find(j.JSXAttribute).filter(function (attribute) {
return j(element).find(j.JSXOpeningElement).at(0).find(j.JSXAttribute).filter(function (attribute) {
var matches = j(attribute).find(j.JSXIdentifier).filter(function (identifier) {

@@ -94,0 +140,0 @@ return identifier.value.name === attributeName;

2

dist/cjs/version.json
{
"name": "@atlaskit/codemod-utils",
"version": "3.2.2",
"version": "3.3.0",
"sideEffects": false
}
export { createRenameFuncFor, createConvertFuncFor, createRenameImportFor, createRemoveFuncFor, replaceImportStatementFor, elevateComponentToNewEntryPoint, createTransformer, renameNamedImportWithAliasName, flattenCertainChildPropsAsProp, createRenameJSXFunc, createRemoveFuncAddCommentFor } from './utils';
export { getDefaultSpecifier, getNamedSpecifier, getJSXAttributesByName, hasJSXAttributesByName, doesIdentifierExist, hasImportDeclaration, addCommentBefore, addCommentToStartOfFile, callExpressionArgMatchesString, testMethodVariantEach, getSafeImportName } from './utils/support';
export { getDefaultSpecifier, getNamedSpecifier, getJSXAttributesByName, hasJSXAttributesByName, doesIdentifierExist, hasImportDeclaration, addCommentBefore, addCommentToStartOfFile, callExpressionArgMatchesString, testMethodVariantEach, getSafeImportName, removeImport, getDynamicImportName, addDynamicImport, tryCreateImport, addToImport } from './utils/support';

@@ -9,5 +9,49 @@ function getNamedSpecifier(j, source, specifier, importName) {

return specifiers.nodes()[0].local.name;
} // not replacing newlines (which \s does)
}
const getDynamicImportName = (j, source, importPath) => {
const dynamicImports = source.find(j.VariableDeclarator).filter(variableDeclaratorPath => {
return j(variableDeclaratorPath).find(j.CallExpression).filter(callExpressionPath => {
const {
callee,
arguments: callExpressionArguments
} = callExpressionPath.node;
return !!(isCallExpressionCalleeImportType(callee) && isCallExpressionArgumentStringLiteralType(callExpressionArguments) && isCallExpressionArgumentValueMatches(callExpressionArguments[0], j, importPath));
}).length > 0;
});
if (!dynamicImports.length) {
return null;
}
const {
id
} = dynamicImports.nodes()[0];
if (id.type !== 'Identifier') {
return null;
}
return id.name;
};
const isCallExpressionCalleeImportType = callee => {
return callee && callee.type === 'Import';
};
const isCallExpressionArgumentStringLiteralType = callExpressionArguments => {
return callExpressionArguments && callExpressionArguments.length && callExpressionArguments[0].type === 'StringLiteral';
};
const isCallExpressionArgumentValueMatches = (callExpressionArgument, j, value) => {
return j(callExpressionArgument).some(path => path.node.value === value);
};
const addDynamicImport = (j, target, name, packageEndpoint) => {
const node = j.variableDeclaration('const', [j.variableDeclarator(j.identifier(name), j.callExpression(j.memberExpression(j.identifier('React'), j.identifier('lazy')), [j.arrowFunctionExpression([], j.callExpression(j.import(), [j.stringLiteral(packageEndpoint)]))]))]);
target.insertAfter(node);
addCommentBefore(j, j(node), 'We have added "React.lazy" here. Feel free to change it to "lazy" or other named import depending upon how you imported.');
}; // not replacing newlines (which \s does)
const spacesAndTabs = /[ \t]{2,}/g;

@@ -59,3 +103,3 @@ const lineStartWithSpaces = /^[ \t]*/gm;

const getJSXAttributesByName = (j, element, attributeName) => {
return j(element).find(j.JSXOpeningElement).find(j.JSXAttribute).filter(attribute => {
return j(element).find(j.JSXOpeningElement).at(0).find(j.JSXAttribute).filter(attribute => {
const matches = j(attribute).find(j.JSXIdentifier).filter(identifier => identifier.value.name === attributeName);

@@ -206,2 +250,2 @@ return Boolean(matches.length);

export { getDefaultSpecifier, getNamedSpecifier, getJSXAttributesByName, hasJSXAttributesByName, hasImportDeclaration, addCommentBefore, addCommentToStartOfFile, doesIdentifierExist, removeImport, tryCreateImport, addToImport, shiftDefaultImport, isEmpty, clean, debug, callExpressionArgMatchesString, testMethodVariantEach, getSafeImportName };
export { getDefaultSpecifier, getNamedSpecifier, getJSXAttributesByName, hasJSXAttributesByName, hasImportDeclaration, addCommentBefore, addCommentToStartOfFile, doesIdentifierExist, removeImport, tryCreateImport, addToImport, shiftDefaultImport, isEmpty, clean, debug, callExpressionArgMatchesString, testMethodVariantEach, getSafeImportName, getDynamicImportName, addDynamicImport };
{
"name": "@atlaskit/codemod-utils",
"version": "3.2.2",
"version": "3.3.0",
"sideEffects": false
}
export { createRenameFuncFor, createConvertFuncFor, createRenameImportFor, createRemoveFuncFor, replaceImportStatementFor, elevateComponentToNewEntryPoint, createTransformer, renameNamedImportWithAliasName, flattenCertainChildPropsAsProp, createRenameJSXFunc, createRemoveFuncAddCommentFor } from './utils';
export { getDefaultSpecifier, getNamedSpecifier, getJSXAttributesByName, hasJSXAttributesByName, doesIdentifierExist, hasImportDeclaration, addCommentBefore, addCommentToStartOfFile, callExpressionArgMatchesString, testMethodVariantEach, getSafeImportName } from './utils/support';
export { getDefaultSpecifier, getNamedSpecifier, getJSXAttributesByName, hasJSXAttributesByName, doesIdentifierExist, hasImportDeclaration, addCommentBefore, addCommentToStartOfFile, callExpressionArgMatchesString, testMethodVariantEach, getSafeImportName, removeImport, getDynamicImportName, addDynamicImport, tryCreateImport, addToImport } from './utils/support';

@@ -15,5 +15,48 @@ import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";

return specifiers.nodes()[0].local.name;
} // not replacing newlines (which \s does)
}
var getDynamicImportName = function getDynamicImportName(j, source, importPath) {
var dynamicImports = source.find(j.VariableDeclarator).filter(function (variableDeclaratorPath) {
return j(variableDeclaratorPath).find(j.CallExpression).filter(function (callExpressionPath) {
var _callExpressionPath$n = callExpressionPath.node,
callee = _callExpressionPath$n.callee,
callExpressionArguments = _callExpressionPath$n.arguments;
return !!(isCallExpressionCalleeImportType(callee) && isCallExpressionArgumentStringLiteralType(callExpressionArguments) && isCallExpressionArgumentValueMatches(callExpressionArguments[0], j, importPath));
}).length > 0;
});
if (!dynamicImports.length) {
return null;
}
var id = dynamicImports.nodes()[0].id;
if (id.type !== 'Identifier') {
return null;
}
return id.name;
};
var isCallExpressionCalleeImportType = function isCallExpressionCalleeImportType(callee) {
return callee && callee.type === 'Import';
};
var isCallExpressionArgumentStringLiteralType = function isCallExpressionArgumentStringLiteralType(callExpressionArguments) {
return callExpressionArguments && callExpressionArguments.length && callExpressionArguments[0].type === 'StringLiteral';
};
var isCallExpressionArgumentValueMatches = function isCallExpressionArgumentValueMatches(callExpressionArgument, j, value) {
return j(callExpressionArgument).some(function (path) {
return path.node.value === value;
});
};
var addDynamicImport = function addDynamicImport(j, target, name, packageEndpoint) {
var node = j.variableDeclaration('const', [j.variableDeclarator(j.identifier(name), j.callExpression(j.memberExpression(j.identifier('React'), j.identifier('lazy')), [j.arrowFunctionExpression([], j.callExpression(j.import(), [j.stringLiteral(packageEndpoint)]))]))]);
target.insertAfter(node);
addCommentBefore(j, j(node), 'We have added "React.lazy" here. Feel free to change it to "lazy" or other named import depending upon how you imported.');
}; // not replacing newlines (which \s does)
var spacesAndTabs = /[ \t]{2,}/g;

@@ -70,3 +113,3 @@ var lineStartWithSpaces = /^[ \t]*/gm;

var getJSXAttributesByName = function getJSXAttributesByName(j, element, attributeName) {
return j(element).find(j.JSXOpeningElement).find(j.JSXAttribute).filter(function (attribute) {
return j(element).find(j.JSXOpeningElement).at(0).find(j.JSXAttribute).filter(function (attribute) {
var matches = j(attribute).find(j.JSXIdentifier).filter(function (identifier) {

@@ -240,2 +283,2 @@ return identifier.value.name === attributeName;

export { getDefaultSpecifier, getNamedSpecifier, getJSXAttributesByName, hasJSXAttributesByName, hasImportDeclaration, addCommentBefore, addCommentToStartOfFile, doesIdentifierExist, removeImport, tryCreateImport, addToImport, shiftDefaultImport, isEmpty, clean, debug, callExpressionArgMatchesString, testMethodVariantEach, getSafeImportName };
export { getDefaultSpecifier, getNamedSpecifier, getJSXAttributesByName, hasJSXAttributesByName, hasImportDeclaration, addCommentBefore, addCommentToStartOfFile, doesIdentifierExist, removeImport, tryCreateImport, addToImport, shiftDefaultImport, isEmpty, clean, debug, callExpressionArgMatchesString, testMethodVariantEach, getSafeImportName, getDynamicImportName, addDynamicImport };
{
"name": "@atlaskit/codemod-utils",
"version": "3.2.2",
"version": "3.3.0",
"sideEffects": false
}
export { createRenameFuncFor, createConvertFuncFor, createRenameImportFor, createRemoveFuncFor, replaceImportStatementFor, elevateComponentToNewEntryPoint, createTransformer, renameNamedImportWithAliasName, flattenCertainChildPropsAsProp, createRenameJSXFunc, createRemoveFuncAddCommentFor, } from './utils';
export { getDefaultSpecifier, getNamedSpecifier, getJSXAttributesByName, hasJSXAttributesByName, doesIdentifierExist, hasImportDeclaration, addCommentBefore, addCommentToStartOfFile, callExpressionArgMatchesString, testMethodVariantEach, getSafeImportName, } from './utils/support';
export { getDefaultSpecifier, getNamedSpecifier, getJSXAttributesByName, hasJSXAttributesByName, doesIdentifierExist, hasImportDeclaration, addCommentBefore, addCommentToStartOfFile, callExpressionArgMatchesString, testMethodVariantEach, getSafeImportName, removeImport, getDynamicImportName, addDynamicImport, tryCreateImport, addToImport, } from './utils/support';

@@ -1,4 +0,6 @@

import core, { ASTPath, CallExpression, ImportDeclaration, ImportDefaultSpecifier, ImportSpecifier, JSXElement, Program, VariableDeclarator } from 'jscodeshift';
import core, { ASTPath, CallExpression, ImportDeclaration, ImportDefaultSpecifier, ImportSpecifier, JSXElement, Program, VariableDeclaration, VariableDeclarator } from 'jscodeshift';
import { Collection } from 'jscodeshift/src/Collection';
declare function getNamedSpecifier(j: core.JSCodeshift, source: Collection<Node>, specifier: string, importName: string): string | null;
declare const getDynamicImportName: (j: core.JSCodeshift, source: Collection<any>, importPath: string) => string | null;
declare const addDynamicImport: (j: core.JSCodeshift, target: Collection<VariableDeclaration>, name: string, packageEndpoint: string) => void;
declare function clean(value: string): string;

@@ -31,2 +33,2 @@ declare const addCommentToStartOfFile: ({ j, base, message, }: {

}): string;
export { getDefaultSpecifier, getNamedSpecifier, getJSXAttributesByName, hasJSXAttributesByName, hasImportDeclaration, addCommentBefore, addCommentToStartOfFile, doesIdentifierExist, removeImport, tryCreateImport, addToImport, shiftDefaultImport, isEmpty, clean, debug, callExpressionArgMatchesString, testMethodVariantEach, getSafeImportName, };
export { getDefaultSpecifier, getNamedSpecifier, getJSXAttributesByName, hasJSXAttributesByName, hasImportDeclaration, addCommentBefore, addCommentToStartOfFile, doesIdentifierExist, removeImport, tryCreateImport, addToImport, shiftDefaultImport, isEmpty, clean, debug, callExpressionArgMatchesString, testMethodVariantEach, getSafeImportName, getDynamicImportName, addDynamicImport, };
{
"name": "@atlaskit/codemod-utils",
"version": "3.2.2",
"version": "3.3.0",
"author": "Atlassian Pty Ltd",

@@ -5,0 +5,0 @@ "license": "Apache-2.0",