typhonjs-escomplex
Version:
Next generation complexity reporting for Javascript & Typescript based on the Babel parser.
434 lines (361 loc) • 17 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
var _promise = require('babel-runtime/core-js/promise');
var _promise2 = _interopRequireDefault(_promise);
var _typeof2 = require('babel-runtime/helpers/typeof');
var _typeof3 = _interopRequireDefault(_typeof2);
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
var _babelParser = require('@typhonjs/babel-parser');
var _babelParser2 = _interopRequireDefault(_babelParser);
var _ESComplexModule = require('typhonjs-escomplex-module/dist/ESComplexModule');
var _ESComplexModule2 = _interopRequireDefault(_ESComplexModule);
var _ESComplexProject = require('typhonjs-escomplex-project/dist/ESComplexProject');
var _ESComplexProject2 = _interopRequireDefault(_ESComplexProject);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Next generation code complexity reporting for Javascript abstract syntax trees (AST). ESComplex exposes all methods
* available via ESComplexModule & ESComplexProject modules which process AST. Several methods available below accept
* source code which is translated via Babel Parser w/ all plugins enabled allowing any ES6/ES7/edge JS and Typescript
* to be parsed. Asynchronous versions with the `Async` suffix of all methods are available as well.
*
* @see https://www.npmjs.com/package/typhonjs-escomplex-module
* @see https://www.npmjs.com/package/typhonjs-escomplex-project
* @see https://www.npmjs.com/package/babylon
*/
var ESComplex = function () {
/**
* Initializes ESComplex.
*
* @param {object} options - module and project options including user plugins to load including:
* ```
* (object) module - Provides an object hash of the following options for the module runtime:
* (boolean) loadDefaultPlugins - When false ESComplexModule will not load any default plugins.
* (Array<Object>) plugins - A list of ESComplexModule plugins that have already been instantiated.
*
* (object) project - Provides an object hash of the following options for the project runtime:
* (boolean) loadDefaultPlugins - When false ESComplexProject will not load any default plugins.
* (Array<Object>) plugins - A list of ESComplexProject plugins that have already been instantiated.
* ```
*/
function ESComplex() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
(0, _classCallCheck3.default)(this, ESComplex);
/* istanbul ignore if */
if ((typeof options === 'undefined' ? 'undefined' : (0, _typeof3.default)(options)) !== 'object') {
throw new TypeError('ctor error: \'options\' is not an \'object\'.');
}
/**
* An instance of ESComplexModule.
* @type {ESComplexModule}
* @private
*/
this._escomplexModule = new _ESComplexModule2.default(options.module);
/**
* An instance of ESComplexProject.
* @type {ESComplexProject}
* @private
*/
this._escomplexProject = new _ESComplexProject2.default(_path2.default, options);
}
/**
* Parses the given source code then processes the generated AST and calculates metrics via plugins.
*
* @param {string} source - Javascript source code.
*
* @param {object} [options] - Module analyze options.
*
* @param {object} [parserOptions] - Overrides default Babel parser options.
*
* @param {object} [parserOverride] - Provides helper directives to override options to simplify modification of
* default Babel parser options.
*
* @returns {object} - A single module report.
*/
(0, _createClass3.default)(ESComplex, [{
key: 'analyzeModule',
value: function analyzeModule(source) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var parserOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : void 0;
var parserOverride = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : void 0;
/* istanbul ignore if */
if (typeof source !== 'string') {
throw new TypeError('analyze error: \'source\' is not a \'string\'.');
}
return this._escomplexModule.analyze(_babelParser2.default.parse(source, parserOptions, parserOverride), options);
}
/**
* Processes the given ast and calculates metrics via plugins.
*
* @param {object|Array} ast - Javascript AST.
*
* @param {object} [options] - Module analyze options.
*
* @returns {object} - A single module report.
*/
}, {
key: 'analyzeModuleAST',
value: function analyzeModuleAST(ast) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return this._escomplexModule.analyze(ast, options);
}
/**
* Processes the given sources and calculates project metrics via plugins.
*
* @param {Array<object>} sources - Array of object hashes containing `code` and `srcPath` entries with optional
* entries include `filePath` and `srcPathAlias`.
*
* @param {object} [options] - Project processing options.
*
* @param {object} [parserOptions] - Overrides default Babel parser options.
*
* @param {object} [parserOverride] - Provides helper directives to override options to simplify modification
* of default Babel parser options.
*
* @returns {{reports: Array<{}>}} - An object hash with a `reports` entry that is an Array of module results.
*/
}, {
key: 'analyzeProject',
value: function analyzeProject(sources) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var parserOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : void 0;
var parserOverride = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : void 0;
// Parse sources and map entries to include `ast` entry from `code`.
var modules = sources.map(function (source) {
try {
return {
ast: _babelParser2.default.parse(source.code, parserOptions, parserOverride),
filePath: source.filePath,
srcPath: source.srcPath,
srcPathAlias: source.srcPathAlias
};
} catch (error) {
/* istanbul ignore if */
if (options.ignoreErrors) {
return null;
}
/* istanbul ignore next */
error.message = source.path + ': ' + error.message;
/* istanbul ignore next */
throw error;
}
}).filter(function (source) {
return !!source;
});
return this._escomplexProject.analyze(modules, options);
}
/**
* Processes the given modules and calculates project metrics via plugins.
*
* @param {Array<object>} modules - Array of object hashes containing `ast` and `srcPath` entries with optional
* entries include `filePath` and `srcPathAlias`.
*
* @param {object} options - (Optional) project processing options.
*
* @returns {{reports: Array<{}>}} - An object hash with a `reports` entry that is an Array of module results.
*/
}, {
key: 'analyzeProjectAST',
value: function analyzeProjectAST(modules) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return this._escomplexProject.analyze(modules, options);
}
/**
* Provides a convenience method to parse the given source code and return the Babel parser AST.
*
* @param {string} source - Javascript source code.
*
* @param {object} [parserOptions] - Overrides default babylon parser options.
*
* @param {object} [parserOverride] - Provides helper directives to override options to simplify modification
* of default Babel parser options.
*
* @returns {object} - babylon generated AST.
*/
}, {
key: 'parse',
value: function parse(source) {
var parserOptions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : void 0;
var parserOverride = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : void 0;
return _babelParser2.default.parse(source, parserOptions, parserOverride);
}
/**
* Processes existing project results and calculates metrics via plugins.
*
* @param {object} results - An object hash with a `reports` entry that is an Array of module results.
*
* @param {object} options - (Optional) project processing options.
*
* @returns {{reports: Array<{}>}} - An object hash with a `reports` entry that is an Array of module results.
*/
}, {
key: 'processProject',
value: function processProject(results) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return this._escomplexProject.process(results, options);
}
// Asynchronous Promise based methods ----------------------------------------------------------------------------
/**
* Wraps in a Promise parsing of the given source code then processes the generated AST and calculates metrics via
* plugins.
*
* @param {string} source - Javascript source code.
*
* @param {object} [options] - Module analyze options.
*
* @param {object} [parserOptions] - Overrides default babylon parser options.
*
* @param {object} [parserOverride] - Provides helper directives to override options to simplify modification
* of default Babel parser options.
*
* @returns {object} - A single module report.
*/
}, {
key: 'analyzeModuleAsync',
value: function analyzeModuleAsync(source) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var _this = this;
var parserOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : void 0;
var parserOverride = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : void 0;
return new _promise2.default(function (resolve, reject) {
try {
resolve(_this.analyzeModule(source, options, parserOptions, parserOverride));
} catch (err) {
/* istanbul ignore next */reject(err);
}
});
}
/**
* Wraps in a Promise processing the given ast and calculates metrics via plugins.
*
* @param {object|Array} ast - Javascript AST.
*
* @param {object} [options] - Module analyze options.
*
* @returns {Promise<object>} - A single module report.
*/
}, {
key: 'analyzeModuleASTAsync',
value: function analyzeModuleASTAsync(ast) {
var _this2 = this;
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return new _promise2.default(function (resolve, reject) {
try {
resolve(_this2.analyzeModuleAST(ast, options));
} catch (err) {
/* istanbul ignore next */reject(err);
}
});
}
/**
* Wraps in a Promise processing the given sources and calculates project metrics via plugins.
*
* @param {Array<object>} sources - Array of object hashes containing `code` and `path` entries.
*
* @param {object} [options] - Project processing options.
*
* @param {object} [parserOptions] - Overrides default babylon parser options.
*
* @param {object} [parserOverride] - Provides helper directives to override options to simplify modification
* of default Babel parser options.
*
* @returns {Promise<{reports: Array<{}>}>} - An object hash with a `reports` entry that is an Array of module
* results.
*/
}, {
key: 'analyzeProjectAsync',
value: function analyzeProjectAsync(sources) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var _this3 = this;
var parserOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : void 0;
var parserOverride = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : void 0;
return new _promise2.default(function (resolve, reject) {
try {
resolve(_this3.analyzeProject(sources, options, parserOptions, parserOverride));
} catch (err) {
/* istanbul ignore next */reject(err);
}
});
}
/**
* Wraps in a Promise processing the given modules and calculates project metrics via plugins.
*
* @param {Array} modules - Array of object hashes containing `ast` and `path` entries.
*
* @param {object} [options] - Project processing options.
*
* @returns {Promise<{reports: Array<{}>}>} - An object hash with a `reports` entry that is an Array of module
* results.
*/
}, {
key: 'analyzeProjectASTAsync',
value: function analyzeProjectASTAsync(modules) {
var _this4 = this;
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return new _promise2.default(function (resolve, reject) {
try {
resolve(_this4.analyzeProjectAST(modules, options));
} catch (err) {
/* istanbul ignore next */reject(err);
}
});
}
/**
* Wraps in a Promise a convenience method to parse the given source code and return the babylon AST.
*
* @param {string} source - Javascript source code.
*
* @param {object} [parserOptions] - Overrides default babylon parser options.
*
* @param {object} [parserOverride] - Provides helper directives to override options to simplify modification
* of default Babel parser options.
*
* @returns {Promise<object>} - babylon generated AST.
*/
}, {
key: 'parseAsync',
value: function parseAsync(source) {
var _this5 = this;
var parserOptions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : void 0;
var parserOverride = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : void 0;
return new _promise2.default(function (resolve, reject) {
try {
resolve(_this5.parse(source, parserOptions, parserOverride));
} catch (err) {
/* istanbul ignore next */reject(err);
}
});
}
/**
* Wraps in a Promise processing existing project results and calculates metrics via plugins.
*
* @param {object} results - An object hash with a `reports` entry that is an Array of module results.
* @param {object} options - (Optional) project processing options.
*
* @returns {Promise<{reports: Array<{}>}>} - An object hash with a `reports` entry that is an Array of module
* results.
*/
}, {
key: 'processProjectAsync',
value: function processProjectAsync(results) {
var _this6 = this;
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return new _promise2.default(function (resolve, reject) {
try {
resolve(_this6.processProject(results, options));
} catch (err) {
/* istanbul ignore next */reject(err);
}
});
}
}]);
return ESComplex;
}();
exports.default = ESComplex;
module.exports = exports['default'];
;