@atlaspack/utils 2.17.3-typescript-bc4459c37.0 → 2.17.3

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.
Files changed (114) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/lib/DefaultMap.js +4 -0
  3. package/lib/ansi-html.js +0 -2
  4. package/lib/config.js +1 -3
  5. package/lib/debug-tools.js +0 -1
  6. package/lib/glob.js +4 -12
  7. package/lib/index.js +9 -164
  8. package/lib/is-url.js +0 -2
  9. package/lib/objectHash.js +1 -0
  10. package/lib/prettyDiagnostic.js +5 -3
  11. package/lib/schema.js +19 -38
  12. package/lib/shared-buffer.js +1 -5
  13. package/lib/urlJoin.js +1 -1
  14. package/package.json +15 -22
  15. package/src/{DefaultMap.ts → DefaultMap.js} +12 -8
  16. package/src/Deferred.js +23 -0
  17. package/src/{PromiseQueue.ts → PromiseQueue.js} +35 -40
  18. package/src/{TapStream.ts → TapStream.js} +7 -8
  19. package/src/{alternatives.ts → alternatives.js} +15 -14
  20. package/src/{ansi-html.ts → ansi-html.js} +1 -1
  21. package/src/{blob.ts → blob.js} +4 -2
  22. package/src/{bundle-url.ts → bundle-url.js} +5 -3
  23. package/src/{collection.ts → collection.js} +21 -14
  24. package/src/{config.ts → config.js} +19 -18
  25. package/src/{countLines.ts → countLines.js} +2 -0
  26. package/src/{debounce.ts → debounce.js} +5 -3
  27. package/src/{debug-tools.ts → debug-tools.js} +8 -6
  28. package/src/{dependency-location.ts → dependency-location.js} +11 -15
  29. package/src/{escape-html.ts → escape-html.js} +3 -5
  30. package/src/{generateBuildMetrics.ts → generateBuildMetrics.js} +18 -16
  31. package/src/{generateCertificate.ts → generateCertificate.js} +5 -7
  32. package/src/{getCertificate.ts → getCertificate.js} +3 -5
  33. package/src/{getExisting.ts → getExisting.js} +3 -4
  34. package/src/{getModuleParts.ts → getModuleParts.js} +2 -3
  35. package/src/{getRootDir.ts → getRootDir.js} +2 -0
  36. package/src/{glob.ts → glob.js} +10 -14
  37. package/src/{hash.ts → hash.js} +17 -22
  38. package/src/{http-server.ts → http-server.js} +38 -32
  39. package/src/{index.ts → index.js} +8 -7
  40. package/src/{is-url.ts → is-url.js} +2 -1
  41. package/src/{isDirectoryInside.ts → isDirectoryInside.js} +1 -0
  42. package/src/{objectHash.ts → objectHash.js} +4 -1
  43. package/src/{openInBrowser.ts → openInBrowser.js} +4 -2
  44. package/src/{parseCSSImport.ts → parseCSSImport.js} +2 -0
  45. package/src/{path.ts → path.js} +3 -1
  46. package/src/{prettifyTime.ts → prettifyTime.js} +2 -0
  47. package/src/{prettyDiagnostic.ts → prettyDiagnostic.js} +20 -22
  48. package/src/{progress-message.ts → progress-message.js} +2 -3
  49. package/src/{relativeBundlePath.ts → relativeBundlePath.js} +3 -3
  50. package/src/{relativeUrl.ts → relativeUrl.js} +1 -0
  51. package/src/{replaceBundleReferences.ts → replaceBundleReferences.js} +36 -54
  52. package/src/{schema.ts → schema.js} +141 -158
  53. package/src/{shared-buffer.ts → shared-buffer.js} +3 -5
  54. package/src/{sourcemap.ts → sourcemap.js} +6 -16
  55. package/src/{stream.ts → stream.js} +21 -29
  56. package/src/throttle.js +15 -0
  57. package/src/{urlJoin.ts → urlJoin.js} +3 -1
  58. package/test/{DefaultMap.test.ts → DefaultMap.test.js} +6 -4
  59. package/test/{PromiseQueue.test.ts → PromiseQueue.test.js} +6 -5
  60. package/test/{collection.test.ts → collection.test.js} +2 -0
  61. package/test/{config.test.ts → config.test.js} +3 -0
  62. package/test/{objectHash.test.ts → objectHash.test.js} +5 -4
  63. package/test/{prettifyTime.test.ts → prettifyTime.test.js} +1 -0
  64. package/test/{replaceBundleReferences.test.ts → replaceBundleReferences.test.js} +32 -0
  65. package/test/{sourcemap.test.ts → sourcemap.test.js} +1 -0
  66. package/test/{throttle.test.ts → throttle.test.js} +3 -1
  67. package/test/{urlJoin.test.ts → urlJoin.test.js} +2 -0
  68. package/LICENSE +0 -201
  69. package/lib/DefaultMap.d.ts +0 -13
  70. package/lib/Deferred.d.ts +0 -8
  71. package/lib/PromiseQueue.d.ts +0 -25
  72. package/lib/TapStream.d.ts +0 -6
  73. package/lib/alternatives.d.ts +0 -3
  74. package/lib/ansi-html.d.ts +0 -1
  75. package/lib/blob.d.ts +0 -4
  76. package/lib/bundle-url.d.ts +0 -4
  77. package/lib/collection.d.ts +0 -33
  78. package/lib/config.d.ts +0 -17
  79. package/lib/countLines.d.ts +0 -1
  80. package/lib/debounce.d.ts +0 -1
  81. package/lib/debug-tools.d.ts +0 -6
  82. package/lib/dependency-location.d.ts +0 -14
  83. package/lib/escape-html.d.ts +0 -1
  84. package/lib/generateBuildMetrics.d.ts +0 -18
  85. package/lib/generateCertificate.d.ts +0 -5
  86. package/lib/getCertificate.d.ts +0 -6
  87. package/lib/getExisting.d.ts +0 -8
  88. package/lib/getModuleParts.d.ts +0 -4
  89. package/lib/getRootDir.d.ts +0 -2
  90. package/lib/glob.d.ts +0 -10
  91. package/lib/hash.d.ts +0 -7
  92. package/lib/http-server.d.ts +0 -19
  93. package/lib/index.d.ts +0 -48
  94. package/lib/is-url.d.ts +0 -1
  95. package/lib/isDirectoryInside.d.ts +0 -2
  96. package/lib/objectHash.d.ts +0 -3
  97. package/lib/openInBrowser.d.ts +0 -1
  98. package/lib/parseCSSImport.d.ts +0 -1
  99. package/lib/path.d.ts +0 -8
  100. package/lib/prettifyTime.d.ts +0 -1
  101. package/lib/prettyDiagnostic.d.ts +0 -17
  102. package/lib/progress-message.d.ts +0 -3
  103. package/lib/relativeBundlePath.d.ts +0 -4
  104. package/lib/relativeUrl.d.ts +0 -1
  105. package/lib/replaceBundleReferences.d.ts +0 -39
  106. package/lib/schema.d.ts +0 -107
  107. package/lib/shared-buffer.d.ts +0 -2
  108. package/lib/sourcemap.d.ts +0 -16
  109. package/lib/stream.d.ts +0 -8
  110. package/lib/throttle.d.ts +0 -1
  111. package/lib/urlJoin.d.ts +0 -5
  112. package/src/Deferred.ts +0 -26
  113. package/src/throttle.ts +0 -13
  114. package/tsconfig.json +0 -4
