Skip to content

Commit 34911de

Browse files
Templarianwjhsfekashida
authored
Update @lwc/lwc/valid-api eslint for data (#161)
* Update @lwc/lwc/valid-api eslint for data * Update lib/rules/valid-api.js Co-authored-by: Will Harney <62956339+wjhsf@users.noreply.github.com> * Update valid-api.js * Update valid-api.js * Update lib/rules/valid-api.js * Update lib/rules/valid-api.js * Update valid-api.js * Update lib/rules/valid-api.js Co-authored-by: Eugene Kashida <ekashida@gmail.com> * Update lib/rules/valid-api.js Co-authored-by: Will Harney <62956339+wjhsf@users.noreply.github.com> * Update valid-api.md * Update valid-api.js * Update valid-api.js --------- Co-authored-by: Will Harney <62956339+wjhsf@users.noreply.github.com> Co-authored-by: Eugene Kashida <ekashida@gmail.com>
1 parent bdc3168 commit 34911de

File tree

3 files changed

+35
-24
lines changed

3 files changed

+35
-24
lines changed

docs/rules/valid-api.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ The following restrictions apply to the `@api` decorator:
55
- Apply to class fields and class methods only.
66
- Fields and methods should be unique per class.
77
- Fields and methods can't start with `on`. The `on` prefix is reserved to bind event handlers.
8-
- Fields and methods can't start with `data`, `slot` or `part`. These names are reserved by LWC.
8+
- Fields and methods can't be named `slot`, `part` or `dataset`. These names are reserved by LWC.
9+
- Fields and methods can't start with `data[A-Z]`. These are mapped to the reflected property `this.dataset.*`.
910
- Boolean properties must be initialized with `false`. By initializing a public property to `true`, the consumer component can't set its value to `false` via the template.
1011

1112
## Rule details

lib/rules/valid-api.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const { isClassField } = require('../util/isClassField');
1313
/**
1414
* Set of APIs that we reserved for forward compatibility of LWC.
1515
*/
16-
const RESERVED_PUBLIC_PROPERTIES = new Set(['slot', 'part']);
16+
const RESERVED_PUBLIC_PROPERTIES = new Set(['slot', 'part', 'dataset']);
1717

1818
/**
1919
* Maps containing attributes that have a camelCased property mapping. We do not want users
@@ -68,17 +68,15 @@ function validatePropertyName(property, context) {
6868
});
6969
}
7070

71-
if (name.startsWith('data')) {
71+
if (RESERVED_PUBLIC_PROPERTIES.has(name)) {
7272
context.report({
7373
node: property,
74-
message: `Invalid public property "${name}". Properties starting with "data" are reserved properties.`,
74+
message: `Invalid public property "${name}". This property name is a reserved property.`,
7575
});
76-
}
77-
78-
if (RESERVED_PUBLIC_PROPERTIES.has(name)) {
76+
} else if (/^data[A-Z]/.test(name)) {
7977
context.report({
8078
node: property,
81-
message: `Invalid public property "${name}". This property name is a reserved property.`,
79+
message: `Invalid public property name "${name}". Properties starting with "data" correspond to data-* HTML attributes, which are not allowed.`,
8280
});
8381
}
8482

test/lib/rules/valid-api.js

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ testRule('valid-api', {
1313
{
1414
code: 'api();',
1515
},
16+
{
17+
code: `import { api } from 'lwc';
18+
class Foo {
19+
@api data
20+
}`,
21+
},
1622
{
1723
code: `import { api } from 'lwc';
1824
class Foo {
@@ -125,18 +131,6 @@ testRule('valid-api', {
125131
},
126132
],
127133
},
128-
{
129-
code: `import { api } from 'lwc';
130-
class Foo {
131-
@api data;
132-
}`,
133-
errors: [
134-
{
135-
message:
136-
'Invalid public property "data". Properties starting with "data" are reserved properties.',
137-
},
138-
],
139-
},
140134
{
141135
code: `import { api } from 'lwc';
142136
class Foo {
@@ -145,7 +139,7 @@ testRule('valid-api', {
145139
errors: [
146140
{
147141
message:
148-
'Invalid public property "dataFooBar". Properties starting with "data" are reserved properties.',
142+
'Invalid public property name "dataFooBar". Properties starting with "data" correspond to data-* HTML attributes, which are not allowed.',
149143
},
150144
],
151145
},
@@ -195,6 +189,18 @@ testRule('valid-api', {
195189
},
196190
],
197191
},
192+
{
193+
code: `import { api } from 'lwc';
194+
class Foo {
195+
@api dataset;
196+
}`,
197+
errors: [
198+
{
199+
message:
200+
'Invalid public property "dataset". This property name is a reserved property.',
201+
},
202+
],
203+
},
198204
{
199205
code: `import { api } from 'lwc';
200206
class Foo {
@@ -295,6 +301,12 @@ testRule('valid-api', {
295301

296302
testTypeScript('valid-api', {
297303
valid: [
304+
{
305+
code: `import { api } from 'lwc';
306+
class Foo {
307+
@api data: string
308+
}`,
309+
},
298310
{
299311
code: `import { api } from 'lwc';
300312
class Foo {
@@ -410,12 +422,12 @@ testTypeScript('valid-api', {
410422
{
411423
code: `import { api } from 'lwc';
412424
class Foo {
413-
@api data: string;
425+
@api dataset: string;
414426
}`,
415427
errors: [
416428
{
417429
message:
418-
'Invalid public property "data". Properties starting with "data" are reserved properties.',
430+
'Invalid public property "dataset". This property name is a reserved property.',
419431
},
420432
],
421433
},
@@ -427,7 +439,7 @@ testTypeScript('valid-api', {
427439
errors: [
428440
{
429441
message:
430-
'Invalid public property "dataFooBar". Properties starting with "data" are reserved properties.',
442+
'Invalid public property name "dataFooBar". Properties starting with "data" correspond to data-* HTML attributes, which are not allowed.',
431443
},
432444
],
433445
},

0 commit comments

Comments
 (0)