@atlaskit/eslint-plugin-no-lookahead-lookbehind-regexp
Version:
Fork of https://github.com/JonasBa/eslint-plugin-no-lookahead-lookbehind-regexp
137 lines (136 loc) • 6.5 kB
JavaScript
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
import { analyzeRegExpForLookaheadAndLookbehind } from '../helpers/analyzeRegExpForLookaheadAndLookbehind';
import { isBinaryExpression, isRegExpLiteral, isStringLiteralRegExp } from '../helpers/ast';
import { collectBrowserTargets, collectUnsupportedTargets } from '../helpers/caniuse';
import { createContextReport } from '../helpers/createReport';
export var DEFAULT_OPTIONS = {
'no-lookahead': 1,
'no-lookbehind': 1,
'no-negative-lookahead': 1,
'no-negative-lookbehind': 1
};
export var DEFAULT_CONF = {
browserslist: false
};
function isPlainObject(obj) {
return Object.prototype.toString.call(obj) === '[object Object]';
}
export var getExpressionsToCheckFromConfiguration = function getExpressionsToCheckFromConfiguration(options) {
if (!options.length) {
return {
rules: DEFAULT_OPTIONS,
config: DEFAULT_CONF
};
}
var rules = options;
var config = {};
if (isPlainObject(options[options.length - 1])) {
rules = options.slice(0, -1);
config = options[options.length - 1];
}
var validOptions = rules.filter(function (option) {
if (typeof option !== 'string') {
return false;
}
return DEFAULT_OPTIONS[option];
});
if (!validOptions.length) {
return {
rules: DEFAULT_OPTIONS,
config: config
};
}
var expressions = validOptions.reduce(function (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: config
};
};
var noLookaheadLookbehindRegexp = {
meta: {
docs: {
description: 'disallow the use of lookahead and lookbehind regular expressions if unsupported by browser',
category: 'Compatibility',
recommended: true
},
type: 'problem'
},
create: function create(context) {
var browsers = context.settings.browsers || context.settings.targets;
var _collectBrowserTarget = collectBrowserTargets(context.getFilename(), browsers),
targets = _collectBrowserTarget.targets,
hasConfig = _collectBrowserTarget.hasConfig;
// Lookahead assertions are part of JavaScript's original regular expression support and are thus supported in all browsers.
var unsupportedTargets = collectUnsupportedTargets('js-regexp-lookbehind', targets);
var _getExpressionsToChec = getExpressionsToCheckFromConfiguration(context.options),
rules = _getExpressionsToChec.rules,
config = _getExpressionsToChec.config;
// If there are no unsupported targets resolved from the browserlist config, then we can skip this rule
if (!unsupportedTargets.length && hasConfig) {
return {};
}
return {
TemplateLiteral: function (_TemplateLiteral) {
function TemplateLiteral(_x) {
return _TemplateLiteral.apply(this, arguments);
}
TemplateLiteral.toString = function () {
return _TemplateLiteral.toString();
};
return TemplateLiteral;
}(function (node) {
var _iterator = _createForOfIteratorHelper(node.quasis),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var quasi = _step.value;
if (typeof quasi.value.raw === 'string') {
var unsupportedGroups = analyzeRegExpForLookaheadAndLookbehind(quasi.value.raw, rules // For string literals, we need to pass the raw value which includes escape characters.
);
if (unsupportedGroups.length > 0) {
createContextReport(node, context, unsupportedGroups, unsupportedTargets, config);
}
}
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
}),
Literal: function (_Literal) {
function Literal(_x2) {
return _Literal.apply(this, arguments);
}
Literal.toString = function () {
return _Literal.toString();
};
return Literal;
}(function (node) {
if ((isStringLiteralRegExp(node) || isBinaryExpression(node)) && typeof node.raw === 'string') {
var unsupportedGroups = analyzeRegExpForLookaheadAndLookbehind(node.raw, rules // For string literals, we need to pass the raw value which includes escape characters.
);
if (unsupportedGroups.length > 0) {
createContextReport(node, context, unsupportedGroups, unsupportedTargets, config);
}
} else if (isRegExpLiteral(node)) {
var _unsupportedGroups = analyzeRegExpForLookaheadAndLookbehind(node.regex.pattern, rules);
if (_unsupportedGroups.length > 0) {
createContextReport(node, context, _unsupportedGroups, unsupportedTargets, config);
}
}
})
};
}
};
export { noLookaheadLookbehindRegexp };