@yozora/tokenizer-ecma-import 2.1.2 → 2.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yozora/tokenizer-ecma-import",
3
- "version": "2.1.2",
3
+ "version": "2.1.4",
4
4
  "author": {
5
5
  "name": "guanghechen",
6
6
  "url": "https://github.com/guanghechen/"
@@ -27,7 +27,7 @@
27
27
  },
28
28
  "files": [
29
29
  "lib/",
30
- "src/",
30
+ "lib/**/*.map",
31
31
  "package.json",
32
32
  "CHANGELOG.md",
33
33
  "LICENSE",
@@ -39,9 +39,9 @@
39
39
  "test": "cross-env TS_NODE_FILES=true NODE_OPTIONS=--experimental-vm-modules jest --config ../../jest.config.mjs --rootDir ."
40
40
  },
41
41
  "dependencies": {
42
- "@yozora/ast": "^2.1.2",
43
- "@yozora/character": "^2.1.2",
44
- "@yozora/core-tokenizer": "^2.1.2"
42
+ "@yozora/ast": "^2.1.4",
43
+ "@yozora/character": "^2.1.4",
44
+ "@yozora/core-tokenizer": "^2.1.4"
45
45
  },
46
- "gitHead": "992bacafd173e7788e99fed34ce8b45f6ed24cfe"
46
+ "gitHead": "aa464ed1e3cd84892773a833910cfc53a556bf5f"
47
47
  }
package/src/index.ts DELETED
@@ -1,9 +0,0 @@
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 DELETED
@@ -1,103 +0,0 @@
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 DELETED
@@ -1,26 +0,0 @@
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 DELETED
@@ -1,32 +0,0 @@
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 DELETED
@@ -1,16 +0,0 @@
1
- import type { EcmaImport, EcmaImportType } from '@yozora/ast'
2
- import type {
3
- IBaseBlockTokenizerProps,
4
- IPartialBlockToken,
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 = IPartialBlockToken<T> & Omit<EcmaImport, 'type'>
13
-
14
- export type IThis = ITokenizer
15
-
16
- export type ITokenizerProps = Partial<IBaseBlockTokenizerProps>
package/src/util.ts DELETED
@@ -1,46 +0,0 @@
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
- }