Skip to content

Commit

Permalink
Fix import in attribute-parser.js and remove unused code in ts-utils.js
Browse files Browse the repository at this point in the history
  • Loading branch information
marklundin committed Sep 11, 2024
1 parent 8b74d69 commit ceb8f9f
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 31 deletions.
8 changes: 3 additions & 5 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, getPrimitiveEnumType } from '../utils/ts-utils.js';
import { extractTextFromDocNode, getLeadingBlockCommentRanges, getType } from '../utils/ts-utils.js';

/**
* A class to parse JSDoc comments and extract attribute metadata.
Expand Down Expand Up @@ -162,12 +162,10 @@ export class AttributeParser {
getNodeAsAttribute(node, errors = []) {

const name = node.name && ts.isIdentifier(node.name) && node.name.text;
let { type, name: typeName, array } = getType(node, this.typeChecker);
const { type, name: typeName, array } = getType(node, this.typeChecker);
const enums = this.getEnumMembers(node, errors);
let value = null;

// if(enums.length > 0 ) typeName =

// we don't need to serialize the value for arrays
const serializer = !array && this.typeSerializerMap.get(typeName);
if (serializer) {
Expand All @@ -194,7 +192,7 @@ export class AttributeParser {
let members = [];

// Check if there's a type annotation directly on the variable declaration
if (/*ts.isVariableDeclaration(node) && */node.type) {
if (node.type) {
typeNode = node;
} else {
// Check for JSDoc annotations
Expand Down
32 changes: 16 additions & 16 deletions src/utils/ts-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,30 +309,30 @@ export function isEnum(node) {

return false;
}

/**
* Determines the primitive type for enums, or falls back to the actual type name.
*
* @param {ts.Type} type - The type to inspect.
* @param {ts.TypeChecker} typeChecker - The TypeScript type checker.
* @returns {string} - The primitive type of the enum or the type's name.
* @returns {'string' | 'boolean' | 'number' | null} - The primitive type of the enum or the type's name.
*/
export function getPrimitiveEnumType(type, typeChecker) {
// Check if the type is an enum type
if (type.symbol?.declarations?.some(decl => ts.isEnumDeclaration(decl))) {
// Get the type of enum members
const enumMembers = type.symbol.declarations[0].members;
const firstMemberValue = typeChecker.getConstantValue(enumMembers[0]);

const validEnumType = [
'number',
'string',
'boolean'
]

if (validEnumType.includes(typeof firstMemberValue)) {
return typeof firstMemberValue;
}
}
if (!type.symbol?.declarations?.some(decl => ts.isEnumDeclaration(decl))) return null;

// Get the type of enum members
const enumMembers = type.symbol.declarations[0].members;
const firstMemberValue = typeChecker.getConstantValue(enumMembers[0]);

const validEnumType = [
'number',
'string',
'boolean'
];

const typeOf = typeof firstMemberValue;
return validEnumType.includes(typeOf) ? typeOf : null;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/enum.valid.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Script, Vec3 } from 'playcanvas';
import { Script } from 'playcanvas';

/**
* @enum {number}
Expand Down
6 changes: 3 additions & 3 deletions test/tests/valid/asset.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { parseAttributes } from '../../utils.js';

function runTests(fileName) {

const isTS= fileName.endsWith('.ts');
const script = isTS ? 'TypeScript' : 'JavaScript';
const isTS = fileName.endsWith('.ts');
const script = isTS ? 'TS' : 'JS';

describe(`${script}: VALID: Asset attribute`, function () {
let data;
Expand Down Expand Up @@ -53,7 +53,7 @@ function runTests(fileName) {
});

});
};
}

runTests('./asset.valid.js');
runTests('./asset.valid.ts');
4 changes: 2 additions & 2 deletions test/tests/valid/checkbox.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { parseAttributes } from '../../utils.js';

function runTests(fileName) {

const isTS= fileName.endsWith('.ts');
const script = isTS ? 'TypeScript' : 'JavaScript';
const isTS = fileName.endsWith('.ts');
const script = isTS ? 'TS' : 'JS';

describe(`${script}: VALID: Checkbox attribute`, function () {
let data;
Expand Down
4 changes: 2 additions & 2 deletions test/tests/valid/enum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { parseAttributes } from '../../utils.js';

function runTests(fileName) {

const isTS= fileName.endsWith('.ts');
const script = isTS ? 'TypeScript' : 'JavaScript';
const isTS = fileName.endsWith('.ts');
const script = isTS ? 'TS' : 'JS';

describe(`${script}: VALID: Enum attribute`, function () {
let data;
Expand Down
4 changes: 2 additions & 2 deletions test/tests/valid/json.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { describe, it, before } from 'mocha';

import { parseAttributes } from '../../utils.js';

describe(`Javascript: VALID: Asset attribute`, function () {
describe('JS: VALID: Asset attribute', function () {

let data;
before(async function () {
Expand Down Expand Up @@ -150,7 +150,7 @@ describe(`Javascript: VALID: Asset attribute`, function () {
});


describe(`Typescript: VALID: Asset attribute`, function () {
describe('TS: VALID: Asset attribute', function () {

let data;
before(async function () {
Expand Down

0 comments on commit ceb8f9f

Please sign in to comment.