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

@hh.ru/babel-plugin-static-value-extractor

Package Overview
Dependencies
Maintainers
9
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@hh.ru/babel-plugin-static-value-extractor - npm Package Compare versions

Comparing version 0.5.1 to 0.5.2

202

lib/index.js

@@ -1,149 +0,159 @@

'use strict';
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
value: true
});
exports.extractStaticValueImportedFilesFromFile = exports.extractStaticValueFromFile = undefined;
exports["default"] = exports.extractStaticValueImportedFilesFromFile = exports.extractStaticValueFromFile = void 0;
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _fs = _interopRequireDefault(require("fs"));
var _fs = require('fs');
var _path = _interopRequireDefault(require("path"));
var _fs2 = _interopRequireDefault(_fs);
var babylon = _interopRequireWildcard(require("@babel/parser"));
var _path = require('path');
var _traverse = _interopRequireDefault(require("@babel/traverse"));
var _path2 = _interopRequireDefault(_path);
var _globAll = _interopRequireDefault(require("glob-all"));
var _babylon = require('babylon');
var _traverser = _interopRequireDefault(require("./traverser"));
var babylon = _interopRequireWildcard(_babylon);
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj["default"] = obj; return newObj; } }
var _babelTraverse = require('babel-traverse');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
var _babelTraverse2 = _interopRequireDefault(_babelTraverse);
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
var _globAll = require('glob-all');
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
var _globAll2 = _interopRequireDefault(_globAll);
function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
var _traverser = require('./traverser');
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }
var _traverser2 = _interopRequireDefault(_traverser);
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { keys.push.apply(keys, Object.getOwnPropertySymbols(object)); } if (enumerableOnly) keys = keys.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); return keys; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
var ENCODING = 'utf8';
var BABEL_PARSING_OPTS = {
sourceType: 'module',
plugins: ['jsx', 'flow', 'doExpressions', 'objectRestSpread', 'decorators', 'classProperties', 'exportExtensions', 'asyncGenerators', 'functionBind', 'functionSent', 'dynamicImport']
sourceType: 'module',
plugins: ['jsx', 'flow', 'doExpressions', 'objectRestSpread', 'decorators-legacy', 'classProperties', 'exportExtensions', 'asyncGenerators', 'functionBind', 'functionSent', 'dynamicImport']
};
var noop = function noop() {};
var extractStaticValueFromCode = function extractStaticValueFromCode(code) {
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var cb = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : noop;
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var cb = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : noop;
try {
var ast = babylon.parse(code.toString(ENCODING), BABEL_PARSING_OPTS);
var traverser = (0, _traverser2.default)(cb, opts);
(0, _babelTraverse2.default)(ast, traverser);
} catch (error) {
if (process.env.NODE_ENV === 'development') {
console.log(error);
} else {
throw error;
}
try {
var ast = babylon.parse(code.toString(ENCODING), BABEL_PARSING_OPTS);
var traverser = (0, _traverser["default"])(cb, opts);
(0, _traverse["default"])(ast, traverser);
} catch (error) {
if (process.env.NODE_ENV === 'development') {
console.log(error);
} else {
throw error;
}
}
};
var extractStaticValueFromFile = exports.extractStaticValueFromFile = function extractStaticValueFromFile(file) {
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var cb = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : noop;
extractStaticValueFromCode(_fs2.default.readFileSync(file), _extends({}, opts, {
filename: file
}), cb);
var extractStaticValueFromFile = function extractStaticValueFromFile(file) {
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var cb = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : noop;
extractStaticValueFromCode(_fs["default"].readFileSync(file), _objectSpread({}, opts, {
filename: file
}), cb);
};
exports.extractStaticValueFromFile = extractStaticValueFromFile;
var cachedFiles = {};
var extractStaticValueImportedFilesFromFile = exports.extractStaticValueImportedFilesFromFile = function extractStaticValueImportedFilesFromFile(file) {
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var cb = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : noop;
var extractStaticValueImportedFilesFromFile = function extractStaticValueImportedFilesFromFile(file) {
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var cb = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : noop;
var staticPropsList = [];
var staticPropsList = [];
function _extractStaticValueImportedFilesFromFile(file, opts) {
var importsDeclarations = [];
function _extractStaticValueImportedFilesFromFile(file, opts) {
var importsDeclarations = [];
if (opts.include && !opts.include.find(function (includePath) {
return file.search(includePath) !== -1;
})) {
return;
}
if (cachedFiles[file]) {
staticPropsList = staticPropsList.concat(cachedFiles[file].propsList);
importsDeclarations = cachedFiles[file].importsDeclarations;
} else {
extractStaticValueFromFile(file, opts, function (_staticPropsList, _importsDeclarations) {
staticPropsList = staticPropsList.concat(_staticPropsList);
importsDeclarations = _importsDeclarations;
cachedFiles[file] = { propsList: _staticPropsList, importsDeclarations: importsDeclarations };
});
}
importsDeclarations.forEach(function (file) {
_extractStaticValueImportedFilesFromFile(file, opts, staticPropsList);
});
if (opts.include && !opts.include.find(function (includePath) {
return file.search(includePath) !== -1;
})) {
return;
}
if (cachedFiles[file]) {
staticPropsList = cachedFiles[file].propsList;
staticPropsList = staticPropsList.concat(cachedFiles[file].propsList);
importsDeclarations = cachedFiles[file].importsDeclarations;
} else {
_extractStaticValueImportedFilesFromFile(file, opts);
cachedFiles[file] = { propsList: staticPropsList, importsDeclarations: [] };
extractStaticValueFromFile(file, opts, function (_staticPropsList, _importsDeclarations) {
staticPropsList = staticPropsList.concat(_staticPropsList);
importsDeclarations = _importsDeclarations;
cachedFiles[file] = {
propsList: _staticPropsList,
importsDeclarations: importsDeclarations
};
});
}
var values = [].concat(_toConsumableArray(new Set(cachedFiles[file].propsList)));
importsDeclarations.forEach(function (file) {
_extractStaticValueImportedFilesFromFile(file, opts, staticPropsList);
});
}
cb(values);
if (cachedFiles[file]) {
staticPropsList = cachedFiles[file].propsList;
} else {
_extractStaticValueImportedFilesFromFile(file, opts);
return values;
cachedFiles[file] = {
propsList: staticPropsList,
importsDeclarations: []
};
}
var values = _toConsumableArray(new Set(cachedFiles[file].propsList));
cb(values);
return values;
};
exports.default = function (globArr) {
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
exports.extractStaticValueImportedFilesFromFile = extractStaticValueImportedFilesFromFile;
var saveFilePath = _path2.default.resolve(opts.saveFilePath);
var PATH_DELIMITER_LENGTH = 1;
var previousContent = void 0;
var _default = function _default(globArr) {
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var staticValues = _globAll2.default.sync(globArr).reduce(function (globObject, file) {
var staticValues = extractStaticValueImportedFilesFromFile(file, opts);
var dir = _path2.default.parse(file).dir;
var saveFilePath = _path["default"].resolve(opts.saveFilePath);
globObject[dir.slice(dir.lastIndexOf('/') + PATH_DELIMITER_LENGTH)] = staticValues;
var PATH_DELIMITER_LENGTH = 1;
var previousContent;
return globObject;
}, {});
var staticValues = _globAll["default"].sync(globArr).reduce(function (globObject, file) {
var staticValues = extractStaticValueImportedFilesFromFile(file, opts);
if (_fs2.default.existsSync(saveFilePath + '/' + opts.saveFileName + '.' + opts.saveFileExt)) {
previousContent = _fs2.default.readFileSync(saveFilePath + '/' + opts.saveFileName + '.' + opts.saveFileExt, ENCODING).toString();
}
var dir = _path["default"].parse(file).dir;
var content = opts.template ? opts.template(staticValues) : JSON.stringify(staticValues);
globObject[dir.slice(dir.lastIndexOf('/') + PATH_DELIMITER_LENGTH)] = staticValues;
return globObject;
}, {});
cachedFiles = {};
if (_fs["default"].existsSync("".concat(saveFilePath, "/").concat(opts.saveFileName, ".").concat(opts.saveFileExt))) {
previousContent = _fs["default"].readFileSync("".concat(saveFilePath, "/").concat(opts.saveFileName, ".").concat(opts.saveFileExt), ENCODING).toString();
}
if (content !== previousContent) {
_fs2.default.mkdirSync(saveFilePath, { recursive: true });
_fs2.default.writeFileSync(saveFilePath + '/' + opts.saveFileName + '.' + opts.saveFileExt, content);
}
};
var content = opts.template ? opts.template(staticValues) : JSON.stringify(staticValues);
cachedFiles = {};
if (content !== previousContent) {
_fs["default"].mkdirSync(saveFilePath, {
recursive: true
});
_fs["default"].writeFileSync("".concat(saveFilePath, "/").concat(opts.saveFileName, ".").concat(opts.saveFileExt), content);
}
};
exports["default"] = _default;

@@ -1,143 +0,138 @@

'use strict';
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
value: true
});
exports["default"] = void 0;
var _fs = require('fs');
var _fs = _interopRequireDefault(require("fs"));
var _fs2 = _interopRequireDefault(_fs);
var _path = _interopRequireDefault(require("path"));
var _path = require('path');
var _core = require("@babel/core");
var _path2 = _interopRequireDefault(_path);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
var _babelCore = require('babel-core');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var JS_EXTENSIONS = ['', 'js', 'jsx'];
var getConcatenatedStaticProps = function getConcatenatedStaticProps(staticProps, nodeStaticProps) {
return staticProps.concat(nodeStaticProps.reduce(function (arr, _ref) {
var value = _ref.value;
return staticProps.concat(nodeStaticProps.reduce(function (arr, _ref) {
var value = _ref.value;
if (_babelCore.types.isStringLiteral(value)) {
arr.push(value.value);
}
if (_core.types.isStringLiteral(value)) {
arr.push(value.value);
}
return arr;
}, []));
return arr;
}, []));
};
var isJsFile = function isJsFile(path) {
return JS_EXTENSIONS.includes(_path2.default.parse(path).ext);
return JS_EXTENSIONS.includes(_path["default"].parse(path).ext);
};
var replacePath = function replacePath(path) {
var pathsToReplace = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return Object.keys(pathsToReplace).filter(function (checkPath) {
return path.startsWith(checkPath);
}).reduce(function (result, checkPath) {
return _path2.default.normalize(result.replace(checkPath, pathsToReplace[checkPath]));
}, path);
var pathsToReplace = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return Object.keys(pathsToReplace).filter(function (checkPath) {
return path.startsWith(checkPath);
}).reduce(function (result, checkPath) {
return _path["default"].normalize(result.replace(checkPath, pathsToReplace[checkPath]));
}, path);
};
exports.default = function (cb) {
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var _default = function _default(cb) {
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var staticProps = [];
var staticProps = [];
var currentFileDir = _path2.default.parse(opts.filename).dir;
var staticPropName = opts.staticPropName,
pathsToReplace = opts.pathsToReplace;
var currentFileDir = _path["default"].parse(opts.filename).dir;
var importDeclarationPaths = [];
var staticPropName = opts.staticPropName,
pathsToReplace = opts.pathsToReplace;
var importDeclarationPaths = [];
var processImports = function processImports(filePath) {
var defaultFileName = _path2.default.parse(filePath).ext ? '' : 'index.jsx';
var importPath = _path2.default.resolve(currentFileDir, filePath);
var processImports = function processImports(filePath) {
var defaultFileName = _path["default"].parse(filePath).ext ? '' : 'index.jsx';
if (_fs2.default.existsSync(importPath)) {
importDeclarationPaths.push(_path2.default.resolve(importPath, defaultFileName));
return;
}
var importPath = _path["default"].resolve(currentFileDir, filePath);
if (_fs2.default.existsSync(importPath + '.jsx')) {
importDeclarationPaths.push(importPath + '.jsx');
return;
}
if (_fs["default"].existsSync(importPath)) {
importDeclarationPaths.push(_path["default"].resolve(importPath, defaultFileName));
return;
}
if (_fs2.default.existsSync(importPath + '.js')) {
importDeclarationPaths.push(importPath + '.js');
}
};
if (_fs["default"].existsSync("".concat(importPath, ".jsx"))) {
importDeclarationPaths.push("".concat(importPath, ".jsx"));
return;
}
return {
Program: {
exit: function exit() {
cb(staticProps, importDeclarationPaths);
}
},
if (_fs["default"].existsSync("".concat(importPath, ".js"))) {
importDeclarationPaths.push("".concat(importPath, ".js"));
}
};
ClassProperty: {
enter: function enter(path) {
var node = path.node;
return {
Program: {
exit: function exit() {
cb(staticProps, importDeclarationPaths);
}
},
ClassProperty: {
enter: function enter(path) {
var node = path.node;
if (!node["static"]) {
return;
}
if (!node.static) {
return;
}
if (_core.types.isIdentifier(node.key) && node.key.name === staticPropName && _core.types.isObjectExpression(node.value)) {
staticProps = getConcatenatedStaticProps(staticProps, node.value.properties);
}
}
},
ExpressionStatement: {
enter: function enter(path) {
path.traverse({
AssignmentExpression: {
enter: function enter(_ref2) {
var node = _ref2.node;
if (_babelCore.types.isIdentifier(node.key) && node.key.name === staticPropName && _babelCore.types.isObjectExpression(node.value)) {
staticProps = getConcatenatedStaticProps(staticProps, node.value.properties);
}
}
},
if (!_core.types.isMemberExpression(node.left)) {
return;
}
ExpressionStatement: {
enter: function enter(path) {
path.traverse({
AssignmentExpression: {
enter: function enter(_ref2) {
var node = _ref2.node;
var propIsMemberExpression = _core.types.isMemberExpression(node.left.object) && node.left.object.property.name === staticPropName && _core.types.isStringLiteral(node.right);
if (!_babelCore.types.isMemberExpression(node.left)) {
return;
}
var propsIsObjectExpression = _core.types.isIdentifier(node.left.object) && node.left.property.name === staticPropName && _core.types.isObjectExpression(node.right);
var propIsMemberExpression = _babelCore.types.isMemberExpression(node.left.object) && node.left.object.property.name === staticPropName && _babelCore.types.isStringLiteral(node.right);
var propsIsObjectExpression = _babelCore.types.isIdentifier(node.left.object) && node.left.property.name === staticPropName && _babelCore.types.isObjectExpression(node.right);
if (propIsMemberExpression) {
staticProps.push(node.right.value);
} else if (propsIsObjectExpression) {
staticProps = getConcatenatedStaticProps(staticProps, node.right.properties);
}
}
}
});
if (propIsMemberExpression) {
staticProps.push(node.right.value);
} else if (propsIsObjectExpression) {
staticProps = getConcatenatedStaticProps(staticProps, node.right.properties);
}
}
},
}
});
}
},
ImportDeclaration: {
enter: function enter(_ref3) {
var node = _ref3.node;
ImportDeclaration: {
enter: function enter(_ref3) {
var node = _ref3.node;
if (node && node.source && isJsFile(node.source.value)) {
processImports(replacePath(node.source.value, pathsToReplace));
}
}
},
CallExpression: {
enter: function enter(_ref4) {
var node = _ref4.node;
if (node && node.source && isJsFile(node.source.value)) {
processImports(replacePath(node.source.value, pathsToReplace));
}
}
},
if (node && node.callee && _core.types.isImport(node.callee)) {
processImports(node.arguments[0].value);
}
}
}
};
};
CallExpression: {
enter: function enter(_ref4) {
var node = _ref4.node;
if (node && node.callee && _babelCore.types.isImport(node.callee)) {
processImports(node.arguments[0].value);
}
}
}
};
};
exports["default"] = _default;
{
"name": "@hh.ru/babel-plugin-static-value-extractor",
"version": "0.5.1",
"version": "0.5.2",
"main": "lib/index.js",
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-preset-env": "^1.7.0",
"babel-preset-stage-0": "^6.24.1",
"@babel/cli": "^7.5.0",
"@babel/plugin-proposal-function-bind": "^7.2.0",
"@babel/preset-env": "^7.5.4",
"@babel/register": "^7.5.5",
"del": "^3.0.0",

@@ -23,3 +24,3 @@ "mocha": "5.2.0"

"build": "babel src -d lib",
"test": "mocha --compilers js:babel-register",
"test": "mocha --compilers js:@babel/register",
"test:watch": "yarn run test -- --watch",

@@ -32,5 +33,5 @@ "prepublish": "yarn clean && yarn build"

"dependencies": {
"babel-core": "^6.26.3",
"@babel/core": "^7.5.4",
"glob-all": "^3.1.0"
}
}

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