Skip to content

Commit a2a53d3

Browse files
committed
docs: update README and add tests for message directives
1 parent c8a5bd7 commit a2a53d3

File tree

3 files changed

+93
-2
lines changed

3 files changed

+93
-2
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ class MyClass {
188188

189189
You instruct the compiler to generate user-defined compiler errors and warnings and informational messages.
190190

191-
- `#error`: Generates an error.
191+
- `#error`: Generates an error, but does not terminate compilation.
192192
- `#warning`: Generates a warning.
193193
- `#info`: Generates an informational message.
194194

@@ -197,6 +197,18 @@ You instruct the compiler to generate user-defined compiler errors and warnings
197197
// #warning this is a warning message
198198
// #info this is an info message
199199
```
200+
201+
Of course, it can also be combined with conditional compilation:
202+
203+
```ts
204+
// #if DEBUG
205+
// #info Debug mode is on
206+
// #endif
207+
// #if !DEBUG
208+
// #info Debug mode is off
209+
// #endif
210+
```
211+
200212
## Custom directive
201213

202214
You can used `defineDirective` to define your own directive.

README.zh-cn.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ class MyClass {
187187

188188
可以指示编译器生成用户定义的编译器错误、警告和信息。
189189

190-
- `#error`: 生成一条错误消息。
190+
- `#error`: 生成一条错误消息,但不会终止编译
191191
- `#warning`: 生成一条警告消息。
192192
- `#info`: 生成一条信息消息。
193193

@@ -197,6 +197,17 @@ class MyClass {
197197
// #info this is an info message
198198
```
199199

200+
当然,也可以和条件编译结合使用:
201+
202+
```ts
203+
// #if DEBUG
204+
// #info Debug mode is on
205+
// #endif
206+
// #if !DEBUG
207+
// #info Debug mode is off
208+
// #endif
209+
```
210+
200211
## 自定义指令
201212

202213
您可以使用 `defineDirective` 定义自己的指令。

test/message.test.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { describe, expect, it, vi } from 'vitest'
2+
import { Context, ifDirective, MessageDirective } from '../src'
3+
4+
describe('message', () => {
5+
it('should log error message', () => {
6+
const context = new Context({
7+
// @ts-expect-error ignore
8+
directives: [MessageDirective],
9+
})
10+
11+
const loggerErrorSpy = vi.spyOn(context.logger, 'error')
12+
const code = '// #error This is an error message'
13+
14+
context.transform(code, 'test.js')
15+
16+
expect(loggerErrorSpy).toHaveBeenCalledWith('This is an error message', { timestamp: true })
17+
})
18+
19+
it('should log warning message', () => {
20+
const context = new Context({
21+
// @ts-expect-error ignore
22+
directives: [MessageDirective],
23+
})
24+
25+
const loggerWarnSpy = vi.spyOn(context.logger, 'warn')
26+
const code = '// #warning This is a warning message'
27+
28+
context.transform(code, 'test.js')
29+
30+
expect(loggerWarnSpy).toHaveBeenCalledWith('This is a warning message', { timestamp: true })
31+
})
32+
33+
it('should log info message', () => {
34+
const context = new Context({
35+
// @ts-expect-error ignore
36+
directives: [MessageDirective],
37+
})
38+
39+
const loggerInfoSpy = vi.spyOn(context.logger, 'info')
40+
const code = '// #info This is an info message'
41+
42+
context.transform(code, 'test.js')
43+
44+
expect(loggerInfoSpy).toHaveBeenCalledWith('This is an info message', { timestamp: true })
45+
})
46+
47+
it('with if directive', () => {
48+
const context = new Context({
49+
// @ts-expect-error ignore
50+
directives: [ifDirective, MessageDirective],
51+
})
52+
context.env.DEV = true
53+
54+
const loggerInfoSpy = vi.spyOn(context.logger, 'info')
55+
const code = `
56+
// #if DEV
57+
// #info DEV mode is on
58+
// #endif
59+
60+
// #if !DEV
61+
// #info DEV mode is off
62+
// #endif
63+
`
64+
65+
context.transform(code, 'test.js')
66+
expect(loggerInfoSpy).toHaveBeenCalledWith('DEV mode is on', { timestamp: true })
67+
})
68+
})

0 commit comments

Comments
 (0)