Skip to content

Commit fa87f7d

Browse files
authored
fix: wrong error & warn code definition (#1500)
1 parent a0233de commit fa87f7d

File tree

15 files changed

+165
-82
lines changed

15 files changed

+165
-82
lines changed

packages/core-base/src/context.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ import {
99
isFunction,
1010
isPlainObject,
1111
assign,
12-
isObject
12+
isObject,
13+
warnOnce
1314
} from '@intlify/shared'
1415
import { VueDevToolsTimelineEvents } from '@intlify/vue-devtools'
1516
import { initI18nDevTools } from './devtools'
16-
import { CoreWarnCodes, getWarnMessage, warnOnce } from './warnings'
17+
import { CoreWarnCodes, getWarnMessage } from './warnings'
1718
import { resolveWithKeyValue } from './resolver'
1819
import { fallbackWithSimple } from './fallbacker'
1920

packages/core-base/src/errors.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1+
import { incrementer } from '@intlify/shared'
12
import {
23
CompileErrorCodes,
34
createCompileError
45
} from '@intlify/message-compiler'
5-
import type { CompileError } from '@intlify/message-compiler'
66

7-
export interface CoreError extends CompileError {
8-
code: CoreErrorCodes
9-
}
7+
import type { BaseError } from '@intlify/shared'
8+
9+
export interface CoreError extends BaseError {}
1010

11-
let code = CompileErrorCodes.__EXTEND_POINT__
12-
const inc = () => ++code
11+
const code = CompileErrorCodes.__EXTEND_POINT__
12+
const inc = incrementer(code)
1313

1414
export const CoreErrorCodes = {
15-
INVALID_ARGUMENT: code, // 15
16-
INVALID_DATE_ARGUMENT: inc(), // 16
17-
INVALID_ISO_DATE_ARGUMENT: inc(), // 17
18-
NOT_SUPPORT_NON_STRING_MESSAGE: inc(), // 18
19-
__EXTEND_POINT__: inc() // 19
15+
INVALID_ARGUMENT: code, // 18
16+
INVALID_DATE_ARGUMENT: inc(), // 19
17+
INVALID_ISO_DATE_ARGUMENT: inc(), // 20
18+
NOT_SUPPORT_NON_STRING_MESSAGE: inc(), // 21
19+
__EXTEND_POINT__: inc() // 22
2020
} as const
2121

2222
export type CoreErrorCodes =

packages/core-base/src/warnings.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { format, warn } from '@intlify/shared'
1+
import { format } from '@intlify/shared'
22

33
export const CoreWarnCodes = {
44
NOT_FOUND_KEY: 1,
@@ -24,15 +24,6 @@ export const warnMessages: { [code: number]: string } = {
2424
[CoreWarnCodes.EXPERIMENTAL_CUSTOM_MESSAGE_COMPILER]: `This project is using Custom Message Compiler, which is an experimental feature. It may receive breaking changes or be removed in the future.`
2525
}
2626

27-
const hasWarned: Record<string, boolean> = {}
28-
29-
export function warnOnce(msg: string) {
30-
if (!hasWarned[msg]) {
31-
hasWarned[msg] = true
32-
warn(msg)
33-
}
34-
}
35-
3627
export function getWarnMessage(
3728
code: CoreWarnCodes,
3829
...args: unknown[]

packages/message-compiler/src/errors.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
import { format } from '@intlify/shared'
22

3+
import type { BaseError } from '@intlify/shared'
34
import type { SourceLocation } from './location'
45

56
export type CompileDomain =
67
| 'tokenizer'
78
| 'parser'
89
| 'generator'
910
| 'transformer'
10-
| 'compiler'
11+
| 'optimizer'
12+
| 'minifier'
1113

12-
export interface CompileError extends SyntaxError {
13-
code: number
14+
export interface CompileError extends BaseError, SyntaxError {
1415
domain?: CompileDomain
1516
location?: SourceLocation
1617
}
1718

18-
export interface CreateCompileErrorOptions {
19+
export interface CompileErrorOptions {
1920
domain?: CompileDomain
2021
messages?: { [code: number]: string }
2122
args?: unknown[]
@@ -40,10 +41,16 @@ export const CompileErrorCodes = {
4041
UNEXPECTED_EMPTY_LINKED_KEY: 13,
4142
UNEXPECTED_LEXICAL_ANALYSIS: 14,
4243

44+
// generator error codes
45+
UNHANDLED_CODEGEN_NODE_TYPE: 15,
46+
47+
// minifier error codes
48+
UNHANDLED_MINIFIER_NODE_TYPE: 16,
49+
4350
// Special value for higher-order compilers to pick up the last code
4451
// to avoid collision of error codes. This should always be kept as the last
4552
// item.
46-
__EXTEND_POINT__: 15
53+
__EXTEND_POINT__: 17
4754
} as const
4855

4956
export type CompileErrorCodes =
@@ -66,13 +73,17 @@ export const errorMessages: { [code: number]: string } = {
6673
[CompileErrorCodes.MUST_HAVE_MESSAGES_IN_PLURAL]: `Plural must have messages`,
6774
[CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_MODIFIER]: `Unexpected empty linked modifier`,
6875
[CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_KEY]: `Unexpected empty linked key`,
69-
[CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS]: `Unexpected lexical analysis in token: '{0}'`
76+
[CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS]: `Unexpected lexical analysis in token: '{0}'`,
77+
// generator error messages
78+
[CompileErrorCodes.UNHANDLED_CODEGEN_NODE_TYPE]: `unhandled codegen node type: '{0}'`,
79+
// minimizer error messages
80+
[CompileErrorCodes.UNHANDLED_MINIFIER_NODE_TYPE]: `unhandled mimifier node type: '{0}'`
7081
}
7182

7283
export function createCompileError<T extends number>(
7384
code: T,
7485
loc: SourceLocation | null,
75-
options: CreateCompileErrorOptions = {}
86+
options: CompileErrorOptions = {}
7687
): CompileError {
7788
const { domain, messages, args } = options
7889
const msg = __DEV__

packages/message-compiler/src/generator.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { SourceMapGenerator } from 'source-map-js'
33
import { NodeTypes } from './nodes'
44
import { LOCATION_STUB } from './location'
55
import { HelperNameMap } from './helpers'
6+
import { createCompileError, CompileErrorCodes } from './errors'
67

78
import type { RawSourceMap } from 'source-map-js'
89
import type {
@@ -58,6 +59,8 @@ type CodeGenerator = {
5859
needIndent(): boolean
5960
}
6061

62+
export const ERROR_DOMAIN = 'parser'
63+
6164
function createCodeGenerator(
6265
ast: ResourceNode,
6366
options: CodeGenOptions
@@ -264,7 +267,14 @@ function generateNode(generator: CodeGenerator, node: Node): void {
264267
break
265268
default:
266269
if (__DEV__) {
267-
throw new Error(`unhandled codegen node type: ${node.type}`)
270+
throw createCompileError(
271+
CompileErrorCodes.UNHANDLED_CODEGEN_NODE_TYPE,
272+
null,
273+
{
274+
domain: ERROR_DOMAIN,
275+
args: [node.type]
276+
}
277+
)
268278
}
269279
}
270280
}

packages/message-compiler/src/minifier.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { NodeTypes } from './nodes'
2+
import { createCompileError, CompileErrorCodes } from './errors'
23

34
import type {
45
MessageNode,
@@ -14,6 +15,8 @@ import type {
1415
NamedNode
1516
} from './nodes'
1617

18+
export const ERROR_DOMAIN = 'minifier'
19+
1720
/* eslint-disable @typescript-eslint/no-explicit-any */
1821

1922
export function minify(node: Node) {
@@ -85,7 +88,14 @@ export function minify(node: Node) {
8588
break
8689
default:
8790
if (__DEV__) {
88-
throw new Error(`unhandled minify node type: ${node.type}`)
91+
throw createCompileError(
92+
CompileErrorCodes.UNHANDLED_MINIFIER_NODE_TYPE,
93+
null,
94+
{
95+
domain: ERROR_DOMAIN,
96+
args: [node.type]
97+
}
98+
)
8999
}
90100
}
91101

packages/message-compiler/test/generator.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
/* eslint-disable no-irregular-whitespace */
22

3+
import { format } from '@intlify/shared'
34
import { createParser } from '../src/parser'
45
import { transform } from '../src/transformer'
56
import { generate } from '../src/generator'
6-
import { SourceMapConsumer } from 'source-map-js'
77
import { CHAR_CR, CHAR_LF, CHAR_LS, CHAR_PS } from '../src/scanner'
8+
import { CompileErrorCodes, errorMessages } from '../src/errors'
9+
import { SourceMapConsumer } from 'source-map-js'
810

911
import type { RawSourceMap } from 'source-map-js'
12+
import type { ResourceNode } from '../src/nodes'
1013

1114
interface Pos {
1215
line: number
@@ -511,4 +514,17 @@ test('disable source map with location: false', async () => {
511514
})
512515
})
513516

517+
test('unhandle error', () => {
518+
// wrong node type
519+
const type = 1024
520+
const ast = {
521+
type
522+
} as unknown as ResourceNode
523+
expect(() =>
524+
generate(ast, { sourceMap: true, location: false })
525+
).toThrowError(
526+
format(errorMessages[CompileErrorCodes.UNHANDLED_CODEGEN_NODE_TYPE], type)
527+
)
528+
})
529+
514530
/* eslint-enable no-irregular-whitespace */

packages/message-compiler/test/minifier.test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import { format } from '@intlify/shared'
12
import { createParser } from '../src/parser'
23
import { optimize } from '../src/optimizer'
34
import { minify } from '../src/minifier'
5+
import { CompileErrorCodes, errorMessages } from '../src/errors'
46

5-
import type { MessageNode, PluralNode } from '../src/nodes'
7+
import type { MessageNode, PluralNode, ResourceNode } from '../src/nodes'
68

79
test('minify', () => {
810
const parser = createParser({ location: false })
@@ -16,3 +18,14 @@ test('minify', () => {
1618
.filter(Boolean)
1719
expect(messages).toEqual(['no apples'])
1820
})
21+
22+
test('unhandle error', () => {
23+
// wrong node type
24+
const type = 1024
25+
const ast = {
26+
type
27+
} as unknown as ResourceNode
28+
expect(() => minify(ast)).toThrowError(
29+
format(errorMessages[CompileErrorCodes.UNHANDLED_MINIFIER_NODE_TYPE], type)
30+
)
31+
})

packages/shared/src/error.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface BaseError {
2+
code: number
3+
}

packages/shared/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
export * from './utils'
2+
export * from './warn'
3+
export * from './error'
24
export * from './emittable'
35
export * from './emitter'

0 commit comments

Comments
 (0)