diff --git a/src/parsers/script-parser.js b/src/parsers/script-parser.js index f6bb31b..b00adc7 100644 --- a/src/parsers/script-parser.js +++ b/src/parsers/script-parser.js @@ -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 diff --git a/src/utils/ts-utils.js b/src/utils/ts-utils.js index 56689e6..2b3cae1 100644 --- a/src/utils/ts-utils.js +++ b/src/utils/ts-utils.js @@ -205,12 +205,13 @@ 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) => { @@ -218,7 +219,7 @@ export function getJSDocCommentRanges(node, text, typeChecker) { 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, diff --git a/test/fixtures/example.dep.js b/test/fixtures/example.dep.js new file mode 100644 index 0000000..339b53c --- /dev/null +++ b/test/fixtures/example.dep.js @@ -0,0 +1,9 @@ +import { Script } from 'playcanvas'; + +export class Example extends Script { + /** + * @attribute + * @type {boolean} + */ + a; +} diff --git a/test/fixtures/inherit.valid.js b/test/fixtures/inherit.valid.js index e9e7d32..e36fd9f 100644 --- a/test/fixtures/inherit.valid.js +++ b/test/fixtures/inherit.valid.js @@ -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 { /** diff --git a/test/tests/valid/inherit.test.js b/test/tests/valid/inherit.test.js index d3e6d39..4df0fbb 100644 --- a/test/tests/valid/inherit.test.js +++ b/test/tests/valid/inherit.test.js @@ -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 () { @@ -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; });