Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure dependencySatisfies only considers actual dependencies (includes a fix for invalid results within monorepo scenarios) #1070

9 changes: 8 additions & 1 deletion packages/macros/src/babel/dependency-satisfies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default function dependencySatisfies(path: NodePath<t.CallExpression>, st
if (path.node.arguments.length !== 2) {
throw error(path, `dependencySatisfies takes exactly two arguments, you passed ${path.node.arguments.length}`);
}
let [packageName, range] = path.node.arguments;
const [packageName, range] = path.node.arguments;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needed to be const due to the function usage in line 35

if (packageName.type !== 'StringLiteral') {
throw error(
assertArray(path.get('arguments'))[0],
Expand All @@ -31,6 +31,13 @@ export default function dependencySatisfies(path: NodePath<t.CallExpression>, st
if (!us) {
return false;
}

let hasPackage = us.dependencies.find(dep => dep.name === packageName.value);

if (!hasPackage) {
return false;
}

let version = packageCache.resolve(packageName.value, us).version;
return satisfies(version, range.value, {
includePrerelease: true,
Expand Down
6 changes: 6 additions & 0 deletions packages/macros/src/glimmer/dependency-satisfies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ export default function dependencySatisfies(

let pkg;
try {
let hasPackage = us.dependencies.find(dep => dep.name === packageName);

if (!hasPackage) {
return false;
}

pkg = packageCache.resolve(packageName, us);
} catch (err) {
// it's not an error if we can't resolve it, we just don't satisfy it.
Expand Down
20 changes: 20 additions & 0 deletions packages/macros/tests/babel/dependency-satisfies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,25 @@ describe(`dependencySatisfies`, function () {
);
expect(runDefault(code)).toBe(true);
});

test('monorepo resolutions resolve correctly', () => {
NullVoxPopuli marked this conversation as resolved.
Show resolved Hide resolved
project = new Project('test-app', '1.0.0');
project.addDependency('@embroider/util', '*');
project.writeSync();

process.chdir(project.baseDir);

let code = transform(`
import { dependencySatisfies } from '@embroider/macros';

// specified in dependencies
export const util = dependencySatisfies('@embroider/util', '*');
// not specified as any kind of dep
export const webpack = dependencySatisfies('@embroider/webpack', '*');
`);

expect(code).toMatch(/util = true/);
expect(code).toMatch(/webpack = false/);
NullVoxPopuli marked this conversation as resolved.
Show resolved Hide resolved
});
});
});