Improve code quality and write tests

This commit is contained in:
Shivam Mathur
2019-09-20 08:11:20 +05:30
parent db44db4b97
commit 43178a7254
3597 changed files with 255478 additions and 785554 deletions

138
node_modules/regexp-tree/dist/transform/index.js generated vendored Normal file
View File

@ -0,0 +1,138 @@
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
'use strict';
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var generator = require('../generator');
var parser = require('../parser');
var traverse = require('../traverse');
/**
* Transform result.
*/
var TransformResult = function () {
/**
* Initializes a transform result for an AST.
*
* @param Object ast - an AST node
* @param mixed extra - any extra data a transform may return
*/
function TransformResult(ast) {
var extra = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
_classCallCheck(this, TransformResult);
this._ast = ast;
this._source = null;
this._string = null;
this._regexp = null;
this._extra = extra;
}
_createClass(TransformResult, [{
key: 'getAST',
value: function getAST() {
return this._ast;
}
}, {
key: 'setExtra',
value: function setExtra(extra) {
this._extra = extra;
}
}, {
key: 'getExtra',
value: function getExtra() {
return this._extra;
}
}, {
key: 'toRegExp',
value: function toRegExp() {
if (!this._regexp) {
this._regexp = new RegExp(this.getSource(), this._ast.flags);
}
return this._regexp;
}
}, {
key: 'getSource',
value: function getSource() {
if (!this._source) {
this._source = generator.generate(this._ast.body);
}
return this._source;
}
}, {
key: 'getFlags',
value: function getFlags() {
return this._ast.flags;
}
}, {
key: 'toString',
value: function toString() {
if (!this._string) {
this._string = generator.generate(this._ast);
}
return this._string;
}
}]);
return TransformResult;
}();
module.exports = {
/**
* Expose `TransformResult`.
*/
TransformResult: TransformResult,
/**
* Transforms a regular expression applying a set of
* transformation handlers.
*
* @param string | AST | RegExp:
*
* a regular expression in different representations: a string,
* a RegExp object, or an AST.
*
* @param Object | Array<Object>:
*
* a handler (or a list of handlers) from `traverse` API.
*
* @return TransformResult instance.
*
* Example:
*
* transform(/[a-z]/i, {
* onChar(path) {
* const {node} = path;
*
* if (...) {
* path.remove();
* }
* }
* });
*/
transform: function transform(regexp, handlers) {
var ast = regexp;
if (regexp instanceof RegExp) {
regexp = '' + regexp;
}
if (typeof regexp === 'string') {
ast = parser.parse(regexp, {
captureLocations: true
});
}
traverse.traverse(ast, handlers);
return new TransformResult(ast);
}
};

88
node_modules/regexp-tree/dist/transform/utils.js generated vendored Normal file
View File

@ -0,0 +1,88 @@
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
'use strict';
/**
* Flattens a nested disjunction node to a list.
*
* /a|b|c|d/
*
* {{{a, b}, c}, d} -> [a, b, c, d]
*/
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); } }
function disjunctionToList(node) {
if (node.type !== 'Disjunction') {
throw new TypeError('Expected "Disjunction" node, got "' + node.type + '"');
}
var list = [];
if (node.left && node.left.type === 'Disjunction') {
list.push.apply(list, _toConsumableArray(disjunctionToList(node.left)).concat([node.right]));
} else {
list.push(node.left, node.right);
}
return list;
}
/**
* Builds a nested disjunction node from a list.
*
* /a|b|c|d/
*
* [a, b, c, d] -> {{{a, b}, c}, d}
*/
function listToDisjunction(list) {
return list.reduce(function (left, right) {
return {
type: 'Disjunction',
left: left,
right: right
};
});
}
/**
* Increases a quantifier by one.
* Does not change greediness.
* * -> +
* + -> {2,}
* ? -> {1,2}
* {2} -> {3}
* {2,} -> {3,}
* {2,3} -> {3,4}
*/
function increaseQuantifierByOne(quantifier) {
if (quantifier.kind === '*') {
quantifier.kind = '+';
} else if (quantifier.kind === '+') {
quantifier.kind = 'Range';
quantifier.from = 2;
delete quantifier.to;
} else if (quantifier.kind === '?') {
quantifier.kind = 'Range';
quantifier.from = 1;
quantifier.to = 2;
} else if (quantifier.kind === 'Range') {
quantifier.from += 1;
if (quantifier.to) {
quantifier.to += 1;
}
}
}
module.exports = {
disjunctionToList: disjunctionToList,
listToDisjunction: listToDisjunction,
increaseQuantifierByOne: increaseQuantifierByOne
};