Skip to content

Commit 779b82c

Browse files
committed
fix: update grant list on file change
1 parent b0e2b46 commit 779b82c

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

src/index.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,30 @@ const META_START = '// ==UserScript==';
2626
const META_END = '// ==/UserScript==';
2727

2828
export default (metafile, transform) => {
29-
const grants = new Set();
29+
const grantMap = new Map();
3030
return {
3131
name: 'banner',
3232
buildStart() {
3333
this.addWatchFile(metafile);
34-
grants.clear();
3534
},
36-
transform(code) {
35+
transform(code, id) {
3736
const ast = this.parse(code);
3837
let scope = attachScopes(ast, 'scope');
38+
const grantSetPerFile = new Set();
3939
walk(ast, {
4040
enter(node, parent) {
4141
if (node.scope) scope = node.scope;
4242
if (node.type === 'Identifier' && isReference(node, parent) && !scope.contains(node.name)) {
4343
if (gmAPIs.includes(node.name)) {
44-
grants.add(node.name);
44+
grantSetPerFile.add(node.name);
4545
}
4646
}
4747
},
4848
leave(node) {
4949
if (node.scope) scope = scope.parent;
5050
},
5151
});
52+
grantMap.set(id, grantSetPerFile);
5253
},
5354
async banner() {
5455
let meta = await fs.readFile(metafile, 'utf8');
@@ -59,6 +60,7 @@ export default (metafile, transform) => {
5960
console.warn('Invalid metadata block. For more details see https://violentmonkey.github.io/api/metadata-block/');
6061
return;
6162
}
63+
const grantSet = new Set();
6264
const items = lines.slice(start + 1, end)
6365
.map(line => {
6466
if (!line.startsWith('// ')) return;
@@ -68,14 +70,24 @@ export default (metafile, transform) => {
6870
const key = line.slice(0, i);
6971
const value = line.slice(i + 1).trim();
7072
if (key === '@grant') {
71-
grants.add(value);
73+
grantSet.add(value);
7274
return;
7375
}
7476
return [key, value];
7577
})
7678
.filter(Boolean);
77-
for (const grant of grants) {
78-
items.push(['@grant', grant]);
79+
for (const id of this.getModuleIds()) {
80+
const grantSetPerFile = grantMap.get(id);
81+
if (grantSetPerFile) {
82+
for (const item of grantSetPerFile) {
83+
grantSet.add(item);
84+
}
85+
}
86+
}
87+
const grantList = Array.from(grantSet);
88+
grantList.sort();
89+
for (const item of grantList) {
90+
items.push(['@grant', item]);
7991
}
8092
const maxKeyWidth = Math.max(...items.map(([key]) => key.length));
8193
meta = [

0 commit comments

Comments
 (0)