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

View File

@ -0,0 +1,69 @@
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
'use strict';
/**
* A regexp-tree plugin to translate `/./s` to `/[\0-\uFFFF]/`.
*/
module.exports = {
// Whether `u` flag present. In which case we transform to
// \u{10FFFF} instead of \uFFFF.
_hasUFlag: false,
// Only run this plugin if we have `s` flag.
shouldRun: function shouldRun(ast) {
var shouldRun = ast.flags.includes('s');
if (!shouldRun) {
return false;
}
// Strip the `s` flag.
ast.flags = ast.flags.replace('s', '');
// Whether we have also `u`.
this._hasUFlag = ast.flags.includes('u');
return true;
},
Char: function Char(path) {
var node = path.node;
if (node.kind !== 'meta' || node.value !== '.') {
return;
}
var toValue = '\\uFFFF';
var toSymbol = '\uFFFF';
if (this._hasUFlag) {
toValue = '\\u{10FFFF}';
toSymbol = '\uDBFF\uDFFF';
}
path.replace({
type: 'CharacterClass',
expressions: [{
type: 'ClassRange',
from: {
type: 'Char',
value: '\\0',
kind: 'decimal',
symbol: '\0'
},
to: {
type: 'Char',
value: toValue,
kind: 'unicode',
symbol: toSymbol
}
}]
});
}
};

View File

@ -0,0 +1,59 @@
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
'use strict';
/**
* A regexp-tree plugin to translate `/(?<name>a)\k<name>/` to `/(a)\1/`.
*/
module.exports = {
// To track the names of the groups, and return them
// in the transform result state.
//
// A map from name to number: {foo: 2, bar: 4}
_groupNames: {},
/**
* Initialises the trasnform.
*/
init: function init() {
this._groupNames = {};
},
/**
* Returns extra state, which eventually is returned to
*/
getExtra: function getExtra() {
return this._groupNames;
},
Group: function Group(path) {
var node = path.node;
if (!node.name) {
return;
}
// Record group name.
this._groupNames[node.name] = node.number;
delete node.name;
delete node.nameRaw;
},
Backreference: function Backreference(path) {
var node = path.node;
if (node.kind !== 'name') {
return;
}
node.kind = 'number';
node.reference = node.number;
delete node.referenceRaw;
}
};

View File

@ -0,0 +1,23 @@
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
'use strict';
/**
* A regexp-tree plugin to remove `x` flag `/foo/x` to `/foo/`.
*
* Note: other features of `x` flags (whitespace, comments) are
* already removed at parsing stage.
*/
module.exports = {
RegExp: function RegExp(_ref) {
var node = _ref.node;
if (node.flags.includes('x')) {
node.flags = node.flags.replace('x', '');
}
}
};

View File

@ -0,0 +1,17 @@
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
'use strict';
module.exports = {
// "dotAll" `s` flag
dotAll: require('./compat-dotall-s-transform'),
// Named capturing groups.
namedCapturingGroups: require('./compat-named-capturing-groups-transform'),
// `x` flag
xFlag: require('./compat-x-flag-transform')
};