@putout/plugin-regexp
Version:
🐊Putout plugin helps with regexp
30 lines (21 loc) • 658 B
JavaScript
import {types} from 'putout';
const {isRegExpLiteral} = types;
export const report = () => `Use '.startsWith()' instead of '.test()'`;
export const match = () => ({
'__a.test(__b)': ({__a}) => {
if (!isRegExpLiteral(__a))
return false;
const raw = __a.raw.slice(1, -1);
if (!raw.startsWith('^'))
return false;
return !/[$+({*\].]/.test(raw);
},
});
export const replace = () => ({
'__a.test(__b)': ({__a}) => {
const str = __a.raw
.slice(1, -1)
.replace('^', '');
return `__b.startsWith('${str}')`;
},
});