Skip to content

Commit

Permalink
add pollyfils option
Browse files Browse the repository at this point in the history
  • Loading branch information
Przemysław Sobstel authored and Przemysław Sobstel committed Nov 10, 2022
1 parent 502f669 commit 4f4022d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
16 changes: 16 additions & 0 deletions packages/eslint-plugin-ecmascript-compat/lib/delegation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
function createDelegatee(config, rootContext) {
const { definition, options } = config;
const pollyfils = (rootContext.options && rootContext.options[0] && rootContext.options[0].polyfills) || [];

// All methods except report() are added later. Define necessary ones for used rules.
const contextLaterAddedMethods = {
Expand All @@ -15,11 +16,26 @@ function createDelegatee(config, rootContext) {
report,
};

function matchesPollyfil(data, pollyfil) {
if (data.message) {
return data.message.includes(`'${pollyfil}'`)
}
if (data.name) {
return data.name === pollyfil;
}
return false;
}

function report(params) {
// Discard fixer; we can't declare the rule as fixable because not all delegatees are.
// Look up messageId on delegate meta; report() would look it up on root rule meta.
const { fix, message, messageId, ...otherParams } = params;

// Skip report if known polyfill exists
if (params.data && pollyfils.some(pollyfil => matchesPollyfil(params.data, pollyfil))) {
return;
}

rootContext.report({
...otherParams,
message: messageId ? definition.meta.messages[messageId] : message,
Expand Down
15 changes: 14 additions & 1 deletion packages/eslint-plugin-ecmascript-compat/lib/rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,20 @@ const delegateeConfigs = compatibility
module.exports = {
meta: {
type: 'problem',
schema: [], // no options
schema: [
{
"type": "object",
"properties": {
"pollyfils": {
"type": "array",
"items": {
"type": "string"
}
}
},
"additionalProperties": "false",
}
]
},
create(context) {
const visitors = delegateeConfigs.map((config) => createDelegatee(config, context));
Expand Down

0 comments on commit 4f4022d

Please sign in to comment.