1+ import { parse as parseCode } from '@babel/parser' ;
12import type { AstNode } from 'rollup' ;
2- import type { EmptyStatement } from 'estree' ;
33
44import {
5- collectGmApi ,
5+ collectGrants ,
66 getMetadata
77} from '../src/util' ;
88
9- describe ( 'collectGmApi' , ( ) => {
10- const EMPTY_STATEMENT : EmptyStatement = {
11- type : 'EmptyStatement'
12- } ;
13-
14- it ( 'should return an empty set on an empty input' , ( ) => {
15- expect ( collectGmApi ( EMPTY_STATEMENT as AstNode ) . size ) . toBe ( 0 ) ;
16- } ) ;
9+ describe ( 'collectGrants' , ( ) => {
10+ const parseCodeAsEstreeAst = ( code : string ) => {
11+ const file = parseCode ( code , { plugins : [ 'estree' ] } ) ;
12+ return file . program as AstNode ;
13+ } ;
14+
15+ it ( 'should return an empty set on an empty input' , ( ) => {
16+ const astNode = parseCodeAsEstreeAst ( `` ) ;
17+ const result = collectGrants ( astNode ) ;
18+
19+ expect ( result . size ) . toBe ( 0 ) ;
20+ } ) ;
21+
22+ it ( 'should return only GM_dummyApi' , ( ) => {
23+ const astNode = parseCodeAsEstreeAst ( `GM_dummyApi` ) ;
24+ const result = collectGrants ( astNode ) ;
25+
26+ expect ( result . size ) . toBe ( 1 ) ;
27+ expect ( result ) . toContain ( 'GM_dummyApi' ) ;
28+ } ) ;
29+
30+ it ( 'should ignore any scope-defined variables that look like GM APIs' , ( ) => {
31+ const astNode = parseCodeAsEstreeAst ( `
32+ let GM_dummyApi;
33+ GM_dummyApi;
34+ ` ) ;
35+ const result = collectGrants ( astNode ) ;
36+
37+ expect ( result . size ) . toBe ( 0 ) ;
38+ } ) ;
39+
40+ it ( 'should return only GM.dummyApi' , ( ) => {
41+ const astNode = parseCodeAsEstreeAst ( `GM.dummyApi` ) ;
42+ const result = collectGrants ( astNode ) ;
43+
44+ expect ( result . size ) . toBe ( 1 ) ;
45+ expect ( result ) . toContain ( 'GM.dummyApi' ) ;
46+ } ) ;
47+
48+ it ( 'should return unsafeWindow when presented with just unsafeWindow' , ( ) => {
49+ const astNode = parseCodeAsEstreeAst ( `unsafeWindow` ) ;
50+ const result = collectGrants ( astNode ) ;
51+
52+ expect ( result . size ) . toBe ( 1 ) ;
53+ expect ( result ) . toContain ( 'unsafeWindow' ) ;
54+ } ) ;
55+
56+ it ( 'should return nothing unsafeWindow when presented with unsafeWindowButNotReally' , ( ) => {
57+ const astNode = parseCodeAsEstreeAst ( `unsafeWindowButNotReally` ) ;
58+ const result = collectGrants ( astNode ) ;
59+
60+ expect ( result . size ) . toBe ( 0 ) ;
61+ } ) ;
62+
63+ it ( 'should return unsafeWindow even when a subfield is accessed' , ( ) => {
64+ const astNode = parseCodeAsEstreeAst ( `unsafeWindow.anotherThing` ) ;
65+ const result = collectGrants ( astNode ) ;
66+
67+ expect ( result . size ) . toBe ( 1 ) ;
68+ expect ( result ) . toContain ( 'unsafeWindow' ) ;
69+ } ) ;
70+
71+ it ( 'should return unsafeWindow even when a subfield is accessed with object notation' , ( ) => {
72+ const astNode = parseCodeAsEstreeAst ( `unsafeWindow["anotherThing"]` ) ;
73+ const result = collectGrants ( astNode ) ;
74+
75+ expect ( result . size ) . toBe ( 1 ) ;
76+ expect ( result ) . toContain ( 'unsafeWindow' ) ;
77+ } ) ;
1778} ) ;
1879
1980describe ( 'getMetadata' , ( ) => {
20- it ( 'should throw error on an empty input' , ( ) => {
21- expect ( ( ) => getMetadata ( '' , new Set ( ) ) ) . toThrow ( Error ) ;
22- } ) ;
23- } ) ;
81+ it ( 'should throw error on an empty input' , ( ) => {
82+ expect ( ( ) => getMetadata ( '' , new Set ( ) ) ) . toThrow ( Error ) ;
83+ } ) ;
84+ } ) ;
0 commit comments