-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1542 from embroider-build/peer-deps-check
Refuse to accept v1 addons as invalid peerDeps
- Loading branch information
Showing
6 changed files
with
1,053 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import Package from './package'; | ||
|
||
// For each package in the graph, discover all the paths to reach that package. | ||
// That is, we're identifying all its consumers. | ||
export function crawlDeps(startingPackage: Package): Map<Package, Package[][]> { | ||
let queue: { pkg: Package; path: Package[] }[] = [{ pkg: startingPackage, path: [] }]; | ||
let seen = new Set<Package>(); | ||
let results = new Map<Package, Package[][]>(); | ||
for (;;) { | ||
let entry = queue.shift(); | ||
if (!entry) { | ||
break; | ||
} | ||
let { pkg, path } = entry; | ||
|
||
let paths = results.get(pkg); | ||
if (paths) { | ||
paths.push(path); | ||
} else { | ||
results.set(pkg, [path]); | ||
} | ||
|
||
if (!seen.has(pkg)) { | ||
seen.add(pkg); | ||
for (let dep of pkg.dependencies) { | ||
if (pkg.categorizeDependency(dep.name) !== 'peerDependencies') { | ||
queue.push({ pkg: dep, path: [...path, pkg] }); | ||
} | ||
} | ||
} | ||
} | ||
return results; | ||
} | ||
|
||
interface PeerDepViolation { | ||
// this package... | ||
pkg: Package; | ||
// sees this peer dep. | ||
dep: Package; | ||
// pkg was reached by this path of ancestors | ||
ancestors: Package[]; | ||
// this particular ancestor... | ||
ancestor: Package; | ||
// provides a conflicting value for the peerDep | ||
ancestorsDep: Package; | ||
} | ||
|
||
export function validatePeerDependencies(appPackage: Package): PeerDepViolation[] { | ||
let violations = []; | ||
for (let [pkg, consumptions] of crawlDeps(appPackage)) { | ||
for (let dep of pkg.dependencies) { | ||
if (pkg.categorizeDependency(dep.name) === 'peerDependencies') { | ||
for (let ancestors of consumptions) { | ||
for (let ancestor of ancestors.slice().reverse()) { | ||
if (ancestor.hasDependency(dep.name)) { | ||
let ancestorsDep = ancestor.dependencies.find(d => d.name === dep.name)!; | ||
if (ancestorsDep !== dep && dep.isEmberPackage()) { | ||
violations.push({ pkg, dep, ancestors, ancestor, ancestorsDep }); | ||
} | ||
continue; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return violations; | ||
} | ||
|
||
export function summarizePeerDepViolations(violations: PeerDepViolation[]): string { | ||
let message = []; | ||
for (let { pkg, dep, ancestors, ancestor, ancestorsDep } of violations) { | ||
for (let [index, a] of ancestors.entries()) { | ||
message.push(packageIdSummary(a)); | ||
if (index + 1 < ancestors.length) { | ||
message.push(displayConnection(a, ancestors[index + 1])); | ||
} else { | ||
message.push(displayConnection(a, pkg)); | ||
} | ||
} | ||
message.push(packageIdSummary(pkg)); | ||
message.push('\n'); | ||
message.push(` sees peerDep ${packageIdSummary(dep)}\n at ${dep.root}\n`); | ||
message.push( | ||
` but ${packageIdSummary(ancestor)} is using ${packageIdSummary(ancestorsDep)}\n at ${ | ||
ancestorsDep.root | ||
}\n\n` | ||
); | ||
} | ||
return message.join(''); | ||
} | ||
|
||
function displayConnection(left: Package, right: Package) { | ||
if (left.packageJSON.dependencies?.[right.name]) { | ||
return ' -> '; | ||
} | ||
if (left.packageJSON.peerDependencies?.[right.name]) { | ||
return ' (peer)-> '; | ||
} | ||
if (left.packageJSON.devDependencies?.[right.name]) { | ||
return ' (dev)-> '; | ||
} | ||
return ' (?)-> '; | ||
} | ||
|
||
function packageIdSummary(pkg: Package): string { | ||
return `${pkg.name}@${pkg.version}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.