@yozora/tokenizer-ecma-import 2.0.4 → 2.0.5
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/lib/cjs/{index.js → index.cjs} +5 -6
- package/lib/esm/{index.js → index.mjs} +4 -5
- package/lib/types/index.d.ts +5 -5
- package/package.json +18 -14
- package/src/index.ts +9 -0
- package/src/match.ts +103 -0
- package/src/parse.ts +26 -0
- package/src/tokenizer.ts +32 -0
- package/src/types.ts +16 -0
- package/src/util.ts +46 -0
|
@@ -114,18 +114,17 @@ const uniqueName = '@yozora/tokenizer-ecma-import';
|
|
|
114
114
|
|
|
115
115
|
class EcmaImportTokenizer extends coreTokenizer.BaseBlockTokenizer {
|
|
116
116
|
constructor(props = {}) {
|
|
117
|
-
var _a, _b;
|
|
118
117
|
super({
|
|
119
|
-
name:
|
|
120
|
-
priority:
|
|
118
|
+
name: props.name ?? uniqueName,
|
|
119
|
+
priority: props.priority ?? coreTokenizer.TokenizerPriority.ATOMIC,
|
|
121
120
|
});
|
|
122
|
-
this.match = match;
|
|
123
|
-
this.parse = parse;
|
|
124
121
|
}
|
|
122
|
+
match = match;
|
|
123
|
+
parse = parse;
|
|
125
124
|
}
|
|
126
125
|
|
|
127
126
|
exports.EcmaImportTokenizer = EcmaImportTokenizer;
|
|
128
127
|
exports.EcmaImportTokenizerName = uniqueName;
|
|
129
|
-
exports
|
|
128
|
+
exports.default = EcmaImportTokenizer;
|
|
130
129
|
exports.ecmaImportMatch = match;
|
|
131
130
|
exports.ecmaImportParse = parse;
|
|
@@ -110,14 +110,13 @@ const uniqueName = '@yozora/tokenizer-ecma-import';
|
|
|
110
110
|
|
|
111
111
|
class EcmaImportTokenizer extends BaseBlockTokenizer {
|
|
112
112
|
constructor(props = {}) {
|
|
113
|
-
var _a, _b;
|
|
114
113
|
super({
|
|
115
|
-
name:
|
|
116
|
-
priority:
|
|
114
|
+
name: props.name ?? uniqueName,
|
|
115
|
+
priority: props.priority ?? TokenizerPriority.ATOMIC,
|
|
117
116
|
});
|
|
118
|
-
this.match = match;
|
|
119
|
-
this.parse = parse;
|
|
120
117
|
}
|
|
118
|
+
match = match;
|
|
119
|
+
parse = parse;
|
|
121
120
|
}
|
|
122
121
|
|
|
123
122
|
export { EcmaImportTokenizer, uniqueName as EcmaImportTokenizerName, EcmaImportTokenizer as default, match as ecmaImportMatch, parse as ecmaImportParse };
|
package/lib/types/index.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { IPartialYastBlockToken, ITokenizer, IBaseBlockTokenizerProps, IMatchBlockHookCreator, IParseBlockHookCreator, BaseBlockTokenizer, IBlockTokenizer } from '@yozora/core-tokenizer';
|
|
2
2
|
import { EcmaImport, EcmaImportType } from '@yozora/ast';
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
type T = EcmaImportType;
|
|
5
|
+
type INode = EcmaImport;
|
|
6
6
|
declare const uniqueName = "@yozora/tokenizer-ecma-import";
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
type IToken = IPartialYastBlockToken<T> & Omit<EcmaImport, 'type'>;
|
|
8
|
+
type IThis = ITokenizer;
|
|
9
|
+
type ITokenizerProps = Partial<IBaseBlockTokenizerProps>;
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* Examples
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yozora/tokenizer-ecma-import",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.5",
|
|
4
4
|
"author": {
|
|
5
5
|
"name": "guanghechen",
|
|
6
6
|
"url": "https://github.com/guanghechen/"
|
|
@@ -11,33 +11,37 @@
|
|
|
11
11
|
"directory": "tokenizers/ecma-import"
|
|
12
12
|
},
|
|
13
13
|
"homepage": "https://github.com/yozorajs/yozora/tree/release-2.x.x/tokenizers/ecma-import",
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
"type": "module",
|
|
15
|
+
"exports": {
|
|
16
|
+
"types": "./lib/types/index.d.ts",
|
|
17
|
+
"import": "./lib/esm/index.mjs",
|
|
18
|
+
"require": "./lib/cjs/index.cjs"
|
|
19
|
+
},
|
|
20
|
+
"source": "./src/index.ts",
|
|
21
|
+
"types": "./lib/types/index.d.ts",
|
|
22
|
+
"main": "./lib/cjs/index.cjs",
|
|
23
|
+
"module": "./lib/esm/index.mjs",
|
|
18
24
|
"license": "MIT",
|
|
19
25
|
"engines": {
|
|
20
26
|
"node": ">= 16.0.0"
|
|
21
27
|
},
|
|
22
28
|
"files": [
|
|
23
29
|
"lib/",
|
|
24
|
-
"
|
|
25
|
-
"!lib/**/*.d.ts.map",
|
|
30
|
+
"src/",
|
|
26
31
|
"package.json",
|
|
27
32
|
"CHANGELOG.md",
|
|
28
33
|
"LICENSE",
|
|
29
34
|
"README.md"
|
|
30
35
|
],
|
|
31
36
|
"scripts": {
|
|
32
|
-
"build": "cross-env NODE_ENV=production rollup -c ../../rollup.config.
|
|
33
|
-
"prebuild": "rimraf lib/",
|
|
37
|
+
"build": "rimraf lib/ && cross-env NODE_ENV=production rollup -c ../../rollup.config.mjs",
|
|
34
38
|
"prepublishOnly": "cross-env ROLLUP_SHOULD_SOURCEMAP=false yarn build",
|
|
35
|
-
"test": "cross-env TS_NODE_FILES=true jest --config ../../jest.config.
|
|
39
|
+
"test": "cross-env TS_NODE_FILES=true NODE_OPTIONS=--experimental-vm-modules jest --config ../../jest.config.mjs --rootDir ."
|
|
36
40
|
},
|
|
37
41
|
"dependencies": {
|
|
38
|
-
"@yozora/ast": "^2.0.
|
|
39
|
-
"@yozora/character": "^2.0.
|
|
40
|
-
"@yozora/core-tokenizer": "^2.0.
|
|
42
|
+
"@yozora/ast": "^2.0.5",
|
|
43
|
+
"@yozora/character": "^2.0.5",
|
|
44
|
+
"@yozora/core-tokenizer": "^2.0.5"
|
|
41
45
|
},
|
|
42
|
-
"gitHead": "
|
|
46
|
+
"gitHead": "7ba3bab49fe65cf2f57082c0503af73da9356cf0"
|
|
43
47
|
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { match as ecmaImportMatch } from './match'
|
|
2
|
+
export { parse as ecmaImportParse } from './parse'
|
|
3
|
+
export { EcmaImportTokenizer, EcmaImportTokenizer as default } from './tokenizer'
|
|
4
|
+
export { uniqueName as EcmaImportTokenizerName } from './types'
|
|
5
|
+
export type {
|
|
6
|
+
IThis as ecmaImportHookContext,
|
|
7
|
+
IToken as IEcmaImportToken,
|
|
8
|
+
ITokenizerProps as IEcmaImportProps,
|
|
9
|
+
} from './types'
|
package/src/match.ts
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import type { Position } from '@yozora/ast'
|
|
2
|
+
import { EcmaImportType } from '@yozora/ast'
|
|
3
|
+
import {
|
|
4
|
+
AsciiCodePoint,
|
|
5
|
+
calcStringFromNodePoints,
|
|
6
|
+
calcTrimBoundaryOfCodePoints,
|
|
7
|
+
} from '@yozora/character'
|
|
8
|
+
import type {
|
|
9
|
+
IMatchBlockHookCreator,
|
|
10
|
+
IPhrasingContentLine,
|
|
11
|
+
IResultOfEatOpener,
|
|
12
|
+
} from '@yozora/core-tokenizer'
|
|
13
|
+
import { calcEndPoint, calcStartPoint } from '@yozora/core-tokenizer'
|
|
14
|
+
import type { IThis, IToken, T } from './types'
|
|
15
|
+
import { regex1, regex2, regex3, resolveNameImports } from './util'
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Examples
|
|
19
|
+
*
|
|
20
|
+
* import '@yozora/parser'
|
|
21
|
+
* import Parser from '@yozora/parser'
|
|
22
|
+
* import Parser, { YozoraParserProps } from '@yozora/parser'
|
|
23
|
+
* import { YozoraParserProps } from '@yozora/parser'
|
|
24
|
+
* import { YozoraParser, YozoraParser as Parser } from '@yozora/parser'
|
|
25
|
+
*
|
|
26
|
+
* @see https://github.com/syntax-tree/mdast#strong
|
|
27
|
+
* @see https://github.github.com/gfm/#emphasis-and-strong-emphasis
|
|
28
|
+
*/
|
|
29
|
+
export const match: IMatchBlockHookCreator<T, IToken, IThis> = function () {
|
|
30
|
+
return {
|
|
31
|
+
isContainingBlock: false,
|
|
32
|
+
eatOpener,
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function eatOpener(line: Readonly<IPhrasingContentLine>): IResultOfEatOpener<T, IToken> {
|
|
36
|
+
/**
|
|
37
|
+
* Four spaces are too much
|
|
38
|
+
* @see https://github.github.com/gfm/#example-180
|
|
39
|
+
*/
|
|
40
|
+
if (line.countOfPrecedeSpaces >= 4) return null
|
|
41
|
+
|
|
42
|
+
const { nodePoints, startIndex, endIndex, firstNonWhitespaceIndex } = line
|
|
43
|
+
if (firstNonWhitespaceIndex + 8 >= endIndex) return null
|
|
44
|
+
|
|
45
|
+
const i = firstNonWhitespaceIndex
|
|
46
|
+
|
|
47
|
+
if (nodePoints[i].codePoint !== AsciiCodePoint.LOWERCASE_I) return null
|
|
48
|
+
if (nodePoints[i + 1].codePoint !== AsciiCodePoint.LOWERCASE_M) return null
|
|
49
|
+
if (nodePoints[i + 2].codePoint !== AsciiCodePoint.LOWERCASE_P) return null
|
|
50
|
+
if (nodePoints[i + 3].codePoint !== AsciiCodePoint.LOWERCASE_O) return null
|
|
51
|
+
if (nodePoints[i + 4].codePoint !== AsciiCodePoint.LOWERCASE_R) return null
|
|
52
|
+
if (nodePoints[i + 5].codePoint !== AsciiCodePoint.LOWERCASE_T) return null
|
|
53
|
+
|
|
54
|
+
const [left, right] = calcTrimBoundaryOfCodePoints(
|
|
55
|
+
nodePoints,
|
|
56
|
+
firstNonWhitespaceIndex,
|
|
57
|
+
endIndex,
|
|
58
|
+
)
|
|
59
|
+
const text: string = calcStringFromNodePoints(nodePoints, left, right)
|
|
60
|
+
let m: RegExpExecArray | null
|
|
61
|
+
|
|
62
|
+
let token: IToken | null = null
|
|
63
|
+
const position = (): Position => ({
|
|
64
|
+
start: calcStartPoint(nodePoints, startIndex),
|
|
65
|
+
end: calcEndPoint(nodePoints, endIndex - 1),
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
// eslint-disable-next-line no-cond-assign
|
|
69
|
+
if ((m = regex1.exec(text)) != null) {
|
|
70
|
+
token = {
|
|
71
|
+
nodeType: EcmaImportType,
|
|
72
|
+
position: position(),
|
|
73
|
+
moduleName: m[2],
|
|
74
|
+
defaultImport: null,
|
|
75
|
+
namedImports: [],
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// eslint-disable-next-line no-cond-assign
|
|
80
|
+
else if ((m = regex2.exec(text)) != null) {
|
|
81
|
+
token = {
|
|
82
|
+
nodeType: EcmaImportType,
|
|
83
|
+
position: position(),
|
|
84
|
+
moduleName: m[3],
|
|
85
|
+
defaultImport: m[1],
|
|
86
|
+
namedImports: [],
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// eslint-disable-next-line no-cond-assign
|
|
91
|
+
else if ((m = regex3.exec(text)) != null) {
|
|
92
|
+
token = {
|
|
93
|
+
nodeType: EcmaImportType,
|
|
94
|
+
position: position(),
|
|
95
|
+
moduleName: m[4],
|
|
96
|
+
defaultImport: m[1],
|
|
97
|
+
namedImports: resolveNameImports(m[2]),
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return token === null ? null : { token, nextIndex: endIndex, saturated: true }
|
|
102
|
+
}
|
|
103
|
+
}
|
package/src/parse.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { EcmaImportType } from '@yozora/ast'
|
|
2
|
+
import type { IParseBlockHookCreator } from '@yozora/core-tokenizer'
|
|
3
|
+
import type { INode, IThis, IToken, T } from './types'
|
|
4
|
+
|
|
5
|
+
export const parse: IParseBlockHookCreator<T, IToken, INode, IThis> = function (api) {
|
|
6
|
+
return {
|
|
7
|
+
parse: tokens =>
|
|
8
|
+
tokens.map(token => {
|
|
9
|
+
const node: INode = api.shouldReservePosition
|
|
10
|
+
? {
|
|
11
|
+
type: EcmaImportType,
|
|
12
|
+
position: token.position,
|
|
13
|
+
moduleName: token.moduleName,
|
|
14
|
+
defaultImport: token.defaultImport,
|
|
15
|
+
namedImports: token.namedImports,
|
|
16
|
+
}
|
|
17
|
+
: {
|
|
18
|
+
type: EcmaImportType,
|
|
19
|
+
moduleName: token.moduleName,
|
|
20
|
+
defaultImport: token.defaultImport,
|
|
21
|
+
namedImports: token.namedImports,
|
|
22
|
+
}
|
|
23
|
+
return node
|
|
24
|
+
}),
|
|
25
|
+
}
|
|
26
|
+
}
|
package/src/tokenizer.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
IBlockTokenizer,
|
|
3
|
+
IMatchBlockHookCreator,
|
|
4
|
+
IParseBlockHookCreator,
|
|
5
|
+
} from '@yozora/core-tokenizer'
|
|
6
|
+
import { BaseBlockTokenizer, TokenizerPriority } from '@yozora/core-tokenizer'
|
|
7
|
+
import { match } from './match'
|
|
8
|
+
import { parse } from './parse'
|
|
9
|
+
import type { INode, IThis, IToken, ITokenizerProps, T } from './types'
|
|
10
|
+
import { uniqueName } from './types'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Lexical Analyzer for Ecma Import statement
|
|
14
|
+
* @see https://github.com/syntax-tree/mdast#strong
|
|
15
|
+
* @see https://github.github.com/gfm/#emphasis-and-strong-emphasis
|
|
16
|
+
*/
|
|
17
|
+
export class EcmaImportTokenizer
|
|
18
|
+
extends BaseBlockTokenizer<T, IToken, INode, IThis>
|
|
19
|
+
implements IBlockTokenizer<T, IToken, INode, IThis>
|
|
20
|
+
{
|
|
21
|
+
/* istanbul ignore next */
|
|
22
|
+
constructor(props: ITokenizerProps = {}) {
|
|
23
|
+
super({
|
|
24
|
+
name: props.name ?? uniqueName,
|
|
25
|
+
priority: props.priority ?? TokenizerPriority.ATOMIC,
|
|
26
|
+
})
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
public override readonly match: IMatchBlockHookCreator<T, IToken, IThis> = match
|
|
30
|
+
|
|
31
|
+
public override readonly parse: IParseBlockHookCreator<T, IToken, INode, IThis> = parse
|
|
32
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { EcmaImport, EcmaImportType } from '@yozora/ast'
|
|
2
|
+
import type {
|
|
3
|
+
IBaseBlockTokenizerProps,
|
|
4
|
+
IPartialYastBlockToken,
|
|
5
|
+
ITokenizer,
|
|
6
|
+
} from '@yozora/core-tokenizer'
|
|
7
|
+
|
|
8
|
+
export type T = EcmaImportType
|
|
9
|
+
export type INode = EcmaImport
|
|
10
|
+
export const uniqueName = '@yozora/tokenizer-ecma-import'
|
|
11
|
+
|
|
12
|
+
export type IToken = IPartialYastBlockToken<T> & Omit<EcmaImport, 'type'>
|
|
13
|
+
|
|
14
|
+
export type IThis = ITokenizer
|
|
15
|
+
|
|
16
|
+
export type ITokenizerProps = Partial<IBaseBlockTokenizerProps>
|
package/src/util.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { IEcmaImportNamedImport } from '@yozora/ast'
|
|
2
|
+
|
|
3
|
+
const namedImportItemRegex = /^(\w+)(?:\s+as\s+(\w+))?$/
|
|
4
|
+
const namedImportRegex = /\{\s*((?:[\w]+(?:\s+as\s+[\w]+)?\s*,\s*)*[\w]+(?:\s+as\s+[\w]+)?)\s*\}\s*/
|
|
5
|
+
const endRegex = /\s*;*\s*$/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* import '@yozora.parser'
|
|
9
|
+
*/
|
|
10
|
+
export const regex1 = new RegExp(/^import\s+(['"])([^'"]+)\1/.source + endRegex.source)
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* import Parser from '@yozora/parser'
|
|
14
|
+
*/
|
|
15
|
+
export const regex2 = new RegExp(
|
|
16
|
+
/^import\s+([\w]+)\s+from\s+(['"])([^'"]+)\2/.source + endRegex.source,
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* import Parser, { YozoraParser } from '@yozora/parser'
|
|
21
|
+
* import Parser, { YozoraParser as Parser } from '@yozora/parser'
|
|
22
|
+
* import Parser, { YozoraParser, YozoraParser as Parser } from '@yozora/parser'
|
|
23
|
+
* import { YozoraParser } from '@yozora/parser'
|
|
24
|
+
* import { YozoraParser as Parser } from '@yozora/parser'
|
|
25
|
+
* import { YozoraParser, YozoraParser as Parser } from '@yozora/parser'
|
|
26
|
+
*/
|
|
27
|
+
export const regex3 = new RegExp(
|
|
28
|
+
/^import\s+(?:([\w]+)\s*,\s*)?/.source +
|
|
29
|
+
namedImportRegex.source +
|
|
30
|
+
/from\s+(['"])([^'"]+)\3/.source +
|
|
31
|
+
endRegex.source,
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
*
|
|
36
|
+
* @param text
|
|
37
|
+
*/
|
|
38
|
+
export function resolveNameImports(text: string): IEcmaImportNamedImport[] {
|
|
39
|
+
const items = text.split(/\s*,\s*/g).filter(item => item.length > 0)
|
|
40
|
+
const result: IEcmaImportNamedImport[] = []
|
|
41
|
+
for (const item of items) {
|
|
42
|
+
const [, src, alias] = namedImportItemRegex.exec(item)!
|
|
43
|
+
result.push({ src, alias: alias == null ? null : alias })
|
|
44
|
+
}
|
|
45
|
+
return result
|
|
46
|
+
}
|