Skip to content

Commit ed2c8a5

Browse files
authored
ESLint compatibility workaround (#6449)
1 parent 22139ca commit ed2c8a5

File tree

1 file changed

+34
-11
lines changed

1 file changed

+34
-11
lines changed

src/util/generate-pnp-map-api.tpl.js

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ const StringDecoder = require('string_decoder');
1414
const ignorePattern = $$BLACKLIST ? new RegExp($$BLACKLIST) : null;
1515

1616
const builtinModules = new Set(Module.builtinModules || Object.keys(process.binding('natives')));
17+
18+
const topLevelLocator = {name: null, reference: null};
19+
const blacklistedLocator = {name: NaN, reference: NaN};
20+
21+
// Used for compatibility purposes - cf setupCompatibilityLayer
1722
const patchedModules = new Map();
23+
const fallbackLocators = [topLevelLocator];
1824

1925
// Splits a require request into its components, or return null if the request is a file path
2026
const pathRegExp = /^(?!\.{0,2}(?:\/|$))((?:@[^\/]+\/)?[^\/]+)\/?(.*|)$/;
@@ -26,9 +32,7 @@ const isStrictRegExp = /^\.{0,2}\//;
2632
// Matches if the path must point to a directory (ie ends with /)
2733
const isDirRegExp = /\/$/;
2834

29-
const topLevelLocator = {name: null, reference: null};
30-
const blacklistedLocator = {name: NaN, reference: NaN};
31-
35+
// Keep a reference around ("module" is a common name in this context, so better rename it to something more significant)
3236
const pnpModule = module;
3337

3438
/**
@@ -374,13 +378,15 @@ exports.resolveToUnqualified = function resolveToUnqualified(request, issuer, {c
374378

375379
let dependencyReference = issuerInformation.packageDependencies.get(dependencyName);
376380

377-
// If we can't find it, we check if we can potentially load it from the top-level packages
378-
// it's a bit of a hack, but it improves compatibility with the existing Node ecosystem. Hopefully we should
379-
// eventually be able to kill it and become stricter once pnp gets enough traction
381+
// If we can't find it, we check if we can potentially load it from the packages that have been defined as potential fallbacks.
382+
// It's a bit of a hack, but it improves compatibility with the existing Node ecosystem. Hopefully we should eventually be able
383+
// to kill this logic and become stricter once pnp gets enough traction and the affected packages fix themselves.
380384

381-
if (dependencyReference === undefined) {
382-
const topLevelInformation = getPackageInformationSafe(topLevelLocator);
383-
dependencyReference = topLevelInformation.packageDependencies.get(dependencyName);
385+
if (issuerLocator !== topLevelLocator) {
386+
for (let t = 0, T = fallbackLocators.length; dependencyReference === undefined && t < T; ++t) {
387+
const fallbackInformation = getPackageInformationSafe(fallbackLocators[t]);
388+
dependencyReference = fallbackInformation.packageDependencies.get(dependencyName);
389+
}
384390
}
385391

386392
// If we can't find the path, and if the package making the request is the top-level, we can offer nicer error messages
@@ -673,12 +679,26 @@ exports.setupCompatibilityLayer = () => {
673679
return stack[2].getFileName();
674680
};
675681

682+
// ESLint currently doesn't have any portable way for shared configs to specify their own
683+
// plugins that should be used (https://github.com/eslint/eslint/issues/10125). This will
684+
// likely get fixed at some point, but it'll take time and in the meantime we'll just add
685+
// additional fallback entries for common shared configs.
686+
687+
for (const name of [`react-scripts`]) {
688+
const packageInformationStore = packageInformationStores.get(name);
689+
if (packageInformationStore) {
690+
for (const reference of packageInformationStore.keys()) {
691+
fallbackLocators.push({name, reference});
692+
}
693+
}
694+
}
695+
676696
// We need to shim the "resolve" module, because Liftoff uses it in order to find the location
677697
// of the module in the dependency tree. And Liftoff is used to power Gulp, which doesn't work
678698
// at all unless modulePath is set, which we cannot configure from any other way than through
679699
// the Liftoff pipeline (the key isn't whitelisted for env or cli options).
680700

681-
patchedModules.set('resolve', realResolve => {
701+
patchedModules.set(/^resolve$/, realResolve => {
682702
const mustBeShimmed = caller => {
683703
const callerLocator = exports.findPackageLocator(caller);
684704

@@ -754,11 +774,14 @@ exports.setupCompatibilityLayer = () => {
754774
};
755775

756776
if (module.parent && module.parent.id === 'internal/preload') {
757-
exports.setup();
758777
exports.setupCompatibilityLayer();
778+
779+
exports.setup();
759780
}
760781

761782
if (process.mainModule === module) {
783+
exports.setupCompatibilityLayer();
784+
762785
const reportError = (code, message, data) => {
763786
process.stdout.write(`${JSON.stringify([{code, message, data}, null])}\n`);
764787
};

0 commit comments

Comments
 (0)