@yozora/tokenizer-ecma-import
@yozora/tokenizer-ecma-import parse ECMAScript import
statement. It should
be noted that only single-line import
statements are supported. in addition
* as
and type import are not support. For example, the following import
statements are supported:
import '@yozora/parser'
import Parser from '@yozora/parser'
import Parser, { YozoraParserProps } from '@yozora/parser'
import { YozoraParserProps } from '@yozora/parser'
import { YozoraParser, YozoraParser as Parser } from '@yozora/parser'
And here is the not supported case:
import * as Parser '@yozora/parser'
import { Parser } from '@yozora/parser'
Install
- npm
- Yarn
- pnpm
npm install --save @yozora/tokenizer-ecma-import
yarn add @yozora/tokenizer-ecma-import
pnpm add @yozora/tokenizer-ecma-import
Usage
@yozora/tokenizer-ecma-import has been integrated into @yozora/parser,
so you can use YozoraParser
directly.
- Basic Usage
- YozoraParser
- GfmParser
- GfmExParser
@yozora/tokenizer-ecma-import cannot be used alone, it needs to be registered in YastParser as a plugin-in before it can be used.
import { DefaultYastParser } from '@yozora/core-parser'
import ParagraphTokenizer from '@yozora/tokenizer-paragraph'
import TextTokenizer from '@yozora/tokenizer-text'
import EcmaImportTokenizer from '@yozora/tokenizer-ecma-import'
const parser = new DefaultYastParser()
.useBlockFallbackTokenizer(new ParagraphTokenizer())
.useInlineFallbackTokenizer(new TextTokenizer())
.useTokenizer(new EcmaImportTokenizer())
// parse source markdown content
parser.parse(`
import { Parser } from '@yozora/parser'
import { Button } from '@mui/material'
import Markdown from '@yozora/react-markdown'
`)
import YozoraParser from '@yozora/parser'
const parser = new YozoraParser()
// parse source markdown content
parser.parse(`
import { Parser } from '@yozora/parser'
import { Button } from '@mui/material'
import Markdown from '@yozora/react-markdown'
`)
import GfmParser from '@yozora/parser-gfm'
import EcmaImportTokenizer from '@yozora/tokenizer-ecma-import'
const parser = new GfmParser()
parser.useTokenizer(new EcmaImportTokenizer())
// parse source markdown content
parser.parse(`
import { Parser } from '@yozora/parser'
import { Button } from '@mui/material'
import Markdown from '@yozora/react-markdown'
`)
import GfmExParser from '@yozora/parser-gfm-ex'
import EcmaImportTokenizer from '@yozora/tokenizer-ecma-import'
const parser = new GfmExParser()
parser.useTokenizer(new EcmaImportTokenizer())
// parse source markdown content
parser.parse(`
import { Parser } from '@yozora/parser'
import { Button } from '@mui/material'
import Markdown from '@yozora/react-markdown'
`)
Options
Name | Type | Required | Default |
---|---|---|---|
name | string | false | "@yozora/tokenizer-ecma-import" |
priority | number | false | TokenizerPriority.ATOMIC |
-
name
: The unique name of the tokenizer, used to bind the token it generates, to determine the tokenizer that should be called in each life cycle of the token in the entire matching / parsing phase. -
priority
: Priority of the tokenizer, determine the order of processing, high priority priority execution. interruptable. In addition, in thematch-block
stage, a high-priority tokenizer can interrupt the matching process of a low-priority tokenizer.
Types
/**
* import { YozoraParserProps, YozoraParser as Parser } from '@yozora/parser'
* ==> [
* { src: 'YozoraParserProps', alias: null },
* { src: 'YozoraParser', alias: 'Parser' },
* ]
*/
export interface EcmaImportNamedImport {
src: string
alias: string | null
}
/**
* import Parser from '@yozora/parser'
* import Parser, { YozoraParserProps } from '@yozora/parser'
* import { YozoraParserProps } from '@yozora/parser'
* import { YozoraParser as Parser } from '@yozora/parser'
*/
export interface EcmaImport extends YastNode<EcmaImportType> {
/**
* import Parser from '@yozora/parser'
* ==> { moduleName: '@yozora/parser' }
*/
moduleName: string
/**
* import Parser, { YozoraParserProps } from '@yozora/parser'
* ==> { defaultImport: 'Parser' }
*/
defaultImport: string | null
/**
* import { YozoraParserProps, YozoraParser as Parser } from '@yozora/parser'
* ==> {
* namedImports: [
* { src: 'YozoraParserProps', alias: null },
* { src: 'YozoraParser', alias: 'Parser' },
* ]
* }
*/
namedImports: EcmaImportNamedImport[]
}
Live Examples
-
Basic.
#1yozorapretty-json