@playcanvas/attribute-parser 1.3.3 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/rollup.config.js +6 -1
- package/src/parsers/attribute-parser.js +3 -2
- package/src/parsers/script-parser.js +13 -1
- package/test/fixtures/conditional.valid.js +22 -0
- package/test/fixtures/enum.valid.js +2 -1
- package/test/fixtures/enum.valid.ts +2 -1
- package/test/tests/valid/conditional.test.js +45 -0
- package/test/tests/valid/enum.test.js +2 -1
package/package.json
CHANGED
package/rollup.config.js
CHANGED
|
@@ -154,7 +154,7 @@ export class AttributeParser {
|
|
|
154
154
|
|
|
155
155
|
// Check if the declaration is a TypeScript enum
|
|
156
156
|
if (ts.isEnumDeclaration(declaration)) {
|
|
157
|
-
members = declaration.members.map(member => ({ [member.name.
|
|
157
|
+
members = declaration.members.map(member => ({ [member.name.text]: member.initializer.text }));
|
|
158
158
|
}
|
|
159
159
|
|
|
160
160
|
// Additionally check for JSDoc enum tag
|
|
@@ -187,7 +187,8 @@ export class AttributeParser {
|
|
|
187
187
|
node.properties.forEach((property) => {
|
|
188
188
|
|
|
189
189
|
if (ts.isPropertyAssignment(property)) {
|
|
190
|
-
const
|
|
190
|
+
const isValidName = ts.isIdentifier(property.name) || ts.isStringLiteral(property.name);
|
|
191
|
+
const name = isValidName && property.name.text;
|
|
191
192
|
const value = getLiteralValue(property.initializer, this.typeChecker);
|
|
192
193
|
members.push({ [name]: value });
|
|
193
194
|
}
|
|
@@ -147,7 +147,9 @@ const SUPPORTED_BLOCK_TAGS = new Map([
|
|
|
147
147
|
['size', 'number'],
|
|
148
148
|
['step', 'number'],
|
|
149
149
|
['title', 'string'],
|
|
150
|
-
['default', 'any']
|
|
150
|
+
['default', 'any'],
|
|
151
|
+
['enabledif', 'any'],
|
|
152
|
+
['visibleif', 'any']
|
|
151
153
|
]);
|
|
152
154
|
|
|
153
155
|
/**
|
|
@@ -202,6 +204,16 @@ const mapAttributesToOutput = (attribute) => {
|
|
|
202
204
|
delete attribute.default;
|
|
203
205
|
}
|
|
204
206
|
|
|
207
|
+
// enabledif is in a {}, so strip it out
|
|
208
|
+
if (attribute.enabledif?.startsWith('{') && attribute.enabledif?.endsWith('}')) {
|
|
209
|
+
attribute.enabledif = attribute.enabledif.slice(1, -1);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// enabledif is in a {}, so strip it out
|
|
213
|
+
if (attribute.visibleif?.startsWith('{') && attribute.visibleif?.endsWith('}')) {
|
|
214
|
+
attribute.visibleif = attribute.visibleif.slice(1, -1);
|
|
215
|
+
}
|
|
216
|
+
|
|
205
217
|
// remove typeName from the output
|
|
206
218
|
delete attribute.typeName;
|
|
207
219
|
delete attribute.value;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Script } from 'playcanvas';
|
|
2
|
+
|
|
3
|
+
class Example extends Script {
|
|
4
|
+
/**
|
|
5
|
+
* @attribute
|
|
6
|
+
* @visibleif {b === 2}
|
|
7
|
+
*/
|
|
8
|
+
a = 1;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @attribute
|
|
12
|
+
* @enabledif {a === 3}
|
|
13
|
+
*/
|
|
14
|
+
b = 1;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @attribute
|
|
18
|
+
*/
|
|
19
|
+
c = 1;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { Example };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { expect } from 'chai';
|
|
2
|
+
import { describe, it, before } from 'mocha';
|
|
3
|
+
|
|
4
|
+
import { parseAttributes } from '../../utils.js';
|
|
5
|
+
|
|
6
|
+
describe('VALID: Conditional Tags', function () {
|
|
7
|
+
let data;
|
|
8
|
+
before(async function () {
|
|
9
|
+
data = await parseAttributes('./conditional.valid.js');
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it('only results should exist', function () {
|
|
13
|
+
expect(data).to.exist;
|
|
14
|
+
expect(data[0]).to.not.be.empty;
|
|
15
|
+
expect(data[1]).to.be.empty;
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('Example: should exist without errors', function () {
|
|
19
|
+
expect(data[0]?.example).to.exist;
|
|
20
|
+
expect(data[0].example.attributes).to.not.be.empty;
|
|
21
|
+
expect(data[0].example.errors).to.be.empty;
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('a: should conditional `@visibleif` tag', function () {
|
|
25
|
+
expect(data[0].example.attributes.a).exist;
|
|
26
|
+
expect(data[0].example.attributes.a.name).to.equal('a');
|
|
27
|
+
expect(data[0].example.attributes.a.type).to.equal('number');
|
|
28
|
+
expect(data[0].example.attributes.a.visibleif).to.equal('b === 2');
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('b: should conditional `@enabledif` tag', function () {
|
|
32
|
+
expect(data[0].example.attributes.b).exist;
|
|
33
|
+
expect(data[0].example.attributes.b.name).to.equal('b');
|
|
34
|
+
expect(data[0].example.attributes.b.type).to.equal('number');
|
|
35
|
+
expect(data[0].example.attributes.b.enabledif).to.equal('a === 3');
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('c: should not include `@enabledif` or `@visibleif` tag if none is declared', function () {
|
|
39
|
+
expect(data[0].example.attributes.c).exist;
|
|
40
|
+
expect(data[0].example.attributes.c.name).to.equal('c');
|
|
41
|
+
expect(data[0].example.attributes.c.type).to.equal('number');
|
|
42
|
+
expect(data[0].example.attributes.c.enabledif).to.not.exist;
|
|
43
|
+
expect(data[0].example.attributes.c.visibleif).to.not.exist;
|
|
44
|
+
});
|
|
45
|
+
});
|
|
@@ -56,10 +56,11 @@ function runTests(fileName) {
|
|
|
56
56
|
expect(data[0].example.attributes.h.name).to.equal('h');
|
|
57
57
|
expect(data[0].example.attributes.h.type).to.equal('string');
|
|
58
58
|
expect(data[0].example.attributes.h.array).to.equal(false);
|
|
59
|
-
expect(data[0].example.attributes.h.enum).to.be.an('array').with.lengthOf(
|
|
59
|
+
expect(data[0].example.attributes.h.enum).to.be.an('array').with.lengthOf(4);
|
|
60
60
|
expect(data[0].example.attributes.h.enum[0]).to.deep.equal({ A: 'a' });
|
|
61
61
|
expect(data[0].example.attributes.h.enum[1]).to.deep.equal({ B: 'b' });
|
|
62
62
|
expect(data[0].example.attributes.h.enum[2]).to.deep.equal({ C: 'c' });
|
|
63
|
+
expect(data[0].example.attributes.h.enum[3]).to.deep.equal({ 'A string key': 'd' });
|
|
63
64
|
expect(data[0].example.attributes.h.default).to.equal('');
|
|
64
65
|
});
|
|
65
66
|
|