How to use regexp-tree - 8 common examples

To help you get started, we’ve selected a few regexp-tree examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sverweij / dependency-cruiser / src / utl / safe-regex.js View on Github external
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);
}
github fkling / astexplorer / website / src / parsers / regexp / transformers / regexp-tree / index.js View on Github external
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,
    );
github fkling / astexplorer / website / src / parsers / regexp / transformers / regexp-tree / index.js View on Github external
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,
    );
github sindresorhus / eslint-plugin-unicorn / rules / regex-shorthand.js View on Github external
'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)
			});
		},
github DmitrySoshnikov / babel-plugin-transform-modern-regexp / index.js View on Github external
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(),
  };
}
github sverweij / dependency-cruiser / src / utl / safe-regex.js View on Github external
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;
      }
    }
  });
github fkling / astexplorer / website / src / parsers / regexp / regexp-tree.js View on Github external
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);
  },
github fkling / astexplorer / website / src / parsers / regexp / regexp-tree.js View on Github external
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);
  },