Skip to content

Commit

Permalink
Merge pull request #21 from playcanvas/fix-heritance-scripts
Browse files Browse the repository at this point in the history
Support Inherited Attributes across files
  • Loading branch information
marklundin authored Oct 24, 2024
2 parents 87cf365 + 0a56902 commit 963a3e9
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 15 deletions.
8 changes: 4 additions & 4 deletions src/parsers/script-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,17 +321,17 @@ export class ScriptParser {
return attributes;
}

const buffer = node.getSourceFile().getFullText();

// Find "/** */" style comments associated with this node.
const comments = getJSDocCommentRanges(node, buffer, this.typeChecker);
const comments = getJSDocCommentRanges(node, this.typeChecker);

// Parse the comments for attribute metadata
for (const comment of comments) {

const memberFileText = comment.member.getSourceFile().getFullText();

// Parse the comment for attribute metadata
const attributeMetadata = this.attributeParser.parseAttributeComment(
TextRange.fromStringRange(buffer, comment.range.pos, comment.range.end),
TextRange.fromStringRange(memberFileText, comment.range.pos, comment.range.end),
comment.member,
errors,
requiresAttributeTag
Expand Down
7 changes: 4 additions & 3 deletions src/utils/ts-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,20 +205,21 @@ function getSuperClasses(node, typeChecker) {
* @param {import('typescript').TypeChecker} typeChecker - The TypeScript type checker
* @returns {{ memberName: string, member: ts.Node, range: import('typescript').CommentRange}[]} - An array of comment ranges
*/
export function getJSDocCommentRanges(node, text, typeChecker) {
export function getJSDocCommentRanges(node, typeChecker) {
const commentRanges = [];

if (ts.isClassDeclaration(node) || ts.isInterfaceDeclaration(node)) {
// get an array of the class an all parent classed
const heritageChain = getSuperClasses(node, typeChecker);
const heritageChain = getSuperClasses(node, typeChecker)
.filter(classNode => !classNode.getSourceFile().isDeclarationFile);

// iterate over the heritance chain
heritageChain.forEach((classNode) => {
// for each class iterate over it's class members
classNode.members.forEach((member) => {
if (ts.isPropertyDeclaration(member) || ts.isSetAccessor(member) || ts.isPropertySignature(member)) {
const memberName = member.name && ts.isIdentifier(member.name) ? member.name.text : 'unnamed';
const ranges = getLeadingBlockCommentRanges(member, text);
const ranges = getLeadingBlockCommentRanges(member, member.getSourceFile().getFullText());
if (ranges.length > 0) {
commentRanges.push({
memberName,
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/example.dep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Script } from 'playcanvas';

export class Example extends Script {
/**
* @attribute
* @type {boolean}
*/
a;
}
8 changes: 1 addition & 7 deletions test/fixtures/inherit.valid.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import { Script } from 'playcanvas';

class Example extends Script {
/**
* @attribute
* @type {boolean}
*/
a;
}
import { Example } from './example.dep.js';

class ExampleExtended extends Example {
/**
Expand Down
10 changes: 9 additions & 1 deletion test/tests/valid/inherit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { parseAttributes } from '../../utils.js';
describe('VALID: Script inheritance attributes', function () {
let data;
before(async function () {
data = await parseAttributes('./inherit.valid.js');
data = await parseAttributes('./inherit.valid.js', './example.dep.js');
});

it('only results should exist', function () {
Expand All @@ -24,6 +24,14 @@ describe('VALID: Script inheritance attributes', function () {
it('ExampleExtended: should exist without errors', function () {
expect(data[0]?.exampleExtended).to.exist;
expect(data[0].exampleExtended.attributes).to.not.be.empty;
expect(data[0].exampleExtended.attributes.a).to.exist;
expect(data[0].exampleExtended.attributes.a.type).to.equal('boolean');
expect(data[0].exampleExtended.attributes.a.array).to.equal(false);
expect(data[0].exampleExtended.attributes.a.default).to.equal(false);
expect(data[0].exampleExtended.attributes.b).to.be.exists;
expect(data[0].exampleExtended.attributes.b.type).to.equal('number');
expect(data[0].exampleExtended.attributes.b.array).to.equal(false);
expect(data[0].exampleExtended.attributes.b.default).to.equal(0);
expect(data[0].exampleExtended.errors).to.be.empty;
});

Expand Down

0 comments on commit 963a3e9

Please sign in to comment.