-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathutil.test.ts
More file actions
86 lines (67 loc) · 2.62 KB
/
Copy pathutil.test.ts
File metadata and controls
86 lines (67 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { parse } from 'acorn';
import type { AstNode } from 'rollup';
import { describe, expect, it } from 'vitest';
import { collectGrants, getMetadata } from '../src/util';
describe('collectGrants', () => {
const parseCodeAsEstreeAst = (code: string) => {
// Acorn options to match Rollup's parsing environment
const ast = parse(code, {
ecmaVersion: 2020, // or a version appropriate for your project's target
sourceType: 'module',
});
return ast as unknown as AstNode;
};
it('should return an empty set on an empty input', () => {
const astNode = parseCodeAsEstreeAst(``);
const result = collectGrants(astNode);
expect(result.size).toBe(0);
});
it('should return only GM_dummyApi', () => {
const astNode = parseCodeAsEstreeAst(`GM_dummyApi`);
const result = collectGrants(astNode);
expect(result.size).toBe(1);
expect(result).toContain('GM_dummyApi');
});
it('should ignore any scope-defined variables that look like GM APIs', () => {
const astNode = parseCodeAsEstreeAst(`
let GM_dummyApi;
GM_dummyApi;
`);
const result = collectGrants(astNode);
expect(result.size).toBe(0);
});
it('should return only GM.dummyApi', () => {
const astNode = parseCodeAsEstreeAst(`GM.dummyApi`);
const result = collectGrants(astNode);
expect(result.size).toBe(1);
expect(result).toContain('GM.dummyApi');
});
it('should return unsafeWindow when presented with just unsafeWindow', () => {
const astNode = parseCodeAsEstreeAst(`unsafeWindow`);
const result = collectGrants(astNode);
expect(result.size).toBe(1);
expect(result).toContain('unsafeWindow');
});
it('should return nothing unsafeWindow when presented with unsafeWindowButNotReally', () => {
const astNode = parseCodeAsEstreeAst(`unsafeWindowButNotReally`);
const result = collectGrants(astNode);
expect(result.size).toBe(0);
});
it('should return unsafeWindow even when a subfield is accessed', () => {
const astNode = parseCodeAsEstreeAst(`unsafeWindow.anotherThing`);
const result = collectGrants(astNode);
expect(result.size).toBe(1);
expect(result).toContain('unsafeWindow');
});
it('should return unsafeWindow even when a subfield is accessed with object notation', () => {
const astNode = parseCodeAsEstreeAst(`unsafeWindow["anotherThing"]`);
const result = collectGrants(astNode);
expect(result.size).toBe(1);
expect(result).toContain('unsafeWindow');
});
});
describe('getMetadata', () => {
it('should throw error on an empty input', () => {
expect(() => getMetadata('', new Set())).toThrow(Error);
});
});