From 4d36fdb7925ba8205865f192df5f3267beba68de Mon Sep 17 00:00:00 2001 From: Mark Lundin Date: Wed, 18 Sep 2024 13:02:12 +0100 Subject: [PATCH 1/3] 1.1.0 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 1db1b46..3eee914 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@playcanvas/attribute-parser", - "version": "1.0.8", + "version": "1.1.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@playcanvas/attribute-parser", - "version": "1.0.8", + "version": "1.1.0", "dependencies": { "@microsoft/tsdoc": "^0.15.0", "@playcanvas/eslint-config": "^1.7.4", diff --git a/package.json b/package.json index f325b86..1789894 100644 --- a/package.json +++ b/package.json @@ -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", From f235b9b4c5cf05115133722b13c59473a168f4cf Mon Sep 17 00:00:00 2001 From: Mark Lundin Date: Wed, 18 Sep 2024 16:45:36 +0100 Subject: [PATCH 2/3] Update attribute-parser.js and enum.valid.js --- src/parsers/attribute-parser.js | 18 ++++++++++-------- test/fixtures/enum.valid.js | 6 +++--- test/tests/valid/enum.test.js | 6 +++++- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/parsers/attribute-parser.js b/src/parsers/attribute-parser.js index f97190d..e8d7278 100644 --- a/src/parsers/attribute-parser.js +++ b/src/parsers/attribute-parser.js @@ -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. @@ -260,14 +260,16 @@ export class AttributeParser { const node = property.initializer; + value = getLiteralValue(node, this.typeChecker); + // 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 }); } diff --git a/test/fixtures/enum.valid.js b/test/fixtures/enum.valid.js index 251107f..7cb11e3 100644 --- a/test/fixtures/enum.valid.js +++ b/test/fixtures/enum.valid.js @@ -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 }; /** diff --git a/test/tests/valid/enum.test.js b/test/tests/valid/enum.test.js index bcb113d..696574f 100644 --- a/test/tests/valid/enum.test.js +++ b/test/tests/valid/enum.test.js @@ -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 () { @@ -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(''); }); From 830c651bff0a2e855df669482102244cedaba4dc Mon Sep 17 00:00:00 2001 From: Mark Lundin Date: Wed, 18 Sep 2024 16:55:19 +0100 Subject: [PATCH 3/3] linting --- src/parsers/attribute-parser.js | 16 +--------------- test/tests/valid/enum.test.js | 2 +- 2 files changed, 2 insertions(+), 16 deletions(-) diff --git a/src/parsers/attribute-parser.js b/src/parsers/attribute-parser.js index e8d7278..2531f80 100644 --- a/src/parsers/attribute-parser.js +++ b/src/parsers/attribute-parser.js @@ -256,21 +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; - - value = getLiteralValue(node, this.typeChecker); - - // 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 }); } }); diff --git a/test/tests/valid/enum.test.js b/test/tests/valid/enum.test.js index 696574f..656c2f7 100644 --- a/test/tests/valid/enum.test.js +++ b/test/tests/valid/enum.test.js @@ -51,7 +51,7 @@ 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).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' });