Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function pryRegExASTFromInput(pRegEx) {
let lRegExp = null;
if (pRegEx instanceof RegExp) {
lRegExp = pRegEx;
} else if (typeof pRegEx === "string") {
lRegExp = new RegExp(pRegEx);
} else {
lRegExp = new RegExp(String(pRegEx));
}
// Build an AST
return regexpTree.parse(lRegExp);
}
import compileModule from '../../../utils/compileModule';
import pkg from 'regexp-tree/package.json';
const ID = 'regexp-tree';
export default {
id: ID,
displayName: ID,
version: pkg.version,
homepage: pkg.homepage,
defaultParserID: ID,
loadTransformer(callback) {
require([
'../../../transpilers/babel',
'regexp-tree',
], (transpile, regexpTree) => callback({ transpile: transpile.default, regexpTree }));
},
transform({ transpile, regexpTree }, transformCode, code) {
transformCode = transpile(transformCode);
let handler = compileModule( // eslint-disable-line no-shadow
transformCode,
);
import compileModule from '../../../utils/compileModule';
import pkg from 'regexp-tree/package.json';
const ID = 'regexp-tree';
export default {
id: ID,
displayName: ID,
version: pkg.version,
homepage: pkg.homepage,
defaultParserID: ID,
loadTransformer(callback) {
require([
'../../../transpilers/babel',
'regexp-tree',
], (transpile, regexpTree) => callback({ transpile: transpile.default, regexpTree }));
},
transform({ transpile, regexpTree }, transformCode, code) {
transformCode = transpile(transformCode);
let handler = compileModule( // eslint-disable-line no-shadow
transformCode,
);
'Literal[regex]': node => {
const {raw: original, regex} = node;
// Regular Expressions with `u` flag are not well handled by `regexp-tree`
// https://github.com/DmitrySoshnikov/regexp-tree/issues/162
if (regex.flags.includes('u')) {
return;
}
let optimized = original;
try {
optimized = optimize(original).toString();
} catch (_) {}
if (original === optimized) {
return;
}
context.report({
node,
message: '{{original}} can be optimized to {{optimized}}',
data: {
original,
optimized
},
fix: fixer => fixer.replaceText(node, optimized)
});
},
function getTranslatedData(regexp, state) {
let whitelist = undefined;
if (Array.isArray(state.opts.features) && state.opts.features.length > 0) {
whitelist = state.opts.features;
}
const compat = regexpTree.compatTranspile(regexp, whitelist);
return {
pattern: compat.getSource(),
flags: compat.getFlags(),
};
}
function isProbablySafe(pRegExAST, pOptions) {
const lRepLimit = pOptions.limit || DEFAULT_REPETITION_LIMIT;
let currentStarHeight = 0;
let maxObservedStarHeight = 0;
let repetitionCount = 0;
regexpTree.traverse(pRegExAST, {
Repetition: {
pre() {
repetitionCount += 1;
currentStarHeight += 1;
if (maxObservedStarHeight < currentStarHeight) {
maxObservedStarHeight = currentStarHeight;
}
},
post() {
currentStarHeight -= 1;
}
}
});
import defaultParserInterface from '../utils/defaultParserInterface';
import pkg from 'regexp-tree/package.json';
const ID = 'regexp-tree';
export default {
...defaultParserInterface,
id: ID,
displayName: ID,
version: pkg.version,
homepage: pkg.homepage,
locationProps: new Set(['loc']),
loadParser(callback) {
require(['regexp-tree'], (regexpTree) => {
callback(regexpTree);
});
},
parse(regexpTree, code, options={}) {
regexpTree
.parser
.setOptions(options);
return regexpTree.parse(code);
},
import defaultParserInterface from '../utils/defaultParserInterface';
import pkg from 'regexp-tree/package.json';
const ID = 'regexp-tree';
export default {
...defaultParserInterface,
id: ID,
displayName: ID,
version: pkg.version,
homepage: pkg.homepage,
locationProps: new Set(['loc']),
loadParser(callback) {
require(['regexp-tree'], (regexpTree) => {
callback(regexpTree);
});
},
parse(regexpTree, code, options={}) {
regexpTree
.parser
.setOptions(options);
return regexpTree.parse(code);
},