@servicetitan/restrict-imports 40.2.2 → 40.4.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.
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/config.ts"],"sourcesContent":["/**\n * validateRawConfig\n */\nexport function validateRawConfig(maybeConfig: any): false | string {\n if (maybeConfig === undefined) {\n return false;\n }\n\n if (!Array.isArray(maybeConfig)) {\n if (validateRawEntry(maybeConfig)) {\n return InvalidConfig.ExpectedAnArrayOrASingleEntry;\n }\n\n return false;\n }\n\n const invalidEntryError = isThereInvalidEntry(maybeConfig);\n if (invalidEntryError) {\n return invalidEntryError;\n }\n\n const invalidOrderError = isInvalidEntryOrder(maybeConfig);\n if (invalidOrderError) {\n return invalidOrderError;\n }\n\n return false;\n\n // ------------------\n\n function isThereInvalidEntry(c0: any[]): false | string {\n for (let index = 0; index < c0.length; index += 1) {\n const entry = c0[index];\n const error = validateRawEntry(entry);\n if (error) {\n return InvalidConfig.InvalidEntry(index, entry, error);\n }\n }\n\n return false;\n }\n\n function isInvalidEntryOrder(c0: RawEntry[]): false | string {\n const available = new Set<string>();\n\n for (let index = 0; index < c0.length; index += 1) {\n const entry = c0[index];\n\n available.add(entry.name);\n\n const allow0 = canonicalizeAllow(entry.allow);\n const allow = allow0 === '*' ? [allow0] : allow0;\n\n const unavailable = allow.find(n => !available.has(n) && !available.has('*'));\n if (unavailable) {\n return InvalidConfig.InvalidOrder(index, entry, unavailable);\n }\n }\n\n return false;\n }\n}\n\nexport const InvalidConfig = {\n ExpectedAnArrayOrASingleEntry: 'Expected an array of config entries, or a single entry',\n\n InvalidEntry: (index: number, entry: any, error: string) =>\n `Invalid entry #${index} (${JSON.stringify(entry)}): ${error}`,\n\n InvalidOrder: (index: number, entry: RawEntry, unavailable: string) =>\n `Invalid entries order, #${index} (${JSON.stringify(entry)}):` +\n ` '${unavailable}' is used before its description`,\n};\n\nexport function validateRawEntry(e: any): false | InvalidEntry {\n if (!isPlainObject(e)) {\n return InvalidEntry.ExpectedAPlainObject;\n }\n\n if (!isValidName(e.name)) {\n return InvalidEntry.InvalidName;\n }\n\n if (!isValidAllow(e.allow)) {\n return InvalidEntry.InvalidAllow;\n }\n\n return false;\n\n // ------------------\n\n // eslint-disable-next-line @typescript-eslint/no-wrapper-object-types\n function isPlainObject(v: any): v is Object {\n return v != null && typeof v === 'object' && Object.getPrototypeOf(v) === Object.prototype;\n }\n\n function isValidName(name: any): name is string {\n return typeof name === 'string';\n }\n\n function isValidAllow(allow: any): allow is RawAllow {\n if (typeof allow === 'string' || allow === undefined) {\n return true;\n }\n\n if (!Array.isArray(allow)) {\n return false;\n }\n\n return !allow.some(v => typeof v !== 'string');\n }\n}\n\nexport enum InvalidEntry {\n ExpectedAPlainObject = 'Expected a plain object with fields `name` and `allow`',\n InvalidName = 'The `name` field is expected to be `string`',\n InvalidAllow = 'The `allow` field is expected to be `string|string[]|undefined`',\n}\n\n/**\n * RawConfig\n */\nexport type RawConfig = RawEntry | RawEntry[] | undefined;\n\nexport interface RawEntry {\n name: string;\n allow?: string | string[];\n}\n\nexport type RawAllow = RawEntry['allow'];\n\n/**\n * Config\n */\nexport type Config = Entry[];\n\nexport interface Entry extends RawEntry {\n name: string;\n allow: Allow;\n}\n\nexport type Allow = '*' | string[];\n\n/**\n * canonicalizeConfig\n */\nexport function canonicalizeConfig(c: RawConfig = []): Config {\n if (Array.isArray(c)) {\n return c.map(canonicalizeEntry);\n }\n return [canonicalizeEntry(c)];\n}\n\nexport function canonicalizeEntry(e: RawEntry): Entry {\n return {\n name: e.name,\n allow: canonicalizeAllow(e.allow),\n };\n}\n\nexport function canonicalizeAllow(a: RawAllow): Allow {\n const allow = Array.isArray(a) ? a : a ? [a] : [];\n\n if (allow.includes('*')) {\n return '*';\n }\n\n return allow;\n}\n"],"names":["InvalidConfig","InvalidEntry","canonicalizeAllow","canonicalizeConfig","canonicalizeEntry","validateRawConfig","validateRawEntry","maybeConfig","undefined","Array","isArray","ExpectedAnArrayOrASingleEntry","invalidEntryError","isThereInvalidEntry","invalidOrderError","isInvalidEntryOrder","c0","index","length","entry","error","available","Set","add","name","allow0","allow","unavailable","find","n","has","InvalidOrder","JSON","stringify","e","isPlainObject","isValidName","isValidAllow","v","Object","getPrototypeOf","prototype","some","c","map","a","includes"],"mappings":"AAAA;;CAEC;;;;;;;;;;;QA6DYA;eAAAA;;QAkDDC;eAAAA;;QA+CIC;eAAAA;;QAdAC;eAAAA;;QAOAC;eAAAA;;QAtJAC;eAAAA;;QAuEAC;eAAAA;;;AAvET,SAASD,kBAAkBE,WAAgB;IAC9C,IAAIA,gBAAgBC,WAAW;QAC3B,OAAO;IACX;IAEA,IAAI,CAACC,MAAMC,OAAO,CAACH,cAAc;QAC7B,IAAID,iBAAiBC,cAAc;YAC/B,OAAOP,cAAcW,6BAA6B;QACtD;QAEA,OAAO;IACX;IAEA,MAAMC,oBAAoBC,oBAAoBN;IAC9C,IAAIK,mBAAmB;QACnB,OAAOA;IACX;IAEA,MAAME,oBAAoBC,oBAAoBR;IAC9C,IAAIO,mBAAmB;QACnB,OAAOA;IACX;IAEA,OAAO;IAEP,qBAAqB;IAErB,SAASD,oBAAoBG,EAAS;QAClC,IAAK,IAAIC,QAAQ,GAAGA,QAAQD,GAAGE,MAAM,EAAED,SAAS,EAAG;YAC/C,MAAME,QAAQH,EAAE,CAACC,MAAM;YACvB,MAAMG,QAAQd,iBAAiBa;YAC/B,IAAIC,OAAO;gBACP,OAAOpB,cAAcC,YAAY,CAACgB,OAAOE,OAAOC;YACpD;QACJ;QAEA,OAAO;IACX;IAEA,SAASL,oBAAoBC,EAAc;QACvC,MAAMK,YAAY,IAAIC;QAEtB,IAAK,IAAIL,QAAQ,GAAGA,QAAQD,GAAGE,MAAM,EAAED,SAAS,EAAG;YAC/C,MAAME,QAAQH,EAAE,CAACC,MAAM;YAEvBI,UAAUE,GAAG,CAACJ,MAAMK,IAAI;YAExB,MAAMC,SAASvB,kBAAkBiB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,OAAOA,MAAM;IACzC;AACJ;AAEO,IAAA,AAAKrC,sCAAAA;;;;WAAAA;;AAiCL,SAASE,mBAAmBwC,IAAe,EAAE;IAChD,IAAIlC,MAAMC,OAAO,CAACiC,IAAI;QAClB,OAAOA,EAAEC,GAAG,CAACxC;IACjB;IACA,OAAO;QAACA,kBAAkBuC;KAAG;AACjC;AAEO,SAASvC,kBAAkB8B,CAAW;IACzC,OAAO;QACHV,MAAMU,EAAEV,IAAI;QACZE,OAAOxB,kBAAkBgC,EAAER,KAAK;IACpC;AACJ;AAEO,SAASxB,kBAAkB2C,CAAW;IACzC,MAAMnB,QAAQjB,MAAMC,OAAO,CAACmC,KAAKA,IAAIA,IAAI;QAACA;KAAE,GAAG,EAAE;IAEjD,IAAInB,MAAMoB,QAAQ,CAAC,MAAM;QACrB,OAAO;IACX;IAEA,OAAOpB;AACX"}
1
+ {"version":3,"sources":["../src/config.ts"],"sourcesContent":["/**\n * validateRawConfig\n */\nexport function validateRawConfig(maybeConfig: any): false | string {\n if (maybeConfig === undefined) {\n return false;\n }\n\n if (!Array.isArray(maybeConfig)) {\n if (validateRawEntry(maybeConfig)) {\n return InvalidConfig.ExpectedAnArrayOrASingleEntry;\n }\n\n return false;\n }\n\n const invalidEntryError = isThereInvalidEntry(maybeConfig);\n if (invalidEntryError) {\n return invalidEntryError;\n }\n\n const invalidOrderError = isInvalidEntryOrder(maybeConfig);\n if (invalidOrderError) {\n return invalidOrderError;\n }\n\n return false;\n\n // ------------------\n\n function isThereInvalidEntry(c0: any[]): false | string {\n for (let index = 0; index < c0.length; index += 1) {\n const entry = c0[index];\n const error = validateRawEntry(entry);\n if (error) {\n return InvalidConfig.InvalidEntry(index, entry, error);\n }\n }\n\n return false;\n }\n\n function isInvalidEntryOrder(c0: RawEntry[]): false | string {\n const available = new Set<string>();\n\n for (let index = 0; index < c0.length; index += 1) {\n const entry = c0[index];\n\n available.add(entry.name);\n\n const allow0 = canonicalizeAllow(entry.allow);\n const allow = allow0 === '*' ? [allow0] : allow0;\n\n const unavailable = allow.find(n => !available.has(n) && !available.has('*'));\n if (unavailable) {\n return InvalidConfig.InvalidOrder(index, entry, unavailable);\n }\n }\n\n return false;\n }\n}\n\nexport const InvalidConfig = {\n ExpectedAnArrayOrASingleEntry: 'Expected an array of config entries, or a single entry',\n\n InvalidEntry: (index: number, entry: any, error: string) =>\n `Invalid entry #${index} (${JSON.stringify(entry)}): ${error}`,\n\n InvalidOrder: (index: number, entry: RawEntry, unavailable: string) =>\n `Invalid entries order, #${index} (${JSON.stringify(entry)}):` +\n ` '${unavailable}' is used before its description`,\n};\n\nexport function validateRawEntry(e: any): false | InvalidEntry {\n if (!isPlainObject(e)) {\n return InvalidEntry.ExpectedAPlainObject;\n }\n\n if (!isValidName(e.name)) {\n return InvalidEntry.InvalidName;\n }\n\n if (!isValidAllow(e.allow)) {\n return InvalidEntry.InvalidAllow;\n }\n\n return false;\n\n // ------------------\n\n // eslint-disable-next-line @typescript-eslint/no-wrapper-object-types\n function isPlainObject(v: any): v is Object {\n return v != null && typeof v === 'object' && Object.getPrototypeOf(v) === Object.prototype;\n }\n\n function isValidName(name: any): name is string {\n return typeof name === 'string';\n }\n\n function isValidAllow(allow: any): allow is RawAllow {\n if (typeof allow === 'string' || allow === undefined) {\n return true;\n }\n\n if (!Array.isArray(allow)) {\n return false;\n }\n\n return !allow.some(v => typeof v !== 'string');\n }\n}\n\nexport enum InvalidEntry {\n ExpectedAPlainObject = 'Expected a plain object with fields `name` and `allow`',\n InvalidName = 'The `name` field is expected to be `string`',\n InvalidAllow = 'The `allow` field is expected to be `string|string[]|undefined`',\n}\n\n/**\n * RawConfig\n */\nexport type RawConfig = RawEntry | RawEntry[] | undefined;\n\nexport interface RawEntry {\n name: string;\n allow?: string | string[];\n}\n\nexport type RawAllow = RawEntry['allow'];\n\n/**\n * Config\n */\nexport type Config = Entry[];\n\nexport interface Entry extends RawEntry {\n name: string;\n allow: Allow;\n}\n\nexport type Allow = '*' | string[];\n\n/**\n * canonicalizeConfig\n */\nexport function canonicalizeConfig(c: RawConfig = []): Config {\n if (Array.isArray(c)) {\n return c.map(canonicalizeEntry);\n }\n return [canonicalizeEntry(c)];\n}\n\nexport function canonicalizeEntry(e: RawEntry): Entry {\n return {\n name: e.name,\n allow: canonicalizeAllow(e.allow),\n };\n}\n\nexport function canonicalizeAllow(a: RawAllow): Allow {\n const allow = Array.isArray(a) ? a : a ? [a] : [];\n\n if (allow.includes('*')) {\n return '*';\n }\n\n return allow;\n}\n"],"names":["InvalidConfig","InvalidEntry","canonicalizeAllow","canonicalizeConfig","canonicalizeEntry","validateRawConfig","validateRawEntry","maybeConfig","undefined","Array","isArray","ExpectedAnArrayOrASingleEntry","invalidEntryError","isThereInvalidEntry","invalidOrderError","isInvalidEntryOrder","c0","index","length","entry","error","available","Set","add","name","allow0","allow","unavailable","find","n","has","InvalidOrder","JSON","stringify","e","isPlainObject","isValidName","isValidAllow","v","Object","getPrototypeOf","prototype","some","c","map","a","includes"],"mappings":"AAAA;;CAEC;;;;;;;;;;;QA6DYA;eAAAA;;QAkDDC;eAAAA;;QA+CIC;eAAAA;;QAdAC;eAAAA;;QAOAC;eAAAA;;QAtJAC;eAAAA;;QAuEAC;eAAAA;;;AAvET,SAASD,kBAAkBE,WAAgB;IAC9C,IAAIA,gBAAgBC,WAAW;QAC3B,OAAO;IACX;IAEA,IAAI,CAACC,MAAMC,OAAO,CAACH,cAAc;QAC7B,IAAID,iBAAiBC,cAAc;YAC/B,OAAOP,cAAcW,6BAA6B;QACtD;QAEA,OAAO;IACX;IAEA,MAAMC,oBAAoBC,oBAAoBN;IAC9C,IAAIK,mBAAmB;QACnB,OAAOA;IACX;IAEA,MAAME,oBAAoBC,oBAAoBR;IAC9C,IAAIO,mBAAmB;QACnB,OAAOA;IACX;IAEA,OAAO;IAEP,qBAAqB;IAErB,SAASD,oBAAoBG,EAAS;QAClC,IAAK,IAAIC,QAAQ,GAAGA,QAAQD,GAAGE,MAAM,EAAED,SAAS,E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EP,qBAAqB;IAErB,sEAAsE;IACtE,SAASS,cAAcG,CAAM;QACzB,OAAOA,KAAK,QAAQ,OAAOA,MAAM,YAAYC,OAAOC,cAAc,CAACF,OAAOC,OAAOE,SAAS;IAC9F;IAEA,SAASL,YAAYZ,IAAS;QAC1B,OAAO,OAAOA,SAAS;IAC3B;IAEA,SAASa,aAAaX,KAAU;QAC5B,IAAI,OAAOA,UAAU,YAAYA,UAAUlB,WAAW;YAClD,OAAO;QACX;QAEA,IAAI,CAACC,MAAMC,OAAO,CAACgB,QAAQ;YACvB,OAAO;QACX;QAEA,OAAO,CAACA,MAAMgB,IAAI,CAACJ,CAAAA,IAAK,OAAOA,MAAM;IACzC;AACJ;AAEO,IAAA,AAAKrC;;;;;;AAiCL,SAASE,mBAAmBwC,IAAe,EAAE;IAChD,IAAIlC,MAAMC,OAAO,CAACiC,IAAI;QAClB,OAAOA,EAAEC,GAAG,CAACxC;IACjB;IACA,OAAO;QAACA,kBAAkBuC;KAAG;AACjC;AAEO,SAASvC,kBAAkB8B,CAAW;IACzC,OAAO;QACHV,MAAMU,EAAEV,IAAI;QACZE,OAAOxB,kBAAkBgC,EAAER,KAAK;IACpC;AACJ;AAEO,SAASxB,kBAAkB2C,CAAW;IACzC,MAAMnB,QAAQjB,MAAMC,OAAO,CAACmC,KAAKA,IAAIA,IAAI;QAACA;KAAE,GAAG,EAAE;IAEjD,IAAInB,MAAMoB,QAAQ,CAAC,MAAM;QACrB,OAAO;IACX;IAEA,OAAOpB;AACX"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@servicetitan/restrict-imports",
3
- "version": "40.2.2",
3
+ "version": "40.4.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
- });