Skip to content

Commit

Permalink
pass node engine directly to targets
Browse files Browse the repository at this point in the history
  • Loading branch information
Mesteery committed Feb 5, 2023
1 parent f2ef747 commit 430cf20
Showing 1 changed file with 5 additions and 22 deletions.
27 changes: 5 additions & 22 deletions rules/no-unnecessary-polyfills.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function getTargetsFromPackage(cwd, useBrowserlistFieldByDefault) {
}

const {browserlist} = result.pkg;
const nodeEngine = result.pkg.engines && result.pkg.engines.node && new SemverNodeVersion(result.pkg.engines.node);
const nodeEngine = result.pkg.engines && result.pkg.engines.node && {node:result.pkg.engines.node};
return useBrowserlistFieldByDefault ? browserlist || nodeEngine : nodeEngine || browserlist;
}

Expand Down Expand Up @@ -65,34 +65,17 @@ const polyfills = Object.keys(compatData).map(feature => {
};
});

class SemverNodeVersion {
constructor(nodeVersion) {
this.nodeVersion = nodeVersion;
this.validNodeVersion = semver.coerce(nodeVersion);
}

compare(featureVersion) {
const supportedNodeVersion = semver.coerce(featureVersion);
return this.validNodeVersion
? semver.lte(supportedNodeVersion, this.validNodeVersion)
: semver.ltr(supportedNodeVersion, this.nodeVersion);
}
}

function processRule(context, node, moduleName, targets) {
if (!moduleName || typeof moduleName !== 'string' || ['.', '/'].includes(moduleName[0])) {
return;
}

const nodeVersion = targets instanceof SemverNodeVersion && targets;
const importedModule = moduleName.replace(/([/\\].+?)\.[^.]+$/, '$1');

const unavailableFeatures = coreJsCompat({targets}).list;
const coreJsModuleFeatures = coreJsEntries[importedModule.replace('core-js-pure', 'core-js')];

const checkFeatures = features => nodeVersion
? features.every(feature => compatData[feature].node && nodeVersion.compare(compatData[feature].node))
: !features.every(feature => unavailableFeatures.includes(feature));
const checkFeatures = features => !features.every(feature => unavailableFeatures.includes(feature));

if (coreJsModuleFeatures) {
if (coreJsModuleFeatures.length > 1) {
Expand All @@ -105,8 +88,8 @@ function processRule(context, node, moduleName, targets) {
},
});
}
} else if (nodeVersion ? nodeVersion.compare(compatData[coreJsModuleFeatures[0]].node) : !unavailableFeatures.includes(coreJsModuleFeatures[0])) {
context.report({ node, messageId: MESSAGE_ID_POLYFILL });
} else if (!unavailableFeatures.includes(coreJsModuleFeatures[0])) {
context.report({node, messageId: MESSAGE_ID_POLYFILL});
}

return;
Expand All @@ -117,7 +100,7 @@ function processRule(context, node, moduleName, targets) {
const [, namespace, method = ''] = polyfill.feature.split('.');
const [, features] = Object.entries(coreJsEntries).find(it => it[0] === `core-js/actual/${namespace}${method && '/'}${method}`);
if (checkFeatures(features)) {
context.report({ node, messageId: MESSAGE_ID_POLYFILL });
context.report({node, messageId: MESSAGE_ID_POLYFILL});
}
}
}
Expand Down

0 comments on commit 430cf20

Please sign in to comment.