Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/snaps-utils/coverage.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"branches": 99.76,
"functions": 99,
"lines": 98.68,
"statements": 97.27
"statements": 97.28
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,36 @@ describe('unusedExports', () => {
),
);
});

it('does not report if the Snap exports a handler and requests permission for it in the manifest', async () => {
const report = jest.fn();
assert(unusedExports.semanticCheck);

const files = getMockSnapFiles({
manifest: getSnapManifest({
initialPermissions: {
'endowment:page-home': {},
'endowment:rpc': {},
'endowment:lifecycle-hooks': {},
},
}),
manifestPath: __filename,
});

await unusedExports.semanticCheck(files, {
report,
options: {
exports: ['onRpcRequest', 'onHomePage', 'onInstall'],
handlerEndowments: {
onRpcRequest: 'endowment:rpc',
onHomePage: 'endowment:page-home',
onInstall: 'endowment:lifecycle-hooks',
onUpdate: 'endowment:lifecycle-hooks',
onUserInput: null,
},
},
});

expect(report).toHaveBeenCalledTimes(0);
});
});
14 changes: 13 additions & 1 deletion packages/snaps-utils/src/manifest/validators/unused-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ export const unusedExports: ValidatorMeta = {
return;
}

// Endowments used based on the exports from the Snap. This is used to
// filter endowments that are used by multiple handlers, e.g., the lifecycle
// handlers.
const usedEndowments = Object.entries(handlerEndowments)
.filter(
([handler, endowment]) =>
endowment === null || exports.includes(handler),
)
.map(([, endowment]) => endowment);

const unusedHandlers = Object.entries(handlerEndowments)
.filter(([handler, endowment]) => {
if (endowment === null) {
Expand All @@ -39,9 +49,11 @@ export const unusedExports: ValidatorMeta = {
}

return (
!usedEndowments.includes(endowment) &&
files.manifest.result.initialPermissions[
endowment as keyof InitialPermissions
] && !exports.includes(handler)
] &&
!exports.includes(handler)
);
},
);
Expand Down
Loading