From 4cfb3e7718dbab742815ee33e569c410936d51ce Mon Sep 17 00:00:00 2001 From: Mark Lundin Date: Wed, 9 Oct 2024 16:52:55 +0100 Subject: [PATCH 1/4] Fix JSDoc comment parsing issue in ScriptParser and getJSDocCommentRanges --- src/parsers/script-parser.js | 8 +++++--- src/utils/ts-utils.js | 7 ++++--- test/fixtures/example.dep.js | 9 +++++++++ test/fixtures/inherit.valid.js | 9 +-------- test/tests/valid/inherit.test.js | 2 +- 5 files changed, 20 insertions(+), 15 deletions(-) create mode 100644 test/fixtures/example.dep.js diff --git a/src/parsers/script-parser.js b/src/parsers/script-parser.js index f6bb31b..8ba7e0a 100644 --- a/src/parsers/script-parser.js +++ b/src/parsers/script-parser.js @@ -321,17 +321,19 @@ export class ScriptParser { return attributes; } - const buffer = node.getSourceFile().getFullText(); + // 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..1b099e9 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..6390794 --- /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; +} \ No newline at end of file diff --git a/test/fixtures/inherit.valid.js b/test/fixtures/inherit.valid.js index e9e7d32..af4c9dc 100644 --- a/test/fixtures/inherit.valid.js +++ b/test/fixtures/inherit.valid.js @@ -1,12 +1,5 @@ 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..c59dcd0 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 () { From e2371e4eaaa5c1f08f604888486e8765f18249dd Mon Sep 17 00:00:00 2001 From: Mark Lundin Date: Wed, 9 Oct 2024 16:53:34 +0100 Subject: [PATCH 2/4] linting --- src/utils/ts-utils.js | 2 +- test/fixtures/example.dep.js | 4 ++-- test/fixtures/inherit.valid.js | 1 + 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/utils/ts-utils.js b/src/utils/ts-utils.js index 1b099e9..2b3cae1 100644 --- a/src/utils/ts-utils.js +++ b/src/utils/ts-utils.js @@ -211,7 +211,7 @@ export function getJSDocCommentRanges(node, typeChecker) { if (ts.isClassDeclaration(node) || ts.isInterfaceDeclaration(node)) { // get an array of the class an all parent classed const heritageChain = getSuperClasses(node, typeChecker) - .filter(classNode => !classNode.getSourceFile().isDeclarationFile) + .filter(classNode => !classNode.getSourceFile().isDeclarationFile); // iterate over the heritance chain heritageChain.forEach((classNode) => { diff --git a/test/fixtures/example.dep.js b/test/fixtures/example.dep.js index 6390794..339b53c 100644 --- a/test/fixtures/example.dep.js +++ b/test/fixtures/example.dep.js @@ -1,4 +1,4 @@ -import { Script } from 'playcanvas' +import { Script } from 'playcanvas'; export class Example extends Script { /** @@ -6,4 +6,4 @@ export class Example extends Script { * @type {boolean} */ a; -} \ No newline at end of file +} diff --git a/test/fixtures/inherit.valid.js b/test/fixtures/inherit.valid.js index af4c9dc..e36fd9f 100644 --- a/test/fixtures/inherit.valid.js +++ b/test/fixtures/inherit.valid.js @@ -1,4 +1,5 @@ import { Script } from 'playcanvas'; + import { Example } from './example.dep.js'; class ExampleExtended extends Example { From 50f27442f6031161b39b1101290828a054d2bcba Mon Sep 17 00:00:00 2001 From: Mark Lundin Date: Wed, 9 Oct 2024 17:24:28 +0100 Subject: [PATCH 3/4] Add attributes to ExampleExtended class --- test/tests/valid/inherit.test.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/tests/valid/inherit.test.js b/test/tests/valid/inherit.test.js index c59dcd0..4df0fbb 100644 --- a/test/tests/valid/inherit.test.js +++ b/test/tests/valid/inherit.test.js @@ -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; }); From 0a569023b42ebc70dc72d8c1ef0a2e2c78f4f979 Mon Sep 17 00:00:00 2001 From: Mark Lundin Date: Wed, 9 Oct 2024 17:31:31 +0100 Subject: [PATCH 4/4] Remove unused code in script-parser.js --- src/parsers/script-parser.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/parsers/script-parser.js b/src/parsers/script-parser.js index 8ba7e0a..b00adc7 100644 --- a/src/parsers/script-parser.js +++ b/src/parsers/script-parser.js @@ -321,8 +321,6 @@ export class ScriptParser { return attributes; } - // const buffer = node.getSourceFile().getFullText(); - // Find "/** */" style comments associated with this node. const comments = getJSDocCommentRanges(node, this.typeChecker);