@putout/operator-regexp
Version:
🐊Putout operator adds ability to check if provided regexp can be converted to string without losing it's sense
23 lines (16 loc) • 582 B
JavaScript
;
const regexpTree = require('regexp-tree');
const notSimpleChar = ({type, kind}) => type !== 'Char' || kind !== 'simple';
module.exports = (regexp) => {
let containsMoreThenSimpleChars = false;
const ast = regexpTree.parse(regexp);
if (ast.body.type !== 'Alternative')
return false;
regexpTree.traverse(ast, {
RegExp({node}) {
const {expressions} = node.body;
containsMoreThenSimpleChars = Boolean(expressions.find(notSimpleChar));
},
});
return !containsMoreThenSimpleChars;
};