@@ -1,7 +1,9 @@
1
+ // @flow strict-local
2
+
1
3
  export class DefaultMap<K, V> extends Map<K, V> {
2
- _getDefault: (arg1: K) => V;
4
+ _getDefault: (K) => V;
3
5
 
4
- constructor(getDefault: (arg1: K) => V, entries?: Iterable<[K, V]>) {
6
+ constructor(getDefault: (K) => V, entries?: Iterable<[K, V]>) {
5
7
  super(entries);
6
8
  this._getDefault = getDefault;
7
9
  }
@@ -15,7 +17,8 @@ export class DefaultMap<K, V> extends Map<K, V> {
15
17
  this.set(key, ret);
16
18
  }
17
19
 
18
- return ret as V;
20
+ // $FlowFixMe
21
+ return ret;
19
22
  }
20
23
  }
21
24
 
@@ -23,11 +26,11 @@ interface Key {}
23
26
 
24
27
  // Duplicated from DefaultMap implementation for Flow
25
28
  // Roughly mirrors https://github.com/facebook/flow/blob/2eb5a78d92c167117ba9caae070afd2b9f598599/lib/core.js#L617
26
- export class DefaultWeakMap<K extends Key, V> extends WeakMap<K, V> {
27
- _getDefault: (arg1: K) => V;
29
+ export class DefaultWeakMap<K: Key, V> extends WeakMap<K, V> {
30
+ _getDefault: (K) => V;
28
31
 
29
- constructor(getDefault: (arg1: K) => V, entries?: Iterable<[K, V]>) {
30
- super(entries as Iterable<readonly [K, V]>);
32
+ constructor(getDefault: (K) => V, entries?: Iterable<[K, V]>) {
33
+ super(entries);
31
34
  this._getDefault = getDefault;
32
35
  }
33
36
 
@@ -40,6 +43,7 @@ export class DefaultWeakMap<K extends Key, V> extends WeakMap<K, V> {
40
43
  this.set(key, ret);
41
44
  }
42
45
 
43
- return ret as V;
46
+ // $FlowFixMe
47
+ return ret;
44
48
  }
45
49
  }
@@ -0,0 +1,23 @@
1
+ // @flow strict-local
2
+
3
+ import invariant from 'assert';
4
+
5
+ export type Deferred<T> = {|
6
+ resolve(T): void,
7
+ reject(mixed): void,
8
+ |};
9
+
10
+ export function makeDeferredWithPromise<T>(): {|
11
+ deferred: Deferred<T>,
12
+ promise: Promise<T>,
13
+ |} {
14
+ let deferred: ?Deferred<T>;
15
+ let promise = new Promise<T>((resolve, reject) => {
16
+ deferred = {resolve, reject};
17
+ });
18
+
19
+ // Promise constructor callback executes synchronously, so this is defined
20
+ invariant(deferred != null);
21
+
22
+ return {deferred, promise};
23
+ }
@@ -1,16 +1,16 @@
1
+ // @flow strict-local
2
+
1
3
  import {makeDeferredWithPromise, type Deferred} from './Deferred';
2
4
 
3
- type PromiseQueueOpts = {
4
- maxConcurrent: number;
5
- };
5
+ type PromiseQueueOpts = {|maxConcurrent: number|};
6
6
 
7
7
  export default class PromiseQueue<T> {
8
- _deferred: Deferred<Array<T>> | null | undefined;
8
+ _deferred: ?Deferred<Array<T>>;
9
9
  _maxConcurrent: number;
10
10
  _numRunning: number = 0;
11
11
  _queue: Array<() => Promise<void>> = [];
12
- _runPromise: Promise<Array<T>> | null | undefined = null;
13
- _error: unknown;
12
+ _runPromise: ?Promise<Array<T>> = null;
13
+ _error: mixed;
14
14
  _count: number = 0;
15
15
  _results: Array<T> = [];
16
16
  _addSubscriptions: Set<() => void> = new Set();
@@ -28,35 +28,30 @@ export default class PromiseQueue<T> {
28
28
  }
29
29
 
30
30
  add(fn: () => Promise<T>): void {
31
- new Promise(
32
- (
33
- resolve: (result: Promise<T> | T) => void,
34
- reject: (error?: any) => void,
35
- ) => {
36
- let i = this._count++;
37
- let wrapped = () =>
38
- fn().then(
39
- (result) => {
40
- this._results[i] = result;
41
- resolve(result);
42
- },
43
- (err) => {
44
- reject(err);
45
- throw err;
46
- },
47
- );
48
-
49
- this._queue.push(wrapped);
50
-
51
- for (const addFn of this._addSubscriptions) {
52
- addFn();
53
- }
54
-
55
- if (this._numRunning > 0 && this._numRunning < this._maxConcurrent) {
56
- this._next();
57
- }
58
- },
59
- ).catch(() => {});
31
+ new Promise((resolve, reject) => {
32
+ let i = this._count++;
33
+ let wrapped = () =>
34
+ fn().then(
35
+ (result) => {
36
+ this._results[i] = result;
37
+ resolve(result);
38
+ },
39
+ (err) => {
40
+ reject(err);
41
+ throw err;
42
+ },
43
+ );
44
+
45
+ this._queue.push(wrapped);
46
+
47
+ for (const addFn of this._addSubscriptions) {
48
+ addFn();
49
+ }
50
+
51
+ if (this._numRunning > 0 && this._numRunning < this._maxConcurrent) {
52
+ this._next();
53
+ }
54
+ }).catch(() => {});
60
55
  }
61
56
 
62
57
  subscribeToAdd(fn: () => void): () => void {
@@ -76,9 +71,9 @@ export default class PromiseQueue<T> {
76
71
  return Promise.resolve([]);
77
72
  }
78
73
 
79
- let {deferred, promise} = makeDeferredWithPromise<Array<T>>();
74
+ let {deferred, promise} = makeDeferredWithPromise();
80
75
  this._deferred = deferred;
81
- this._runPromise = promise as Promise<Array<T>>;
76
+ this._runPromise = promise;
82
77
 
83
78
  while (this._queue.length && this._numRunning < this._maxConcurrent) {
84
79
  this._next();
@@ -89,7 +84,7 @@ export default class PromiseQueue<T> {
89
84
 
90
85
  async _next(): Promise<void> {
91
86
  let fn = this._queue.shift();
92
- await this._runFn(fn as () => unknown);
87
+ await this._runFn(fn);
93
88
  if (this._queue.length) {
94
89
  this._next();
95
90
  } else if (this._numRunning === 0) {
@@ -97,11 +92,11 @@ export default class PromiseQueue<T> {
97
92
  }
98
93
  }
99
94
 
100
- async _runFn(fn: () => unknown): Promise<void> {
95
+ async _runFn(fn: () => mixed): Promise<void> {
101
96
  this._numRunning++;
102
97
  try {
103
98
  await fn();
104
- } catch (e: any) {
99
+ } catch (e) {
105
100
  // Only store the first error that occurs.
106
101
  // We don't reject immediately so that any other concurrent
107
102
  // requests have time to complete.
@@ -1,3 +1,5 @@
1
+ // @flow strict-local
2
+
1
3
  import {Transform} from 'stream';
2
4
 
3
5
  /*
@@ -5,24 +7,21 @@ import {Transform} from 'stream';
5
7
  * callback. Continues to pass data chunks down the stream.
6
8
  */
7
9
  export default class TapStream extends Transform {
8
- _tap: (arg1: Buffer) => unknown;
9
- constructor(tap: (arg1: Buffer) => unknown, options: unknown) {
10
- super({...(options as any)});
10
+ _tap: (Buffer) => mixed;
11
+ constructor(tap: (Buffer) => mixed, options: mixed) {
12
+ super({...options});
11
13
  this._tap = tap;
12
14
  }
13
15
 
14
16
  _transform(
15
17
  chunk: Buffer | string,
16
18
  encoding: string,
17
- callback: (
18
- err?: Error | null | undefined,
19
- chunk?: Buffer | string,
20
- ) => unknown,
19
+ callback: (err: ?Error, chunk?: Buffer | string) => mixed,
21
20
  ) {
22
21
  try {
23
22
  this._tap(Buffer.from(chunk));
24
23
  callback(null, chunk);
25
- } catch (err: any) {
24
+ } catch (err) {
26
25
  callback(err);
27
26
  }
28
27
  }
@@ -1,3 +1,4 @@
1
+ // @flow
1
2
  import path from 'path';
2
3
  import type {FileSystem} from '@atlaspack/types';
3
4
  import {fuzzySearch} from './schema';
@@ -47,7 +48,7 @@ export async function findAlternativeNodeModules(
47
48
  potentialModules.push(...modules);
48
49
  }
49
50
  }
50
- } catch (err: any) {
51
+ } catch (err) {
51
52
  // ignore
52
53
  }
53
54
 
@@ -67,16 +68,16 @@ async function findAllFilesUp({
67
68
  collected,
68
69
  leadingDotSlash = true,
69
70
  includeDirectories = true,
70
- }: {
71
- fs: FileSystem;
72
- dir: string;
73
- root: string;
74
- basedir: string;
75
- maxlength: number;
76
- collected: Array<string>;
77
- leadingDotSlash?: boolean;
78
- includeDirectories?: boolean;
79
- }): Promise<unknown> {
71
+ }: {|
72
+ fs: FileSystem,
73
+ dir: string,
74
+ root: string,
75
+ basedir: string,
76
+ maxlength: number,
77
+ collected: Array<string>,
78
+ leadingDotSlash?: boolean,
79
+ includeDirectories?: boolean,
80
+ |}): Promise<mixed> {
80
81
  let dirContent = (await fs.readdir(dir)).sort();
81
82
  return Promise.all(
82
83
  dirContent.map(async (item) => {
@@ -110,9 +111,9 @@ export async function findAlternativeFiles(
110
111
  fileSpecifier: string,
111
112
  dir: string,
112
113
  projectRoot: string,
113
- leadingDotSlash: boolean = true,
114
- includeDirectories: boolean = true,
115
- includeExtension: boolean = false,
114
+ leadingDotSlash?: boolean = true,
115
+ includeDirectories?: boolean = true,
116
+ includeExtension?: boolean = false,
116
117
  ): Promise<Array<string>> {
117
118
  let potentialFiles: Array<string> = [];
118
119
  // Find our root, we won't recommend files above the package root as that's bad practise
@@ -1,4 +1,4 @@
1
- // @ts-expect-error ansi-html-community is not typed
1
+ // @flow strict-local
2
2
  import ansiHTML from 'ansi-html-community';
3
3
  import {escapeHTML} from './escape-html';
4
4
 
@@ -1,3 +1,5 @@
1
+ // @flow strict-local
2
+
1
3
  import type {Blob} from '@atlaspack/types';
2
4
 
3
5
  import {Buffer} from 'buffer';
@@ -10,7 +12,7 @@ export function blobToBuffer(blob: Blob): Promise<Buffer> {
10
12
  } else if (blob instanceof Buffer) {
11
13
  return Promise.resolve(Buffer.from(blob));
12
14
  } else {
13
- return Promise.resolve(Buffer.from(blob as string, 'utf8'));
15
+ return Promise.resolve(Buffer.from(blob, 'utf8'));
14
16
  }
15
17
  }
16
18
 
@@ -20,6 +22,6 @@ export async function blobToString(blob: Blob): Promise<string> {
20
22
  } else if (blob instanceof Buffer) {
21
23
  return blob.toString();
22
24
  } else {
23
- return blob as string;
25
+ return blob;
24
26
  }
25
27
  }
@@ -1,4 +1,6 @@
1
- let bundleURL: string | null | undefined = null;
1
+ // @flow strict-local
2
+
3
+ let bundleURL: ?string = null;
2
4
  function getBundleURLCached(): string {
3
5
  if (bundleURL == null) {
4
6
  bundleURL = _getBundleURL();
@@ -11,7 +13,7 @@ function _getBundleURL(): string {
11
13
  // Attempt to find the URL of the current script and use that as the base URL
12
14
  try {
13
15
  throw new Error();
14
- } catch (err: any) {
16
+ } catch (err) {
15
17
  let stack: string = typeof err.stack === 'string' ? err.stack : '';
16
18
  let matches = stack.match(/(https?|file|ftp):\/\/[^)\n]+/g);
17
19
  if (matches) {
@@ -22,7 +24,7 @@ function _getBundleURL(): string {
22
24
  return '/';
23
25
  }
24
26
 
25
- export function getBaseURL(url?: string | null): string {
27
+ export function getBaseURL(url: ?string): string {
26
28
  if (url == null) {
27
29
  return '/';
28
30
  }
@@ -1,16 +1,20 @@
1
+ // @flow strict-local
2
+
1
3
  export function unique<T>(array: Array<T>): Array<T> {
2
4
  return [...new Set(array)];
3
5
  }
4
6
 
5
7
  export function objectSortedEntries(obj: {
6
- readonly [key: string]: unknown;
7
- }): Array<[string, unknown]> {
8
+ +[string]: mixed,
9
+ ...
10
+ }): Array<[string, mixed]> {
8
11
  return Object.entries(obj).sort(([keyA], [keyB]) => keyA.localeCompare(keyB));
9
12
  }
10
13
 
11
14
  export function objectSortedEntriesDeep(object: {
12
- readonly [key: string]: unknown;
13
- }): Array<[string, unknown]> {
15
+ +[string]: mixed,
16
+ ...
17
+ }): Array<[string, mixed]> {
14
18
  let sortedEntries = objectSortedEntries(object);
15
19
  for (let i = 0; i < sortedEntries.length; i++) {
16
20
  sortedEntries[i][1] = sortEntry(sortedEntries[i][1]);
@@ -18,13 +22,13 @@ export function objectSortedEntriesDeep(object: {
18
22
  return sortedEntries;
19
23
  }
20
24
 
21
- function sortEntry(entry: unknown): unknown {
25
+ function sortEntry(entry: mixed) {
22
26
  if (Array.isArray(entry)) {
23
27
  return entry.map(sortEntry);
24
28
  }
25
29
 
26
30
  if (typeof entry === 'object' && entry != null) {
27
- return objectSortedEntriesDeep(entry as {readonly [key: string]: unknown});
31
+ return objectSortedEntriesDeep(entry);
28
32
  }
29
33
 
30
34
  return entry;
@@ -40,8 +44,11 @@ function sortEntry(entry: unknown): unknown {
40
44
  * @param {*} b Set B
41
45
  * @returns A \ B
42
46
  */
43
- export function setDifference<T>(a: ReadonlySet<T>, b: ReadonlySet<T>): Set<T> {
44
- let difference = new Set<T>();
47
+ export function setDifference<T>(
48
+ a: $ReadOnlySet<T>,
49
+ b: $ReadOnlySet<T>,
50
+ ): Set<T> {
51
+ let difference = new Set();
45
52
  for (let e of a) {
46
53
  if (!b.has(e)) {
47
54
  difference.add(e);
@@ -62,10 +69,10 @@ export function setDifference<T>(a: ReadonlySet<T>, b: ReadonlySet<T>): Set<T> {
62
69
  * @returns A Δ B
63
70
  */
64
71
  export function setSymmetricDifference<T>(
65
- a: ReadonlySet<T>,
66
- b: ReadonlySet<T>,
72
+ a: $ReadOnlySet<T>,
73
+ b: $ReadOnlySet<T>,
67
74
  ): Set<T> {
68
- let difference = new Set<T>();
75
+ let difference = new Set();
69
76
  for (let e of a) {
70
77
  if (!b.has(e)) {
71
78
  difference.add(e);
@@ -79,7 +86,7 @@ export function setSymmetricDifference<T>(
79
86
  return difference;
80
87
  }
81
88
 
82
- export function setIntersect<T>(a: Set<T>, b: ReadonlySet<T>): void {
89
+ export function setIntersect<T>(a: Set<T>, b: $ReadOnlySet<T>): void {
83
90
  for (let entry of a) {
84
91
  if (!b.has(entry)) {
85
92
  a.delete(entry);
@@ -88,7 +95,7 @@ export function setIntersect<T>(a: Set<T>, b: ReadonlySet<T>): void {
88
95
  }
89
96
 
90
97
  export function setIntersectStatic<T>(a: Set<T>, b: Set<T>): Set<T> {
91
- let intersection = new Set<T>();
98
+ let intersection = new Set();
92
99
  for (let entry of a) {
93
100
  if (b.has(entry)) {
94
101
  intersection.add(entry);
@@ -101,7 +108,7 @@ export function setUnion<T>(a: Iterable<T>, b: Iterable<T>): Set<T> {
101
108
  return new Set([...a, ...b]);
102
109
  }
103
110
 
104
- export function setEqual<T>(a: ReadonlySet<T>, b: ReadonlySet<T>): boolean {
111
+ export function setEqual<T>(a: $ReadOnlySet<T>, b: $ReadOnlySet<T>): boolean {
105
112
  if (a.size != b.size) {
106
113
  return false;
107
114
  }
@@ -1,24 +1,24 @@
1
+ // @flow
2
+
1
3
  import type {ConfigResult, File, FilePath} from '@atlaspack/types';
2
4
  import type {FileSystem} from '@atlaspack/fs';
3
5
  import ThrowableDiagnostic from '@atlaspack/diagnostic';
4
6
  import path from 'path';
5
- // @ts-expect-error TS7016
6
7
  import clone from 'clone';
7
8
  import json5 from 'json5';
8
9
  import {parse as toml} from '@iarna/toml';
9
10
  import LRU from 'lru-cache';
10
11
 
11
- export type ConfigOutput = {
12
- config: ConfigResult;
13
- files: Array<File>;
14
- };
12
+ export type ConfigOutput = {|
13
+ config: ConfigResult,
14
+ files: Array<File>,
15
+ |};
15
16
 
16
- export type ConfigOptions = {
17
- parse?: boolean;
18
- parser?: (arg1: string) => any;
19
- };
17
+ export type ConfigOptions = {|
18
+ parse?: boolean,
19
+ parser?: (string) => any,
20
+ |};
20
21
 
21
- // @ts-expect-error TS2351
22
22
  const configCache = new LRU<FilePath, ConfigOutput>({max: 500});
23
23
  const resolveCache = new Map();
24
24
 
@@ -27,7 +27,7 @@ export function resolveConfig(
27
27
  filepath: FilePath,
28
28
  filenames: Array<FilePath>,
29
29
  projectRoot: FilePath,
30
- ): Promise<FilePath | null | undefined> {
30
+ ): Promise<?FilePath> {
31
31
  // Cache the result of resolving config for this directory.
32
32
  // This is automatically invalidated at the end of the current build.
33
33
  let key = path.dirname(filepath) + filenames.join(',');
@@ -50,7 +50,7 @@ export function resolveConfigSync(
50
50
  filepath: FilePath,
51
51
  filenames: Array<FilePath>,
52
52
  projectRoot: FilePath,
53
- ): FilePath | null | undefined {
53
+ ): ?FilePath {
54
54
  return fs.findAncestorFile(filenames, path.dirname(filepath), projectRoot);
55
55
  }
56
56
 
@@ -59,7 +59,7 @@ export async function loadConfig(
59
59
  filepath: FilePath,
60
60
  filenames: Array<FilePath>,
61
61
  projectRoot: FilePath,
62
- opts?: ConfigOptions | null,
62
+ opts: ?ConfigOptions,
63
63
  ): Promise<ConfigOutput | null> {
64
64
  let parse = opts?.parse ?? true;
65
65
  let configFile = await resolveConfig(fs, filepath, filenames, projectRoot);
@@ -73,6 +73,7 @@ export async function loadConfig(
73
73
  let extname = path.extname(configFile).slice(1);
74
74
  if (extname === 'js' || extname === 'cjs') {
75
75
  let output = {
76
+ // $FlowFixMe
76
77
  config: clone(module.require(configFile)),
77
78
  files: [{filePath: configFile}],
78
79
  };
@@ -82,7 +83,7 @@ export async function loadConfig(
82
83
  }
83
84
 
84
85
  return readConfig(fs, configFile, opts);
85
- } catch (err: any) {
86
+ } catch (err) {
86
87
  if (err.code === 'MODULE_NOT_FOUND' || err.code === 'ENOENT') {
87
88
  return null;
88
89
  }
@@ -102,7 +103,7 @@ loadConfig.clear = () => {
102
103
  export async function readConfig(
103
104
  fs: FileSystem,
104
105
  configFile: FilePath,
105
- opts?: ConfigOptions | null,
106
+ opts: ?ConfigOptions,
106
107
  ): Promise<ConfigOutput | null> {
107
108
  let parse = opts?.parse ?? true;
108
109
  let cachedOutput = configCache.get(String(parse) + configFile);
@@ -120,7 +121,7 @@ export async function readConfig(
120
121
  let parse = opts?.parser ?? getParser(extname);
121
122
  try {
122
123
  config = parse(configContent);
123
- } catch (e: any) {
124
+ } catch (e) {
124
125
  if (extname !== '' && extname !== 'json') {
125
126
  throw e;
126
127
  }
@@ -160,7 +161,7 @@ export async function readConfig(
160
161
 
161
162
  configCache.set(String(parse) + configFile, output);
162
163
  return output;
163
- } catch (err: any) {
164
+ } catch (err) {
164
165
  if (err.code === 'MODULE_NOT_FOUND' || err.code === 'ENOENT') {
165
166
  return null;
166
167
  }
@@ -169,7 +170,7 @@ export async function readConfig(
169
170
  }
170
171
  }
171
172
 
172
- function getParser(extname: string) {
173
+ function getParser(extname) {
173
174
  switch (extname) {
174
175
  case 'toml':
175
176
  return toml;
@@ -1,3 +1,5 @@
1
+ // @flow strict-local
2
+
1
3
  export default function countLines(
2
4
  string: string,
3
5
  startIndex: number = 0,
@@ -1,8 +1,10 @@
1
- export default function debounce<TArgs extends Array<unknown>>(
2
- fn: (...args: TArgs) => unknown,
1
+ // @flow strict-local
2
+
3
+ export default function debounce<TArgs: Array<mixed>>(
4
+ fn: (...args: TArgs) => mixed,
3
5
  delay: number,
4
6
  ): (...args: TArgs) => void {
5
- let timeout: NodeJS.Timeout | undefined | null;
7
+ let timeout;
6
8
 
7
9
  return function (...args: TArgs) {
8
10
  if (timeout) {
@@ -1,3 +1,5 @@
1
+ // @flow
2
+
1
3
  /*
2
4
  * These tools are intended for Atlaspack developers, to provide extra utilities
3
5
  * to make debugging Atlaspack issues more straightforward.
@@ -9,10 +11,10 @@
9
11
  * You can enable all tools by setting `ATLASPACK_DEBUG_TOOLS=all`.
10
12
  */
11
13
 
12
- type DebugTools = {
13
- ['asset-file-names-in-output']: boolean;
14
- ['simple-cli-reporter']: boolean;
15
- };
14
+ type DebugTools = {|
15
+ 'asset-file-names-in-output': boolean,
16
+ 'simple-cli-reporter': boolean,
17
+ |};
16
18
 
17
19
  export let debugTools: DebugTools = {
18
20
  'asset-file-names-in-output': false,
@@ -26,11 +28,11 @@ for (let tool of envVarValue.split(',')) {
26
28
 
27
29
  if (tool === 'all') {
28
30
  for (let key in debugTools) {
29
- debugTools[key as keyof DebugTools] = true;
31
+ debugTools[key] = true;
30
32
  }
31
33
  break;
32
34
  } else if (debugTools.hasOwnProperty(tool)) {
33
- debugTools[tool as keyof DebugTools] = true;
35
+ debugTools[tool] = true;
34
36
  } else if (tool === '') {
35
37
  continue;
36
38
  } else {
@@ -1,24 +1,20 @@
1
+ // @flow
2
+
1
3
  export default function createDependencyLocation(
2
- start: {
3
- line: number;
4
- column: number;
5
- },
4
+ start: {|
5
+ line: number,
6
+ column: number,
7
+ |},
6
8
  specifier: string,
7
9
  lineOffset: number = 0,
8
10
  columnOffset: number = 0,
9
11
  // Imports are usually wrapped in quotes
10
12
  importWrapperLength: number = 2,
11
- ): {
12
- end: {
13
- column: number;
14
- line: number;
15
- };
16
- filePath: string;
17
- start: {
18
- column: number;
19
- line: number;
20
- };
21
- } {
13
+ ): {|
14
+ end: {|column: number, line: number|},
15
+ filePath: string,
16
+ start: {|column: number, line: number|},
17
+ |} {
22
18
  return {
23
19
  filePath: specifier,
24
20
  start: {
@@ -1,3 +1,4 @@
1
+ // @flow
1
2
  // Based on _.escape https://github.com/lodash/lodash/blob/master/escape.js
2
3
  const reUnescapedHtml = /[&<>"']/g;
3
4
  const reHasUnescapedHtml = RegExp(reUnescapedHtml.source);
@@ -8,14 +9,11 @@ const htmlEscapes = {
8
9
  '>': '&gt;',
9
10
  '"': '&quot;',
10
11
  "'": '&#39;',
11
- } as const;
12
+ };
12
13
 
13
14
  export function escapeHTML(s: string): string {
14
15
  if (reHasUnescapedHtml.test(s)) {
15
- return s.replace(
16
- reUnescapedHtml,
17
- (c) => htmlEscapes[c as keyof typeof htmlEscapes],
18
- );
16
+ return s.replace(reUnescapedHtml, (c) => htmlEscapes[c]);
19
17
  }
20
18
 
21
19
  return s;