@@ -24,6 +24,7 @@ import type {
24
24
BlockStatement ,
25
25
Decorator ,
26
26
CallExpression ,
27
+ SpreadElement ,
27
28
} from 'estree' ;
28
29
import type { ComponentMetaState , WireAdapter } from '../types' ;
29
30
@@ -42,54 +43,53 @@ function bMemberExpressionChain(props: string[]): MemberExpression {
42
43
43
44
function getWireParams (
44
45
node : MethodDefinition | PropertyDefinition
45
- ) : [ Expression , Expression | undefined ] {
46
+ ) : ( Expression | SpreadElement ) [ ] {
46
47
const { decorators } = node ;
47
48
48
49
if ( decorators . length > 1 ) {
49
50
throw generateError ( node , DecoratorErrors . ONE_WIRE_DECORATOR_ALLOWED ) ;
50
51
}
51
52
52
- // validate the parameters
53
+ // Before calling this function, we validate that it has exactly one decorator, @wire
53
54
const wireDecorator = decorators [ 0 ] . expression ;
54
55
if ( ! is . callExpression ( wireDecorator ) ) {
55
- // TODO [#5032]: Harmonize errors thrown in `@lwc/ssr-compiler`
56
- throw new Error ( 'todo - invalid usage' ) ;
56
+ throw generateError ( node , DecoratorErrors . FUNCTION_IDENTIFIER_SHOULD_BE_FIRST_PARAMETER ) ;
57
57
}
58
58
59
59
const args = wireDecorator . arguments ;
60
- if ( args . length === 0 || args . length > 2 ) {
61
- // TODO [#5032]: Harmonize errors thrown in `@lwc/ssr-compiler`
62
- throw new Error ( 'todo - wrong number of args' ) ;
60
+ if ( args . length === 0 ) {
61
+ throw generateError ( node , DecoratorErrors . ADAPTER_SHOULD_BE_FIRST_PARAMETER ) ;
63
62
}
64
63
65
- const [ id , config ] = args ;
66
- if ( is . spreadElement ( id ) || is . spreadElement ( config ) ) {
67
- // TODO [#5032]: Harmonize errors thrown in `@lwc/ssr-compiler`
68
- throw new Error ( 'todo - spread in params' ) ;
69
- }
70
- return [ id , config ] ;
64
+ return args ;
71
65
}
72
66
73
67
function validateWireId (
74
- id : Expression ,
68
+ id : Expression | SpreadElement ,
75
69
path : NodePath < PropertyDefinition | MethodDefinition >
76
70
) : asserts id is Identifier | MemberExpression {
77
71
// name of identifier or object used in member expression (e.g. "foo" for `foo.bar`)
78
72
let wireAdapterVar : string ;
79
73
80
74
if ( is . memberExpression ( id ) ) {
81
75
if ( id . computed ) {
82
- // TODO [#5032]: Harmonize errors thrown in `@lwc/ssr-compiler`
83
- throw new Error ( 'todo - FUNCTION_IDENTIFIER_CANNOT_HAVE_COMPUTED_PROPS' ) ;
76
+ throw generateError (
77
+ path . node ! ,
78
+ DecoratorErrors . FUNCTION_IDENTIFIER_CANNOT_HAVE_COMPUTED_PROPS
79
+ ) ;
84
80
}
85
81
if ( ! is . identifier ( id . object ) ) {
86
- // TODO [#5032]: Harmonize errors thrown in `@lwc/ssr-compiler`
87
- throw new Error ( 'todo - FUNCTION_IDENTIFIER_CANNOT_HAVE_NESTED_MEMBER_EXRESSIONS' ) ;
82
+ throw generateError (
83
+ path . node ! ,
84
+ DecoratorErrors . FUNCTION_IDENTIFIER_CANNOT_HAVE_NESTED_MEMBER_EXRESSIONS
85
+ ) ;
88
86
}
89
87
wireAdapterVar = id . object . name ;
90
88
} else if ( ! is . identifier ( id ) ) {
91
- // TODO [#5032]: Harmonize errors thrown in `@lwc/ssr-compiler`
92
- throw new Error ( 'todo - invalid adapter name' ) ;
89
+ throw generateError (
90
+ path . node ! ,
91
+ DecoratorErrors . FUNCTION_IDENTIFIER_SHOULD_BE_FIRST_PARAMETER
92
+ ) ;
93
93
} else {
94
94
wireAdapterVar = id . name ;
95
95
}
@@ -104,12 +104,11 @@ function validateWireId(
104
104
}
105
105
106
106
function validateWireConfig (
107
- config : Expression ,
107
+ config : Expression | SpreadElement | undefined ,
108
108
path : NodePath < PropertyDefinition | MethodDefinition >
109
109
) : asserts config is NoSpreadObjectExpression {
110
110
if ( ! is . objectExpression ( config ) ) {
111
- // TODO [#5032]: Harmonize errors thrown in `@lwc/ssr-compiler`
112
- throw new Error ( 'todo - CONFIG_OBJECT_SHOULD_BE_SECOND_PARAMETER' ) ;
111
+ throw generateError ( path . node ! , DecoratorErrors . CONFIG_OBJECT_SHOULD_BE_SECOND_PARAMETER ) ;
113
112
}
114
113
for ( const property of config . properties ) {
115
114
// Only validate computed object properties because static props are all valid
@@ -127,8 +126,10 @@ function validateWireConfig(
127
126
if ( is . templateLiteral ( key ) ) {
128
127
// A template literal is not guaranteed to always result in the same value
129
128
// (e.g. `${Math.random()}`), so we disallow them entirely.
130
- // TODO [#5032]: Harmonize errors thrown in `@lwc/ssr-compiler`
131
- throw new Error ( 'todo - COMPUTED_PROPERTY_CANNOT_BE_TEMPLATE_LITERAL' ) ;
129
+ throw generateError (
130
+ path . node ! ,
131
+ DecoratorErrors . COMPUTED_PROPERTY_CANNOT_BE_TEMPLATE_LITERAL
132
+ ) ;
132
133
} else if ( ! ( 'regex' in key ) ) {
133
134
// A literal can be a regexp, template literal, or primitive; only allow primitives
134
135
continue ;
0 commit comments