How to use the regexp-tree.optimize function in regexp-tree

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 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)
			});
		},