@flex-development/mlly 1.0.0-alpha.16 → 1.0.0-alpha.17
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/CHANGELOG.md +14 -0
- package/dist/index.mjs.map +4 -5
- package/dist/internal/dequote.mjs +1 -0
- package/dist/internal/dequote.mjs.map +8 -0
- package/dist/internal/format-type-map.mjs +1 -0
- package/dist/internal/format-type-map.mjs.map +8 -0
- package/dist/internal/get-specifier-kind.mjs +1 -0
- package/dist/internal/get-specifier-kind.mjs.map +8 -0
- package/dist/internal/get-subpaths.mjs +1 -0
- package/dist/internal/get-subpaths.mjs.map +8 -0
- package/dist/internal/regex-encoded-sep.mjs +1 -0
- package/dist/internal/regex-encoded-sep.mjs.map +8 -0
- package/dist/internal/regex-internal-specifier.mjs +1 -0
- package/dist/internal/regex-internal-specifier.mjs.map +8 -0
- package/dist/internal/regex-invalid-segment.mjs +1 -0
- package/dist/internal/regex-invalid-segment.mjs.map +8 -0
- package/dist/internal/regex-package-name.mjs +1 -0
- package/dist/internal/regex-package-name.mjs.map +8 -0
- package/dist/internal/regex-package-path.mjs +1 -0
- package/dist/internal/regex-package-path.mjs.map +8 -0
- package/dist/internal/resolver.mjs +1 -0
- package/dist/internal/resolver.mjs.map +8 -0
- package/dist/internal/validate-array-set.mjs +1 -0
- package/dist/internal/validate-array-set.mjs.map +8 -0
- package/dist/internal/validate-boolean.mjs +1 -0
- package/dist/internal/validate-boolean.mjs.map +8 -0
- package/dist/internal/validate-map.mjs +1 -0
- package/dist/internal/validate-map.mjs.map +8 -0
- package/dist/internal/validate-object.mjs +1 -0
- package/dist/internal/validate-object.mjs.map +8 -0
- package/dist/internal/validate-set.mjs +1 -0
- package/dist/internal/validate-set.mjs.map +8 -0
- package/dist/internal/validate-string.mjs +1 -0
- package/dist/internal/validate-string.mjs.map +8 -0
- package/dist/internal/validate-url-string.mjs +1 -0
- package/dist/internal/validate-url-string.mjs.map +8 -0
- package/dist/utils/compare-subpaths.mjs.map +4 -5
- package/dist/utils/conditions.mjs.map +4 -5
- package/dist/utils/detect-syntax.mjs.map +4 -5
- package/dist/utils/extension-format-map.mjs.map +4 -5
- package/dist/utils/extract-statements.mjs.map +4 -5
- package/dist/utils/fill-modules.mjs.map +5 -8
- package/dist/utils/find-dynamic-imports.mjs.map +5 -8
- package/dist/utils/find-exports.mjs.map +5 -8
- package/dist/utils/find-requires.mjs.map +5 -8
- package/dist/utils/find-static-imports.mjs.map +5 -8
- package/dist/utils/find-subpath.mjs.map +5 -9
- package/dist/utils/get-format.mjs.map +5 -8
- package/dist/utils/get-source.mjs.map +4 -5
- package/dist/utils/has-cjs-syntax.mjs.map +4 -5
- package/dist/utils/has-esm-syntax.mjs.map +4 -5
- package/dist/utils/index.mjs.map +5 -8
- package/dist/utils/is-absolute-specifier.mjs.map +4 -5
- package/dist/utils/is-bare-specifier.mjs.map +4 -5
- package/dist/utils/is-directory.mjs.map +4 -5
- package/dist/utils/is-exports-sugar.mjs.map +4 -5
- package/dist/utils/is-file.mjs.map +4 -5
- package/dist/utils/is-relative-specifier.mjs.map +4 -5
- package/dist/utils/lookup-package-scope.mjs.map +4 -5
- package/dist/utils/parse-data-url.mjs.map +4 -5
- package/dist/utils/parse-module-id.mjs.map +5 -9
- package/dist/utils/parse-subpath.mjs.map +5 -8
- package/dist/utils/pattern-character.mjs.map +4 -5
- package/dist/utils/read-package-json.mjs.map +4 -5
- package/dist/utils/resolve-alias.mjs.map +5 -9
- package/dist/utils/resolve-aliases.mjs.map +4 -5
- package/dist/utils/resolve-extensions.mjs.map +4 -5
- package/dist/utils/resolve-module.mjs.map +4 -5
- package/dist/utils/resolve-modules.mjs.map +4 -5
- package/dist/utils/to-absolute-specifier.mjs.map +4 -5
- package/dist/utils/to-bare-specifier.mjs.map +4 -5
- package/dist/utils/to-data-url.mjs.map +4 -5
- package/dist/utils/to-node-url.mjs.map +4 -5
- package/dist/utils/to-relative-specifier.mjs.map +4 -5
- package/dist/utils/to-url.mjs.map +4 -5
- package/dist/utils/validate-assertions.mjs.map +5 -8
- package/dist/utils/validate-exports.mjs.map +4 -5
- package/package.json +8 -9
- package/src/enums/assert-type.ts +0 -23
- package/src/enums/format.ts +0 -19
- package/src/enums/index.ts +0 -11
- package/src/enums/kind-specifier-syntax.ts +0 -16
- package/src/enums/kind-specifier.ts +0 -19
- package/src/enums/kind-statement-syntax.ts +0 -24
- package/src/enums/kind-statement.ts +0 -17
- package/src/index.ts +0 -9
- package/src/interfaces/import-assertions.ts +0 -24
- package/src/interfaces/import-dynamic.ts +0 -46
- package/src/interfaces/import-static.ts +0 -46
- package/src/interfaces/index.ts +0 -25
- package/src/interfaces/options-fill-module.ts +0 -26
- package/src/interfaces/options-find-subpath.ts +0 -57
- package/src/interfaces/options-get-format.ts +0 -80
- package/src/interfaces/options-get-source.ts +0 -59
- package/src/interfaces/options-parse-module-id.ts +0 -39
- package/src/interfaces/options-parse-subpath.ts +0 -57
- package/src/interfaces/options-resolve-alias.ts +0 -59
- package/src/interfaces/options-resolve-module.ts +0 -72
- package/src/interfaces/options-resolve.ts +0 -17
- package/src/interfaces/package-scope.ts +0 -30
- package/src/interfaces/parsed-data-url.ts +0 -63
- package/src/interfaces/parsed-module-id.ts +0 -90
- package/src/interfaces/parsed-subpath.ts +0 -56
- package/src/interfaces/statement-export.ts +0 -52
- package/src/interfaces/statement-import.ts +0 -45
- package/src/interfaces/statement-require.ts +0 -45
- package/src/interfaces/statement.ts +0 -64
- package/src/internal/dequote.ts +0 -18
- package/src/internal/format-type-map.ts +0 -28
- package/src/internal/get-specifier-kind.ts +0 -41
- package/src/internal/get-subpaths.ts +0 -43
- package/src/internal/regex-encoded-sep.ts +0 -17
- package/src/internal/regex-internal-specifier.ts +0 -19
- package/src/internal/regex-invalid-segment.ts +0 -25
- package/src/internal/regex-package-name.ts +0 -18
- package/src/internal/regex-package-path.ts +0 -20
- package/src/internal/resolver.ts +0 -790
- package/src/internal/validate-array-set.ts +0 -35
- package/src/internal/validate-boolean.ts +0 -30
- package/src/internal/validate-map.ts +0 -36
- package/src/internal/validate-object.ts +0 -30
- package/src/internal/validate-set.ts +0 -32
- package/src/internal/validate-string.ts +0 -31
- package/src/internal/validate-url-string.ts +0 -34
- package/src/types/declaration.ts +0 -22
- package/src/types/fn-change-ext.ts +0 -33
- package/src/types/index.ts +0 -14
- package/src/types/mime-type.ts +0 -19
- package/src/types/module-id.ts +0 -15
- package/src/types/module-specifier-type.ts +0 -13
- package/src/types/protocol.ts +0 -40
- package/src/types/syntax-kind-export.ts +0 -20
- package/src/types/syntax-kind-import.ts +0 -22
- package/src/types/syntax-kind-require.ts +0 -15
- package/src/utils/compare-subpaths.ts +0 -100
- package/src/utils/conditions.ts +0 -15
- package/src/utils/detect-syntax.ts +0 -40
- package/src/utils/extension-format-map.ts +0 -34
- package/src/utils/extract-statements.ts +0 -46
- package/src/utils/fill-modules.ts +0 -133
- package/src/utils/find-dynamic-imports.ts +0 -75
- package/src/utils/find-exports.ts +0 -156
- package/src/utils/find-requires.ts +0 -75
- package/src/utils/find-static-imports.ts +0 -86
- package/src/utils/find-subpath.ts +0 -251
- package/src/utils/get-format.ts +0 -249
- package/src/utils/get-source.ts +0 -144
- package/src/utils/has-cjs-syntax.ts +0 -44
- package/src/utils/has-esm-syntax.ts +0 -40
- package/src/utils/index.ts +0 -45
- package/src/utils/is-absolute-specifier.ts +0 -53
- package/src/utils/is-bare-specifier.ts +0 -36
- package/src/utils/is-directory.ts +0 -25
- package/src/utils/is-exports-sugar.ts +0 -81
- package/src/utils/is-file.ts +0 -25
- package/src/utils/is-relative-specifier.ts +0 -34
- package/src/utils/lookup-package-scope.ts +0 -97
- package/src/utils/parse-data-url.ts +0 -71
- package/src/utils/parse-module-id.ts +0 -197
- package/src/utils/parse-subpath.ts +0 -331
- package/src/utils/pattern-character.ts +0 -13
- package/src/utils/read-package-json.ts +0 -113
- package/src/utils/resolve-alias.ts +0 -224
- package/src/utils/resolve-aliases.ts +0 -58
- package/src/utils/resolve-extensions.ts +0 -33
- package/src/utils/resolve-module.ts +0 -181
- package/src/utils/resolve-modules.ts +0 -60
- package/src/utils/to-absolute-specifier.ts +0 -30
- package/src/utils/to-bare-specifier.ts +0 -211
- package/src/utils/to-data-url.ts +0 -42
- package/src/utils/to-node-url.ts +0 -27
- package/src/utils/to-relative-specifier.ts +0 -55
- package/src/utils/to-url.ts +0 -30
- package/src/utils/validate-assertions.ts +0 -126
- package/src/utils/validate-exports.ts +0 -131
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file extractStatements
|
|
3
|
-
* @module mlly/utils/extractStatements
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import type { Statement } from '#src/interfaces'
|
|
7
|
-
import validateString from '#src/internal/validate-string'
|
|
8
|
-
import type { NodeError } from '@flex-development/errnode'
|
|
9
|
-
import { sort, trim } from '@flex-development/tutils'
|
|
10
|
-
import findDynamicImports from './find-dynamic-imports'
|
|
11
|
-
import findExports from './find-exports'
|
|
12
|
-
import findRequires from './find-requires'
|
|
13
|
-
import findStaticImports from './find-static-imports'
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Finds all `export`, `import`, and/or `require` statements in `code`.
|
|
17
|
-
*
|
|
18
|
-
* Ignores matches in comments.
|
|
19
|
-
*
|
|
20
|
-
* @see {@linkcode findDynamicImports}
|
|
21
|
-
* @see {@linkcode findExports}
|
|
22
|
-
* @see {@linkcode findRequires}
|
|
23
|
-
* @see {@linkcode findStaticImports}
|
|
24
|
-
*
|
|
25
|
-
* @param {string} [code=''] - Code to evaluate
|
|
26
|
-
* @return {Statement[]} Extracted statements
|
|
27
|
-
* @throws {NodeError<TypeError>} If `code` is not a string
|
|
28
|
-
*/
|
|
29
|
-
const extractStatements = (code: string = ''): Statement[] => {
|
|
30
|
-
validateString(code, 'code')
|
|
31
|
-
|
|
32
|
-
// code shorter than first shortest keyword => no possible matches
|
|
33
|
-
if (trim(code).length < 'export'.length) return []
|
|
34
|
-
|
|
35
|
-
return sort(
|
|
36
|
-
[
|
|
37
|
-
...findDynamicImports(code),
|
|
38
|
-
...findExports(code),
|
|
39
|
-
...findRequires(code),
|
|
40
|
-
...findStaticImports(code)
|
|
41
|
-
],
|
|
42
|
-
(s1: Statement, s2: Statement): number => s1.start - s2.start
|
|
43
|
-
)
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export default extractStatements
|
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file fillModules
|
|
3
|
-
* @module mlly/utils/fillModules
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { SpecifierSyntaxKind } from '#src/enums'
|
|
7
|
-
import type { FillModuleOptions } from '#src/interfaces'
|
|
8
|
-
import validateArraySet from '#src/internal/validate-array-set'
|
|
9
|
-
import validateURLString from '#src/internal/validate-url-string'
|
|
10
|
-
import {
|
|
11
|
-
ERR_UNKNOWN_FILE_EXTENSION,
|
|
12
|
-
type NodeError
|
|
13
|
-
} from '@flex-development/errnode'
|
|
14
|
-
import pathe from '@flex-development/pathe'
|
|
15
|
-
import {
|
|
16
|
-
DOT,
|
|
17
|
-
isFunction,
|
|
18
|
-
regexp,
|
|
19
|
-
trim,
|
|
20
|
-
type Optional
|
|
21
|
-
} from '@flex-development/tutils'
|
|
22
|
-
import type { URL } from 'node:url'
|
|
23
|
-
import CONDITIONS from './conditions'
|
|
24
|
-
import extractStatements from './extract-statements'
|
|
25
|
-
import isAbsoluteSpecifier from './is-absolute-specifier'
|
|
26
|
-
import isBareSpecifier from './is-bare-specifier'
|
|
27
|
-
import resolveModule from './resolve-module'
|
|
28
|
-
import toBareSpecifier from './to-bare-specifier'
|
|
29
|
-
import toRelativeSpecifier from './to-relative-specifier'
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Ensures all absolute and relative module specifiers in the given piece of
|
|
33
|
-
* source `code` are fully specified.
|
|
34
|
-
*
|
|
35
|
-
* Ignores specifiers that already have file extensions.
|
|
36
|
-
*
|
|
37
|
-
* @see {@linkcode FillModuleOptions}
|
|
38
|
-
* @see https://nodejs.org/api/esm.html#mandatory-file-extensions
|
|
39
|
-
* @see https://nodejs.org/api/esm.html#terminology
|
|
40
|
-
*
|
|
41
|
-
* @async
|
|
42
|
-
*
|
|
43
|
-
* @param {string} code - Code to evaluate
|
|
44
|
-
* @param {FillModuleOptions} options - Module fill options
|
|
45
|
-
* @return {Promise<string>} `code` with fully specified module specifiers
|
|
46
|
-
* @throws {NodeError<TypeError>}
|
|
47
|
-
*/
|
|
48
|
-
const fillModules = async (
|
|
49
|
-
code: string,
|
|
50
|
-
options: FillModuleOptions
|
|
51
|
-
): Promise<string> => {
|
|
52
|
-
const { conditions = CONDITIONS, ext, parent = import.meta.url } = options
|
|
53
|
-
|
|
54
|
-
// validate options
|
|
55
|
-
validateArraySet(conditions, 'options.conditions')
|
|
56
|
-
validateURLString(parent, 'options.parent')
|
|
57
|
-
|
|
58
|
-
// ensure specifiers have file extensions
|
|
59
|
-
for (const statement of extractStatements(code)) {
|
|
60
|
-
if (statement.specifier) {
|
|
61
|
-
if (statement.specifier_syntax !== SpecifierSyntaxKind.DYNAMIC) {
|
|
62
|
-
/**
|
|
63
|
-
* Resolved module URL.
|
|
64
|
-
*
|
|
65
|
-
* @const {URL} url
|
|
66
|
-
*/
|
|
67
|
-
const url: URL = await resolveModule(statement.specifier, {
|
|
68
|
-
...options,
|
|
69
|
-
/**
|
|
70
|
-
* Returns a replacement file extension for the given `specifier` if
|
|
71
|
-
* it is non-bare and does not already have an extension.
|
|
72
|
-
*
|
|
73
|
-
* Throws [`ERR_UNKNOWN_FILE_EXTENSION`][1] if the new extension is
|
|
74
|
-
* `null`, `undefined`, an empty string, or a dot character (`'.'`).
|
|
75
|
-
*
|
|
76
|
-
* [1]: https://nodejs.org/api/errors.html#err_unknown_file_extension
|
|
77
|
-
*
|
|
78
|
-
* @async
|
|
79
|
-
*
|
|
80
|
-
* @param {string} specifier - Module specifier
|
|
81
|
-
* @param {URL} url - Resolved module URL
|
|
82
|
-
* @return {Promise<Optional<string>>} New file extension
|
|
83
|
-
*/
|
|
84
|
-
async ext(specifier: string, url: URL): Promise<Optional<string>> {
|
|
85
|
-
// skip replacement for bare specifiers
|
|
86
|
-
if (isBareSpecifier(specifier)) return void 0
|
|
87
|
-
|
|
88
|
-
// skip replacement for specifiers that are already fully specified
|
|
89
|
-
if (pathe.extname(specifier) === pathe.extname(url.href)) {
|
|
90
|
-
return void 0
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Replacement file extension.
|
|
95
|
-
*
|
|
96
|
-
* @var {string} rext
|
|
97
|
-
*/
|
|
98
|
-
const rext: string = isFunction(ext)
|
|
99
|
-
? await ext(specifier, url)
|
|
100
|
-
: ext
|
|
101
|
-
|
|
102
|
-
// ensure replacement extension is non-empty and non-dot ('.')
|
|
103
|
-
if (!(rext && trim(rext).length > (rext.startsWith(DOT) ? 1 : 0))) {
|
|
104
|
-
throw new ERR_UNKNOWN_FILE_EXTENSION(rext, specifier)
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
return rext
|
|
108
|
-
}
|
|
109
|
-
})
|
|
110
|
-
|
|
111
|
-
// replace original module specifier
|
|
112
|
-
code = code.replace(
|
|
113
|
-
statement.code,
|
|
114
|
-
statement.code.replace(
|
|
115
|
-
new RegExp(`(?<=["'])${regexp(statement.specifier)}(?=["'])`),
|
|
116
|
-
// convert module url back to absolute, bare, or relative specifier
|
|
117
|
-
statement.specifier.startsWith('#')
|
|
118
|
-
? statement.specifier
|
|
119
|
-
: isAbsoluteSpecifier(statement.specifier)
|
|
120
|
-
? url.href
|
|
121
|
-
: isBareSpecifier(statement.specifier)
|
|
122
|
-
? toBareSpecifier(url, parent, new Set(conditions))
|
|
123
|
-
: toRelativeSpecifier(url, parent)
|
|
124
|
-
)
|
|
125
|
-
)
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
return code
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
export default fillModules
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file findDynamicImports
|
|
3
|
-
* @module mlly/utils/findDynamicImports
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import {
|
|
7
|
-
SpecifierSyntaxKind,
|
|
8
|
-
StatementKind,
|
|
9
|
-
StatementSyntaxKind
|
|
10
|
-
} from '#src/enums'
|
|
11
|
-
import type { DynamicImport } from '#src/interfaces'
|
|
12
|
-
import dequote from '#src/internal/dequote'
|
|
13
|
-
import getSpecifierKind from '#src/internal/get-specifier-kind'
|
|
14
|
-
import validateString from '#src/internal/validate-string'
|
|
15
|
-
import type { NodeError } from '@flex-development/errnode'
|
|
16
|
-
import { DYNAMIC_IMPORT_REGEX } from '@flex-development/import-regex'
|
|
17
|
-
import { split, trim } from '@flex-development/tutils'
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Finds all dynamic import statements in `code`. Ignores matches in comments.
|
|
21
|
-
*
|
|
22
|
-
* @see {@linkcode DynamicImport}
|
|
23
|
-
* @see https://regex101.com/r/PTPAvU
|
|
24
|
-
* @see https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/import
|
|
25
|
-
*
|
|
26
|
-
* @param {string} [code=''] - Code to evaluate
|
|
27
|
-
* @return {DynamicImport[]} Dynamic import statement objects
|
|
28
|
-
* @throws {NodeError<TypeError>} If `code` is not a string
|
|
29
|
-
*/
|
|
30
|
-
const findDynamicImports = (code: string = ''): DynamicImport[] => {
|
|
31
|
-
validateString(code, 'code')
|
|
32
|
-
|
|
33
|
-
return [...code.matchAll(DYNAMIC_IMPORT_REGEX)].map(match => {
|
|
34
|
-
const { 0: code = '', index: start = 0, groups = {} } = match
|
|
35
|
-
const {
|
|
36
|
-
expression = '',
|
|
37
|
-
imports = '',
|
|
38
|
-
options = '',
|
|
39
|
-
specifier = ''
|
|
40
|
-
} = groups
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Module specifier syntax.
|
|
44
|
-
*
|
|
45
|
-
* @const {SpecifierSyntaxKind} specifier_syntax
|
|
46
|
-
*/
|
|
47
|
-
const specifier_syntax: SpecifierSyntaxKind = /^["']/.test(specifier)
|
|
48
|
-
? SpecifierSyntaxKind.STATIC
|
|
49
|
-
: SpecifierSyntaxKind.DYNAMIC
|
|
50
|
-
|
|
51
|
-
return {
|
|
52
|
-
code,
|
|
53
|
-
end: start + code.length,
|
|
54
|
-
expression,
|
|
55
|
-
imports:
|
|
56
|
-
imports === ''
|
|
57
|
-
? []
|
|
58
|
-
: split(imports.replace(/^{|}$/g, ''), ',')
|
|
59
|
-
.map(trim)
|
|
60
|
-
.filter(e => !!e.length),
|
|
61
|
-
kind: StatementKind.IMPORT,
|
|
62
|
-
options,
|
|
63
|
-
specifier: dequote(specifier),
|
|
64
|
-
specifier_kind:
|
|
65
|
-
specifier_syntax === SpecifierSyntaxKind.DYNAMIC
|
|
66
|
-
? null
|
|
67
|
-
: getSpecifierKind(specifier),
|
|
68
|
-
specifier_syntax,
|
|
69
|
-
start,
|
|
70
|
-
syntax: StatementSyntaxKind.DYNAMIC
|
|
71
|
-
}
|
|
72
|
-
})
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export default findDynamicImports
|
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file findExports
|
|
3
|
-
* @module mlly/utils/findExports
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import {
|
|
7
|
-
SpecifierSyntaxKind,
|
|
8
|
-
StatementKind,
|
|
9
|
-
StatementSyntaxKind
|
|
10
|
-
} from '#src/enums'
|
|
11
|
-
import type { ExportStatement } from '#src/interfaces'
|
|
12
|
-
import getSpecifierKind from '#src/internal/get-specifier-kind'
|
|
13
|
-
import validateString from '#src/internal/validate-string'
|
|
14
|
-
import type { NodeError } from '@flex-development/errnode'
|
|
15
|
-
import {
|
|
16
|
-
EXPORT_AGGREGATE_REGEX,
|
|
17
|
-
EXPORT_DECLARATION_REGEX,
|
|
18
|
-
EXPORT_DEFAULT_REGEX,
|
|
19
|
-
EXPORT_LIST_REGEX
|
|
20
|
-
} from '@flex-development/export-regex'
|
|
21
|
-
import { cast, sort, split, trim } from '@flex-development/tutils'
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Finds all export statements in `code`. Ignores matches in comments.
|
|
25
|
-
*
|
|
26
|
-
* @see {@linkcode ExportStatement}
|
|
27
|
-
* @see https://regex101.com/r/JtvRUt
|
|
28
|
-
* @see https://regex101.com/r/8HpMrA
|
|
29
|
-
* @see https://regex101.com/r/G7GhEt
|
|
30
|
-
* @see https://regex101.com/r/KQEDdZ
|
|
31
|
-
* @see https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/export
|
|
32
|
-
*
|
|
33
|
-
* @param {string} [code=''] - Code to evaluate
|
|
34
|
-
* @return {ExportStatement[]} Export statement objects
|
|
35
|
-
* @throws {NodeError<TypeError>} If `code` is not a string
|
|
36
|
-
*/
|
|
37
|
-
const findExports = (code: string = ''): ExportStatement[] => {
|
|
38
|
-
validateString(code, 'code')
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* `export` statement objects.
|
|
42
|
-
*
|
|
43
|
-
* @const {ExportStatement[]} statements
|
|
44
|
-
*/
|
|
45
|
-
const statements: ExportStatement[] = []
|
|
46
|
-
|
|
47
|
-
// get aggregate export statements
|
|
48
|
-
for (const match of code.matchAll(EXPORT_AGGREGATE_REGEX)) {
|
|
49
|
-
const { 0: code = '', index: start = 0, groups = {} } = match
|
|
50
|
-
const { exports = '', specifier = '', type = '' } = groups
|
|
51
|
-
|
|
52
|
-
statements.push({
|
|
53
|
-
code,
|
|
54
|
-
declaration: null,
|
|
55
|
-
end: start + code.length,
|
|
56
|
-
exports: exports.startsWith('*')
|
|
57
|
-
? [exports]
|
|
58
|
-
: split(exports.replace(/^{|}$/g, ''), ',')
|
|
59
|
-
.map(trim)
|
|
60
|
-
.filter(e => !!e.length),
|
|
61
|
-
kind: StatementKind.EXPORT,
|
|
62
|
-
modifiers: [],
|
|
63
|
-
specifier,
|
|
64
|
-
specifier_kind: getSpecifierKind(specifier),
|
|
65
|
-
specifier_syntax: SpecifierSyntaxKind.STATIC,
|
|
66
|
-
start,
|
|
67
|
-
syntax: exports.startsWith('{')
|
|
68
|
-
? StatementSyntaxKind.NAMED
|
|
69
|
-
: StatementSyntaxKind.NAMESPACE,
|
|
70
|
-
type: !!type
|
|
71
|
-
})
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// get declaration export statements
|
|
75
|
-
for (const match of code.matchAll(EXPORT_DECLARATION_REGEX)) {
|
|
76
|
-
const { 0: code = '', index: start = 0, groups = {} } = match
|
|
77
|
-
const { declaration = '', exports = '', modifiers = '' } = groups
|
|
78
|
-
|
|
79
|
-
statements.push({
|
|
80
|
-
code,
|
|
81
|
-
declaration: cast(declaration),
|
|
82
|
-
end: start + code.length,
|
|
83
|
-
exports: /^\w+$/.test(exports)
|
|
84
|
-
? [exports]
|
|
85
|
-
: split(exports.replace(/^[[{]|[\]}]$/g, ''), ',')
|
|
86
|
-
.map(e => trim(e).replace(/\s*=\s*.*$/, ''))
|
|
87
|
-
.filter(e => !!e.length),
|
|
88
|
-
kind: StatementKind.EXPORT,
|
|
89
|
-
modifiers:
|
|
90
|
-
modifiers === ''
|
|
91
|
-
? []
|
|
92
|
-
: split(modifiers, ' ')
|
|
93
|
-
.map(trim)
|
|
94
|
-
.filter(e => !!e.length),
|
|
95
|
-
specifier: null,
|
|
96
|
-
specifier_kind: null,
|
|
97
|
-
specifier_syntax: null,
|
|
98
|
-
start,
|
|
99
|
-
syntax: StatementSyntaxKind.DECLARATION,
|
|
100
|
-
type: false
|
|
101
|
-
})
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
// get export default statements
|
|
105
|
-
for (const match of code.matchAll(EXPORT_DEFAULT_REGEX)) {
|
|
106
|
-
const { 0: code = '', index: start = 0, groups = {} } = match
|
|
107
|
-
const { exports = '', kind: declaration = '', modifiers = '' } = groups
|
|
108
|
-
|
|
109
|
-
statements.push({
|
|
110
|
-
code,
|
|
111
|
-
declaration: declaration ? cast(declaration) : null,
|
|
112
|
-
end: start + code.length,
|
|
113
|
-
exports: exports === '' ? [] : [exports],
|
|
114
|
-
kind: StatementKind.EXPORT,
|
|
115
|
-
modifiers:
|
|
116
|
-
modifiers === ''
|
|
117
|
-
? []
|
|
118
|
-
: split(modifiers, ' ')
|
|
119
|
-
.map(trim)
|
|
120
|
-
.filter(e => !!e.length),
|
|
121
|
-
specifier: null,
|
|
122
|
-
specifier_kind: null,
|
|
123
|
-
specifier_syntax: null,
|
|
124
|
-
start,
|
|
125
|
-
syntax: StatementSyntaxKind.DEFAULT,
|
|
126
|
-
type: false
|
|
127
|
-
})
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
// get list export statements
|
|
131
|
-
for (const match of code.matchAll(EXPORT_LIST_REGEX)) {
|
|
132
|
-
const { 0: code = '', index: start = 0, groups = {} } = match
|
|
133
|
-
const { exports = '', type = '' } = groups
|
|
134
|
-
|
|
135
|
-
statements.push({
|
|
136
|
-
code,
|
|
137
|
-
declaration: null,
|
|
138
|
-
end: start + code.length,
|
|
139
|
-
exports: split(exports.replace(/^{|}$/g, ''), ',')
|
|
140
|
-
.map(trim)
|
|
141
|
-
.filter(e => !!e.length),
|
|
142
|
-
kind: StatementKind.EXPORT,
|
|
143
|
-
modifiers: [],
|
|
144
|
-
specifier: null,
|
|
145
|
-
specifier_kind: null,
|
|
146
|
-
specifier_syntax: null,
|
|
147
|
-
start,
|
|
148
|
-
syntax: StatementSyntaxKind.LIST,
|
|
149
|
-
type: !!type
|
|
150
|
-
})
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
return sort(statements, (s1, s2): number => s1.start - s2.start)
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
export default findExports
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file findRequires
|
|
3
|
-
* @module mlly/utils/findRequires
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import {
|
|
7
|
-
SpecifierSyntaxKind,
|
|
8
|
-
StatementKind,
|
|
9
|
-
StatementSyntaxKind
|
|
10
|
-
} from '#src/enums'
|
|
11
|
-
import type { RequireStatement } from '#src/interfaces'
|
|
12
|
-
import dequote from '#src/internal/dequote'
|
|
13
|
-
import getSpecifierKind from '#src/internal/get-specifier-kind'
|
|
14
|
-
import validateString from '#src/internal/validate-string'
|
|
15
|
-
import type { NodeError } from '@flex-development/errnode'
|
|
16
|
-
import { split, trim } from '@flex-development/tutils'
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Finds all `require` statements in `code`. Ignores matches in comments.
|
|
20
|
-
*
|
|
21
|
-
* @see {@linkcode RequireStatement}
|
|
22
|
-
* @see https://regex101.com/r/uCqSYB
|
|
23
|
-
* @see https://nodejs.org/api/modules.html#requireid
|
|
24
|
-
*
|
|
25
|
-
* @param {string} [code=''] - Code to evaluate
|
|
26
|
-
* @return {RequireStatement[]} Require statement objects
|
|
27
|
-
* @throws {NodeError<TypeError>} If `code` is not a string
|
|
28
|
-
*/
|
|
29
|
-
const findRequires = (code: string = ''): RequireStatement[] => {
|
|
30
|
-
validateString(code, 'code')
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* `require` statement regex.
|
|
34
|
-
*
|
|
35
|
-
* @const {RegExp} REQUIRE_REGEX
|
|
36
|
-
*/
|
|
37
|
-
const REQUIRE_REGEX: RegExp =
|
|
38
|
-
/(?<=^|[\s,:;([])\b(?:(?:const\s*|let\s*|var\s*)?(?:(?<=(?:const\s*|let\s*|var\s*))(?<imports>(?:[$_\p{ID_Start}][$\u200C\u200D\p{ID_Continue}]*)|(?:[\w\t\n\r "$'*,./:{}-]+?)))?\s*=?\s*(?<kind>require)\((?<specifier>["']?[\S\t\n\r]+?["']?)\))(?<!(?:\/\/|\*).*)/gu
|
|
39
|
-
|
|
40
|
-
return [...code.matchAll(REQUIRE_REGEX)].map(match => {
|
|
41
|
-
const { 0: code = '', index: start = 0, groups = {} } = match
|
|
42
|
-
const { imports = '', specifier = '' } = groups
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Module specifier syntax.
|
|
46
|
-
*
|
|
47
|
-
* @const {SpecifierSyntaxKind} specifier_syntax
|
|
48
|
-
*/
|
|
49
|
-
const specifier_syntax: SpecifierSyntaxKind = /^["']/.test(specifier)
|
|
50
|
-
? SpecifierSyntaxKind.STATIC
|
|
51
|
-
: SpecifierSyntaxKind.DYNAMIC
|
|
52
|
-
|
|
53
|
-
return {
|
|
54
|
-
code,
|
|
55
|
-
end: start + code.length,
|
|
56
|
-
imports:
|
|
57
|
-
imports === ''
|
|
58
|
-
? []
|
|
59
|
-
: split(imports.replace(/^{|}$/g, ''), ',')
|
|
60
|
-
.map(trim)
|
|
61
|
-
.filter(e => !!e.length),
|
|
62
|
-
kind: StatementKind.REQUIRE,
|
|
63
|
-
specifier: dequote(specifier),
|
|
64
|
-
specifier_kind:
|
|
65
|
-
specifier_syntax === SpecifierSyntaxKind.DYNAMIC
|
|
66
|
-
? null
|
|
67
|
-
: getSpecifierKind(specifier),
|
|
68
|
-
specifier_syntax,
|
|
69
|
-
start,
|
|
70
|
-
syntax: StatementSyntaxKind.REQUIRE
|
|
71
|
-
}
|
|
72
|
-
})
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export default findRequires
|
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file findStaticImports
|
|
3
|
-
* @module mlly/utils/findStaticImports
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import {
|
|
7
|
-
SpecifierSyntaxKind,
|
|
8
|
-
StatementKind,
|
|
9
|
-
StatementSyntaxKind
|
|
10
|
-
} from '#src/enums'
|
|
11
|
-
import type { StaticImport } from '#src/interfaces'
|
|
12
|
-
import getSpecifierKind from '#src/internal/get-specifier-kind'
|
|
13
|
-
import validateString from '#src/internal/validate-string'
|
|
14
|
-
import type { NodeError } from '@flex-development/errnode'
|
|
15
|
-
import { STATIC_IMPORT_REGEX } from '@flex-development/import-regex'
|
|
16
|
-
import { split, trim } from '@flex-development/tutils'
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Finds all static import statements in `code`. Ignores matches in comments.
|
|
20
|
-
*
|
|
21
|
-
* @see {@linkcode StaticImport}
|
|
22
|
-
* @see https://regex101.com/r/wlYQUN
|
|
23
|
-
* @see https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/import
|
|
24
|
-
*
|
|
25
|
-
* @param {string} [code=''] - Code to evaluate
|
|
26
|
-
* @return {StaticImport[]} Static import statement objects
|
|
27
|
-
* @throws {NodeError<TypeError>} If `code` is not a string
|
|
28
|
-
*/
|
|
29
|
-
const findStaticImports = (code: string = ''): StaticImport[] => {
|
|
30
|
-
validateString(code, 'code')
|
|
31
|
-
|
|
32
|
-
return [...code.matchAll(STATIC_IMPORT_REGEX)].map(match => {
|
|
33
|
-
const { 0: code = '', index: start = 0, groups = {} } = match
|
|
34
|
-
const { assertion = '', imports = '', specifier = '', type = '' } = groups
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Statement syntax kind.
|
|
38
|
-
*
|
|
39
|
-
* @const {StaticImport['syntax']} syntax
|
|
40
|
-
*/
|
|
41
|
-
const syntax: StaticImport['syntax'] =
|
|
42
|
-
imports === ''
|
|
43
|
-
? StatementSyntaxKind.SIDE_EFFECT
|
|
44
|
-
: imports.startsWith('* as')
|
|
45
|
-
? StatementSyntaxKind.NAMESPACE
|
|
46
|
-
: imports.startsWith('{')
|
|
47
|
-
? StatementSyntaxKind.NAMED
|
|
48
|
-
: /^\w+$/.test(imports)
|
|
49
|
-
? StatementSyntaxKind.DEFAULT
|
|
50
|
-
: /^\w+,\s*{/.test(imports)
|
|
51
|
-
? StatementSyntaxKind.DEFAULT_WITH_NAMED
|
|
52
|
-
: StatementSyntaxKind.DEFAULT_WITH_NAMESPACE
|
|
53
|
-
|
|
54
|
-
return {
|
|
55
|
-
assertion,
|
|
56
|
-
code,
|
|
57
|
-
end: start + code.length,
|
|
58
|
-
imports:
|
|
59
|
-
syntax === StatementSyntaxKind.SIDE_EFFECT
|
|
60
|
-
? []
|
|
61
|
-
: syntax === StatementSyntaxKind.NAMED
|
|
62
|
-
? split(imports.replace(/^{|}$/g, ''), ',')
|
|
63
|
-
.map(trim)
|
|
64
|
-
.filter(i => !!i.length)
|
|
65
|
-
: syntax === StatementSyntaxKind.DEFAULT_WITH_NAMED
|
|
66
|
-
? split(imports, ',')
|
|
67
|
-
.map(i => trim(i).replace(/^{|}$/g, ''))
|
|
68
|
-
.map(trim)
|
|
69
|
-
.filter(i => !!i.length)
|
|
70
|
-
: syntax === StatementSyntaxKind.DEFAULT_WITH_NAMESPACE
|
|
71
|
-
? split(imports, ',')
|
|
72
|
-
.map(trim)
|
|
73
|
-
.filter(i => !!i.length)
|
|
74
|
-
: [imports],
|
|
75
|
-
kind: StatementKind.IMPORT,
|
|
76
|
-
specifier,
|
|
77
|
-
specifier_kind: getSpecifierKind(specifier),
|
|
78
|
-
specifier_syntax: SpecifierSyntaxKind.STATIC,
|
|
79
|
-
start,
|
|
80
|
-
syntax,
|
|
81
|
-
type: !!type
|
|
82
|
-
}
|
|
83
|
-
})
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
export default findStaticImports
|