@servicetitan/restrict-imports 40.2.2 → 40.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@servicetitan/restrict-imports",
3
- "version": "40.2.2",
3
+ "version": "40.3.0",
4
4
  "description": "Webpack plugin to enforce import restrictions between modules",
5
5
  "repository": {
6
6
  "type": "git",
@@ -12,7 +12,11 @@
12
12
  "typings": "./dist/index.d.ts",
13
13
  "files": [
14
14
  "dist",
15
- "src"
15
+ "src",
16
+ "!**/__tests__",
17
+ "!**/__mocks__",
18
+ "!**/__snapshots__",
19
+ "!**/*.test.*"
16
20
  ],
17
21
  "publishConfig": {
18
22
  "access": "public"
@@ -1,206 +0,0 @@
1
- import {
2
- Allow,
3
- canonicalizeAllow,
4
- canonicalizeConfig,
5
- canonicalizeEntry,
6
- Config,
7
- Entry,
8
- InvalidConfig,
9
- InvalidEntry,
10
- RawAllow,
11
- RawConfig,
12
- RawEntry,
13
- validateRawConfig,
14
- validateRawEntry,
15
- } from '../config';
16
-
17
- describe('validateRawConfig()', () => {
18
- const cases = [
19
- [null, InvalidConfig.ExpectedAnArrayOrASingleEntry],
20
- [false, InvalidConfig.ExpectedAnArrayOrASingleEntry],
21
- [0, InvalidConfig.ExpectedAnArrayOrASingleEntry],
22
- [42, InvalidConfig.ExpectedAnArrayOrASingleEntry],
23
- ['', InvalidConfig.ExpectedAnArrayOrASingleEntry],
24
- ['awesome', InvalidConfig.ExpectedAnArrayOrASingleEntry],
25
- [new Date(2019, 1), InvalidConfig.ExpectedAnArrayOrASingleEntry],
26
- [() => undefined, InvalidConfig.ExpectedAnArrayOrASingleEntry],
27
- [{}, InvalidConfig.ExpectedAnArrayOrASingleEntry],
28
-
29
- [undefined, false],
30
- [[], false],
31
-
32
- [
33
- [{ name: 'incredible' }, { name: 'amazing', allow: 42 }],
34
- InvalidConfig.InvalidEntry(
35
- 1,
36
- { name: 'amazing', allow: 42 },
37
- InvalidEntry.InvalidAllow
38
- ),
39
- ],
40
-
41
- [
42
- [
43
- { name: 'awesome' },
44
- { name: 'amazing', allow: 'incredible' },
45
- { name: 'incredible', allow: 'amazing' },
46
- ],
47
- InvalidConfig.InvalidOrder(1, { name: 'amazing', allow: 'incredible' }, 'incredible'),
48
- ],
49
- [
50
- [
51
- { name: 'awesome' },
52
- { name: 'breathtaking', allow: 'awesome' },
53
- { name: 'amazing', allow: ['incredible', 'breathtaking'] },
54
- { name: 'incredible', allow: 'breathtaking' },
55
- ],
56
- InvalidConfig.InvalidOrder(
57
- 2,
58
- { name: 'amazing', allow: ['incredible', 'breathtaking'] },
59
- 'incredible'
60
- ),
61
- ],
62
- [
63
- [
64
- { name: 'amazing' },
65
- { name: 'incredible', allow: '*' },
66
- { name: '*', allow: 'amazing' },
67
- ],
68
- InvalidConfig.InvalidOrder(1, { name: 'incredible', allow: '*' }, '*'),
69
- ],
70
-
71
- [
72
- [
73
- { name: 'awesome' },
74
- { name: 'breathtaking' },
75
- { name: 'amazing', allow: ['awesome', 'breathtaking'] },
76
- { name: '*', allow: 'amazing' },
77
- { name: 'incredible', allow: ['*', 'awesome'] },
78
- ],
79
- false,
80
- ],
81
- [[{ name: '*', allow: 'amazing' }], false],
82
- [
83
- [
84
- { name: 'common' },
85
- { name: 'ach-modal', allow: 'common' },
86
- { name: 'accounting', allow: ['common', 'ach-modal'] },
87
- { name: 'onboarding', allow: ['common', 'ach-modal'] },
88
- { name: '*', allow: 'common' },
89
- { name: 'header', allow: '*' },
90
- { name: 'settings', allow: '*' },
91
- ],
92
- false,
93
- ],
94
- ] as [any, string][];
95
-
96
- test.each(cases)('(%p) -> %p', (c, exp) => expect(validateRawConfig(c)).toEqual(exp));
97
- });
98
-
99
- describe('validateRawEntry()', () => {
100
- const cases = [
101
- [undefined, InvalidEntry.ExpectedAPlainObject],
102
- [null, InvalidEntry.ExpectedAPlainObject],
103
- [false, InvalidEntry.ExpectedAPlainObject],
104
- [0, InvalidEntry.ExpectedAPlainObject],
105
- [42, InvalidEntry.ExpectedAPlainObject],
106
- ['', InvalidEntry.ExpectedAPlainObject],
107
- ['awesome', InvalidEntry.ExpectedAPlainObject],
108
- [new Date(2019, 1), InvalidEntry.ExpectedAPlainObject],
109
- [[], InvalidEntry.ExpectedAPlainObject],
110
- [() => undefined, InvalidEntry.ExpectedAPlainObject],
111
-
112
- [{}, InvalidEntry.InvalidName],
113
- [{ name: undefined }, InvalidEntry.InvalidName],
114
- [{ name: null }, InvalidEntry.InvalidName],
115
- [{ name: false }, InvalidEntry.InvalidName],
116
- [{ name: 'awesome' }, false],
117
-
118
- [{ name: 'awesome', allow: null }, InvalidEntry.InvalidAllow],
119
- [{ name: 'awesome', allow: {} }, InvalidEntry.InvalidAllow],
120
- [{ name: 'awesome', allow: 1 }, InvalidEntry.InvalidAllow],
121
- [{ name: 'awesome', allow: '*' }, false],
122
- [{ name: 'awesome', allow: 'amazing' }, false],
123
- [{ name: 'awesome', allow: [] }, false],
124
- [{ name: 'awesome', allow: ['amazing', 'breathtaking'] }, false],
125
- ] as [any, InvalidEntry][];
126
-
127
- test.each(cases)('(%p) -> %p', (entry, exp) => expect(validateRawEntry(entry)).toEqual(exp));
128
- });
129
-
130
- describe('canonicalizeConfig()', () => {
131
- test('single entry', () => {
132
- const config: RawConfig = { name: '*', allow: 'amazing' };
133
- expect(canonicalizeConfig(config)).toEqual([{ name: '*', allow: ['amazing'] }]);
134
- });
135
-
136
- test('multiple entries', () => {
137
- const config: RawConfig = [
138
- { name: 'awesome' },
139
- { name: 'breathtaking' },
140
- { name: 'amazing', allow: ['awesome', 'breathtaking'] },
141
- { name: '*', allow: 'amazing' },
142
- { name: 'incredible', allow: ['*', 'awesome'] },
143
- ];
144
-
145
- expect(canonicalizeConfig(config)).toEqual([
146
- { name: 'awesome', allow: [] },
147
- { name: 'breathtaking', allow: [] },
148
- { name: 'amazing', allow: ['awesome', 'breathtaking'] },
149
- { name: '*', allow: ['amazing'] },
150
- { name: 'incredible', allow: '*' },
151
- ] as Config);
152
- });
153
- });
154
-
155
- describe('canonicalizeEntry()', () => {
156
- test('works as wrapper of canonicalizeAllow()', () => {
157
- const entry: RawEntry = { name: 'amazing', allow: 'awesome' };
158
-
159
- expect(canonicalizeEntry(entry)).toEqual({ name: 'amazing', allow: ['awesome'] } as Entry);
160
- });
161
- });
162
-
163
- describe('canonicalizeAllow()', () => {
164
- const descCases: {
165
- desc: string;
166
- cases: [/* allow */ RawAllow, /* expected */ Allow][];
167
- }[] = [
168
- {
169
- desc: 'an asterisk should always canonicalize into `*`',
170
- cases: [
171
- ['*', '*'],
172
- [['*'], '*'],
173
- [['awesome', '*'], '*'],
174
- ],
175
- },
176
- {
177
- desc: 'a string should canonicalize into array of one string',
178
- cases: [['awesome', ['awesome']]],
179
- },
180
- {
181
- desc: 'an array of strings is already canonical',
182
- cases: [
183
- [['awesome'], ['awesome']],
184
- [
185
- ['awesome', 'breathtaking'],
186
- ['awesome', 'breathtaking'],
187
- ],
188
- ],
189
- },
190
- {
191
- desc: 'empty cases',
192
- cases: [
193
- [undefined, []],
194
- [[], []],
195
- ],
196
- },
197
- ];
198
-
199
- for (const { desc, cases } of descCases) {
200
- describe(desc, () => {
201
- test.each(cases)('(%p) -> %p', (allow, exp) =>
202
- expect(canonicalizeAllow(allow)).toEqual(exp)
203
- );
204
- });
205
- }
206
- });
@@ -1,187 +0,0 @@
1
- import { Allow, Config, RawConfig } from '../config';
2
- import {
3
- allowZone,
4
- disallowZone,
5
- getAllow,
6
- getDisallow,
7
- restrictImports,
8
- } from '../restrict-imports';
9
-
10
- describe('restrictImports()', () => {
11
- const base = '/app/modules/';
12
- const modules = [
13
- 'amazing',
14
- 'awesome',
15
- 'brilliant',
16
- 'incredible',
17
- 'marvelous',
18
- 'surprising',
19
- 'unbelievable',
20
- 'wonderful',
21
- ];
22
-
23
- test('an example config', () => {
24
- const config: RawConfig = [
25
- { name: 'awesome' },
26
- { name: 'amazing', allow: 'awesome' },
27
- { name: '*', allow: ['awesome', 'amazing'] },
28
- { name: 'incredible', allow: '*' },
29
- { name: 'surprising', allow: '*' },
30
- ];
31
-
32
- expect(restrictImports(base, modules, config)).toEqual([
33
- {
34
- target: '/app/modules/amazing',
35
- from: '/app/modules/**/*',
36
- except: ['/app/modules/{amazing,awesome}/**/*'],
37
- },
38
- {
39
- target: '/app/modules/awesome',
40
- from: '/app/modules/**/*',
41
- except: ['/app/modules/awesome/**/*'],
42
- },
43
- {
44
- target: '/app/modules/brilliant',
45
- from: '/app/modules/**/*',
46
- except: ['/app/modules/{brilliant,awesome,amazing}/**/*'],
47
- },
48
- {
49
- target: '/app/modules/incredible',
50
- from: '/app/modules/surprising/**/*',
51
- },
52
- {
53
- target: '/app/modules/marvelous',
54
- from: '/app/modules/**/*',
55
- except: ['/app/modules/{marvelous,awesome,amazing}/**/*'],
56
- },
57
- {
58
- target: '/app/modules/unbelievable',
59
- from: '/app/modules/**/*',
60
- except: ['/app/modules/{unbelievable,awesome,amazing}/**/*'],
61
- },
62
- {
63
- target: '/app/modules/wonderful',
64
- from: '/app/modules/**/*',
65
- except: ['/app/modules/{wonderful,awesome,amazing}/**/*'],
66
- },
67
- ]);
68
- });
69
-
70
- test('an empty config will restrict imports between all modules', () => {
71
- expect(restrictImports(base, modules)).toEqual([
72
- {
73
- target: '/app/modules/amazing',
74
- from: '/app/modules/**/*',
75
- except: ['/app/modules/amazing/**/*'],
76
- },
77
- {
78
- target: '/app/modules/awesome',
79
- from: '/app/modules/**/*',
80
- except: ['/app/modules/awesome/**/*'],
81
- },
82
- {
83
- target: '/app/modules/brilliant',
84
- from: '/app/modules/**/*',
85
- except: ['/app/modules/brilliant/**/*'],
86
- },
87
- {
88
- target: '/app/modules/incredible',
89
- from: '/app/modules/**/*',
90
- except: ['/app/modules/incredible/**/*'],
91
- },
92
- {
93
- target: '/app/modules/marvelous',
94
- from: '/app/modules/**/*',
95
- except: ['/app/modules/marvelous/**/*'],
96
- },
97
- {
98
- target: '/app/modules/surprising',
99
- from: '/app/modules/**/*',
100
- except: ['/app/modules/surprising/**/*'],
101
- },
102
- {
103
- target: '/app/modules/unbelievable',
104
- from: '/app/modules/**/*',
105
- except: ['/app/modules/unbelievable/**/*'],
106
- },
107
- {
108
- target: '/app/modules/wonderful',
109
- from: '/app/modules/**/*',
110
- except: ['/app/modules/wonderful/**/*'],
111
- },
112
- ]);
113
- });
114
-
115
- test('invalid configs should throw exceptions', () => {
116
- expect(() => restrictImports(base, modules, null as any)).toThrow();
117
- expect(() => restrictImports(base, modules, {} as any)).toThrow();
118
- });
119
- });
120
-
121
- describe('getAllow()', () => {
122
- const config: Config = [
123
- { name: 'amazing', allow: ['awesome'] },
124
- { name: 'awesome', allow: [] },
125
- { name: '*', allow: ['awesome', 'amazing'] },
126
- { name: 'incredible', allow: '*' },
127
- ];
128
-
129
- // eslint-disable-next-line @typescript-eslint/naming-convention
130
- const getAllow_ = getAllow(config);
131
-
132
- const cases = [
133
- ['amazing', ['awesome']],
134
- ['awesome', []],
135
- ['breathtaking', ['awesome', 'amazing']],
136
- ['incredible', '*'],
137
- ['unbelievable', ['awesome', 'amazing']],
138
- ] as [/* module */ string, /* expected */ Allow][];
139
-
140
- test.each(cases)('(%p) -> %p', (module, exp) => expect(getAllow_(module)).toEqual(exp));
141
- });
142
-
143
- describe('getDisallow()', () => {
144
- const config: Config = [
145
- { name: '*', allow: [] },
146
- { name: 'incredible', allow: '*' },
147
- { name: 'breathtaking', allow: '*' },
148
- { name: 'marvelous', allow: '*' },
149
- ];
150
-
151
- const cases = [
152
- ['incredible', ['breathtaking', 'marvelous']],
153
- ['breathtaking', ['marvelous']],
154
- ['marvelous', []],
155
- ] as [/* module */ string, /* expected */ Allow][];
156
-
157
- test.each(cases)('(%p) -> %p', (module, exp) =>
158
- expect(getDisallow(config, module)).toEqual(exp)
159
- );
160
- });
161
-
162
- describe('allowZone()', () => {
163
- test('`./packages/admin` can import from itself only', () => {
164
- expect(allowZone('/packages/', 'admin')).toEqual({
165
- target: '/packages/admin',
166
- from: '/packages/**/*',
167
- except: ['/packages/admin/**/*'],
168
- });
169
- });
170
-
171
- test('`./app/awesome` can import from `common` and itself', () => {
172
- expect(allowZone('/app/', 'awesome', ['common'])).toEqual({
173
- target: '/app/awesome',
174
- from: '/app/**/*',
175
- except: ['/app/{awesome,common}/**/*'],
176
- });
177
- });
178
- });
179
-
180
- describe('disallowZone()', () => {
181
- test('`./app/awesome` can import from any module, except for `common`', () => {
182
- expect(disallowZone('./app/', 'awesome', ['common'])).toEqual({
183
- target: './app/awesome',
184
- from: './app/common/**/*',
185
- });
186
- });
187
- });