Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String Enum Fix #17

Merged
merged 3 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"engines": {
"node": ">=18.0.0"
},
"version": "1.0.8",
"version": "1.1.0",
"dependencies": {
"@microsoft/tsdoc": "^0.15.0",
"@playcanvas/eslint-config": "^1.7.4",
Expand Down
16 changes: 2 additions & 14 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 @@ -256,19 +256,7 @@ export class AttributeParser {

if (ts.isPropertyAssignment(property)) {
const name = property.name && ts.isIdentifier(property.name) && property.name.text;
let value;

const node = property.initializer;

// 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();
}

const value = getLiteralValue(property.initializer, this.typeChecker);
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);
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