@atlaskit/eslint-plugin-no-lookahead-lookbehind-regexp
Version:
Fork of https://github.com/JonasBa/eslint-plugin-no-lookahead-lookbehind-regexp
117 lines (115 loc) • 4.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.noLookaheadLookbehindRegexp = exports.getExpressionsToCheckFromConfiguration = exports.DEFAULT_OPTIONS = exports.DEFAULT_CONF = void 0;
var _analyzeRegExpForLookaheadAndLookbehind = require("../helpers/analyzeRegExpForLookaheadAndLookbehind");
var _ast = require("../helpers/ast");
var _caniuse = require("../helpers/caniuse");
var _createReport = require("../helpers/createReport");
const DEFAULT_OPTIONS = exports.DEFAULT_OPTIONS = {
'no-lookahead': 1,
'no-lookbehind': 1,
'no-negative-lookahead': 1,
'no-negative-lookbehind': 1
};
const DEFAULT_CONF = exports.DEFAULT_CONF = {
browserslist: false
};
function isPlainObject(obj) {
return Object.prototype.toString.call(obj) === '[object Object]';
}
const getExpressionsToCheckFromConfiguration = options => {
if (!options.length) {
return {
rules: DEFAULT_OPTIONS,
config: DEFAULT_CONF
};
}
let rules = options;
let config = {};
if (isPlainObject(options[options.length - 1])) {
rules = options.slice(0, -1);
config = options[options.length - 1];
}
const validOptions = rules.filter(option => {
if (typeof option !== 'string') {
return false;
}
return DEFAULT_OPTIONS[option];
});
if (!validOptions.length) {
return {
rules: DEFAULT_OPTIONS,
config
};
}
const expressions = validOptions.reduce((acc, opt) => {
acc[opt] = 1;
return acc;
}, {
'no-lookahead': 0,
'no-lookbehind': 0,
'no-negative-lookahead': 0,
'no-negative-lookbehind': 0
});
return {
rules: expressions,
config
};
};
exports.getExpressionsToCheckFromConfiguration = getExpressionsToCheckFromConfiguration;
const noLookaheadLookbehindRegexp = exports.noLookaheadLookbehindRegexp = {
meta: {
docs: {
description: 'disallow the use of lookahead and lookbehind regular expressions if unsupported by browser',
category: 'Compatibility',
recommended: true
},
type: 'problem'
},
create(context) {
const browsers = context.settings.browsers || context.settings.targets;
const {
targets,
hasConfig
} = (0, _caniuse.collectBrowserTargets)(context.getFilename(), browsers);
// Lookahead assertions are part of JavaScript's original regular expression support and are thus supported in all browsers.
const unsupportedTargets = (0, _caniuse.collectUnsupportedTargets)('js-regexp-lookbehind', targets);
const {
rules,
config
} = getExpressionsToCheckFromConfiguration(context.options);
// If there are no unsupported targets resolved from the browserlist config, then we can skip this rule
if (!unsupportedTargets.length && hasConfig) {
return {};
}
return {
TemplateLiteral(node) {
for (let quasi of node.quasis) {
if (typeof quasi.value.raw === 'string') {
const unsupportedGroups = (0, _analyzeRegExpForLookaheadAndLookbehind.analyzeRegExpForLookaheadAndLookbehind)(quasi.value.raw, rules // For string literals, we need to pass the raw value which includes escape characters.
);
if (unsupportedGroups.length > 0) {
(0, _createReport.createContextReport)(node, context, unsupportedGroups, unsupportedTargets, config);
}
}
}
},
Literal(node) {
if (((0, _ast.isStringLiteralRegExp)(node) || (0, _ast.isBinaryExpression)(node)) && typeof node.raw === 'string') {
const unsupportedGroups = (0, _analyzeRegExpForLookaheadAndLookbehind.analyzeRegExpForLookaheadAndLookbehind)(node.raw, rules // For string literals, we need to pass the raw value which includes escape characters.
);
if (unsupportedGroups.length > 0) {
(0, _createReport.createContextReport)(node, context, unsupportedGroups, unsupportedTargets, config);
}
} else if ((0, _ast.isRegExpLiteral)(node)) {
const unsupportedGroups = (0, _analyzeRegExpForLookaheadAndLookbehind.analyzeRegExpForLookaheadAndLookbehind)(node.regex.pattern, rules);
if (unsupportedGroups.length > 0) {
(0, _createReport.createContextReport)(node, context, unsupportedGroups, unsupportedTargets, config);
}
}
}
};
}
};