Skip to content

Commit

Permalink
Update attribute-parser.js and enum.valid.js
Browse files Browse the repository at this point in the history
  • Loading branch information
marklundin committed Sep 18, 2024
1 parent 4d36fdb commit f235b9b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
18 changes: 10 additions & 8 deletions src/parsers/attribute-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as ts from 'typescript';
import { ParsingError } from './parsing-error.js';
import { hasTag } from '../utils/attribute-utils.js';
import { parseTag, validateTag } from '../utils/tag-utils.js';
import { extractTextFromDocNode, getLeadingBlockCommentRanges, getType } from '../utils/ts-utils.js';
import { extractTextFromDocNode, getLeadingBlockCommentRanges, getLiteralValue, getType } from '../utils/ts-utils.js';

/**
* A class to parse JSDoc comments and extract attribute metadata.
Expand Down Expand Up @@ -260,14 +260,16 @@ export class AttributeParser {

const node = property.initializer;

value = getLiteralValue(node, this.typeChecker);

Check failure on line 263 in src/parsers/attribute-parser.js

View workflow job for this annotation

GitHub Actions / Unit Test

'value' is never reassigned. Use 'const' instead

// Enums can only contain primitives (string|number|boolean)
if (ts.isNumericLiteral(node)) {
value = parseFloat(node.getText());
} else if (node.kind === ts.SyntaxKind.TrueKeyword || node.kind === ts.SyntaxKind.FalseKeyword) {
value = node.kind === ts.SyntaxKind.TrueKeyword;
} else {
value = node.getText();
}
// if (ts.isNumericLiteral(node)) {
// value = parseFloat(node.getText());
// } else if (node.kind === ts.SyntaxKind.TrueKeyword || node.kind === ts.SyntaxKind.FalseKeyword) {
// value = node.kind === ts.SyntaxKind.TrueKeyword;
// } else {
// value = node.getText();
// }

members.push({ [name]: value });
}
Expand Down
6 changes: 3 additions & 3 deletions test/fixtures/enum.valid.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { Script, Vec3 } from 'playcanvas';
* @enum {number}
*/
const NumberEnum = {
A: 0,
B: 1,
C: 2
A: 13,
B: 14,
C: 23
};

/**
Expand Down
6 changes: 5 additions & 1 deletion test/tests/valid/enum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('VALID: Enum attribute', function () {
expect(data[0].example.attributes.e.name).to.equal('e');
expect(data[0].example.attributes.e.type).to.equal('number');
expect(data[0].example.attributes.e.array).to.equal(false);
expect(data[0].example.attributes.e.default).to.equal(0);
expect(data[0].example.attributes.e.default).to.equal(13);
});

it('f: should be a enum attribute with a default value', function () {
Expand All @@ -51,6 +51,10 @@ describe('VALID: Enum attribute', function () {
expect(data[0].example.attributes.h.name).to.equal('h');
expect(data[0].example.attributes.h.type).to.equal('string');
expect(data[0].example.attributes.h.array).to.equal(false);
expect(data[0].example.attributes.h.enum).to.be.an('array').with.lengthOf(3)

Check failure on line 54 in test/tests/valid/enum.test.js

View workflow job for this annotation

GitHub Actions / Unit Test

Missing semicolon
expect(data[0].example.attributes.h.enum[0]).to.deep.equal({ A: 'a' });
expect(data[0].example.attributes.h.enum[1]).to.deep.equal({ B: 'b' });
expect(data[0].example.attributes.h.enum[2]).to.deep.equal({ C: 'c' });
expect(data[0].example.attributes.h.default).to.equal('');
});

Expand Down

0 comments on commit f235b9b

Please sign in to comment.