@a-la/context 中文文档教程
@a-la/context
@a-la/context
是 alamode
转译器。 它将提供一种方法来记录给定规则的转换结果以及发出的事件。
yarn add -E @a-la/context
Table Of Contents
- Table Of Contents
- API
async stream(rules: Rule|Rule[], text: string, eventKeys?: string[]): ReturnType
ReturnType
setConfig(config: Object)
setFile(file: string)
setProps(props: !Object)
- Copyright
API
该包可通过导入其默认功能获得:
import ÀLaContext from '@a-la/context'
然后将上下文传递给 Zoroaster 测试套件的 context
属性。
async <ins>stream</ins>(
rules: Rule|Rule[],
text: string,
eventKeys?: string[],
): <i>ReturnType</i>
根据一条规则或一组规则创建一个 Replaceable
流,以传递的文本异步结束它并返回结果。
在下面的示例中,转换规则用于将 export
语句替换为 module.exports
语句,并发出 exports
事件。
/**
* A rule to replace an `export function` statement with `module.exports`.
*/
export const exportFunctionRule = {
re: / *export function ([$_\w][$_\w\d]*)/gm,
replacement(_, fn) {
// async
this.emit('exports', fn)
// sync
this.exports = this.exports || []
this.exports.push(fn)
return `module.exports.${fn} = function ${fn}`
},
}
现在,可以使用 @a-la/context
和 zoroaster
测试此规则/a> 测试框架。
import { equal, deepEqual } from '@zoroaster/assert'
import ÀLaContext from '@a-la/context'
import { exportFunctionRule as rule } from '../../src/rule'
/** @type {Object.<string, (c: ÀLaContext)>} */
const T = {
context: ÀLaContext,
async 'replaces the export function'({ stream }) {
const fn = 'test'
const data = `export function ${fn}() {}`
const {
result,
events,
replaceable,
} = await stream(rule, data, ['exports'])
const expected = `module.exports.${fn} = function ${fn}() {}`
equal(result, expected)
deepEqual(events, {
exports: [fn],
})
deepEqual(replaceable.exports, [fn])
},
}
export default T
example/test/spec
✓ replaces the export function
Executed 1 test.
stream
函数的输出是一个如下所述的对象。
ReturnType
:可替换实例、字符串结果和事件映射。
Name | Type | Description |
---|---|---|
events* | Object<string, any[]> | Emitted events recorded against passed events keys. |
result* | string | The caught output of a Replaceable stream as a string. |
replaceable* | Replaceable | The instance of a Replaceable stream. |
<ins>setConfig</ins>(
config: Object,
): <i>void</i>
此方法允许设置在 stream
方法中创建的 Replaceable 实例的 config
属性。 某些转换可能使用 config
来实现某些功能,例如,替换 @a 中的源字符串-la/import
转换。
<ins>setFile</ins>(
file: string,
): <i>void</i>
在可替换流上设置 file
属性。 这是导入转换需要找到导入模块的 package.json 文件来检查它是否具有 alamode
属性,以便 esCheck
可以跳过。
<ins>setProps</ins>(
props: !Object,
): <i>void</i>
将属性分配给转换流。 规则将能够通过它们的 this
上下文访问它们。
Copyright
© Art Deco™ for À La Mode 2020 |
---|
@a-la/context
@a-la/context
is a a test context for @a-la
packages used in alamode
transpiler. It will provide a means to record the result of transforms for given rules, as well as emitted events.
yarn add -E @a-la/context
Table Of Contents
- Table Of Contents
- API
async stream(rules: Rule|Rule[], text: string, eventKeys?: string[]): ReturnType
ReturnType
setConfig(config: Object)
setFile(file: string)
setProps(props: !Object)
- Copyright
API
The package is available by importing its default function:
import ÀLaContext from '@a-la/context'
The context is then passed to the context
property of Zoroaster tests suites.
async <ins>stream</ins>(
rules: Rule|Rule[],
text: string,
eventKeys?: string[],
): <i>ReturnType</i>
Creates a Replaceable
stream according to a rule or set of rules, asynchronously ends it with passed text and returns the outcome.
In the example below, a transform rule is used to replace an export
statement with a module.exports
statement, and emit an exports
event.
/**
* A rule to replace an `export function` statement with `module.exports`.
*/
export const exportFunctionRule = {
re: / *export function ([$_\w][$_\w\d]*)/gm,
replacement(_, fn) {
// async
this.emit('exports', fn)
// sync
this.exports = this.exports || []
this.exports.push(fn)
return `module.exports.${fn} = function ${fn}`
},
}
Now, this rule can be tested using the @a-la/context
and zoroaster
testing framework.
import { equal, deepEqual } from '@zoroaster/assert'
import ÀLaContext from '@a-la/context'
import { exportFunctionRule as rule } from '../../src/rule'
/** @type {Object.<string, (c: ÀLaContext)>} */
const T = {
context: ÀLaContext,
async 'replaces the export function'({ stream }) {
const fn = 'test'
const data = `export function ${fn}() {}`
const {
result,
events,
replaceable,
} = await stream(rule, data, ['exports'])
const expected = `module.exports.${fn} = function ${fn}() {}`
equal(result, expected)
deepEqual(events, {
exports: [fn],
})
deepEqual(replaceable.exports, [fn])
},
}
export default T
example/test/spec
✓ replaces the export function
???? Executed 1 test.
The output of the stream
function is an object described below.
ReturnType
: Replaceable instance, string result and events map.
Name | Type | Description |
---|---|---|
events* | Object<string, any[]> | Emitted events recorded against passed events keys. |
result* | string | The caught output of a Replaceable stream as a string. |
replaceable* | Replaceable | The instance of a Replaceable stream. |
<ins>setConfig</ins>(
config: Object,
): <i>void</i>
This method allows to set the config
property of the Replaceable instance created inside of the stream
method. Some transform may use config
for certain functionality, e.g., replacing of the source string in the @a-la/import
transform.
<ins>setFile</ins>(
file: string,
): <i>void</i>
Sets the file
property on the replaceable stream. This is required by the import transform to find the package.json file of the imported module to check whether it has the alamode
property so that the esCheck
can be skipped.
<ins>setProps</ins>(
props: !Object,
): <i>void</i>
Assigns properties to the transform stream. The rules will be able to access them via their this
context.
Copyright
© Art Deco™ for À La Mode 2020 |
---|
你可能也喜欢
- 375to320 中文文档教程
- @36node/kanban-sdk 中文文档教程
- @58fe/babel-plugin-v5-import 中文文档教程
- @8pattern/jlogger 中文文档教程
- @aansi/spool 中文文档教程
- @abehandler/timebinner 中文文档教程
- @abi-software/mapintegratedvuer 中文文档教程
- @abizzle/eslint-config-abizzle 中文文档教程
- @absmartly/javascript-sdk 中文文档教程
- @acoustic-content-sdk/cross-frame-redux 中文文档教程