Skip to content

Commit d24a76c

Browse files
authored
Merge pull request #37 from playcanvas/feat-script-name
Parse static scriptName field
2 parents cc9aea1 + 0f4a20f commit d24a76c

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

src/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,26 @@ export class JSDocParser {
143143

144144
const esmScriptClass = pcTypes.statements.find(node => node.kind === ts.SyntaxKind.ClassDeclaration && node.name.text === 'Script')?.symbol;
145145

146+
const isScriptNameMember = member => (
147+
ts.isPropertyDeclaration(member) && // Is a property declaration
148+
ts.isIdentifier(member.name) &&
149+
member.name.text === 'scriptName' &&
150+
(ts.getCombinedModifierFlags(member) & ts.ModifierFlags.Static) !== 0 &&
151+
ts.isStringLiteral(member.initializer)
152+
);
153+
146154
// Check if the file exports a class that inherits from `Script`
147155
nodes.forEach((node, name) => {
148156
if (isAliasedClassDeclaration(node, typeChecker) && inheritsFrom(node, typeChecker, esmScriptClass)) {
157+
158+
// Check for a static scriptName property and use that as the name if it exists
159+
const scriptNameMember = node.members.find(isScriptNameMember);
160+
161+
// If the scriptName property exists, use that as the name
162+
if (scriptNameMember) {
163+
name = scriptNameMember.initializer.text;
164+
}
165+
149166
esmScripts.set(name, node);
150167
}
151168
});

test/fixtures/program.valid.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { Script } from 'playcanvas';
66
export const MyEnum = { value: 0 };
77

88
class Example extends Script {
9+
static scriptName = 'customExample';
10+
911
/**
1012
* @attribute
1113
* @precision 1

test/tests/valid/program.test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,15 @@ describe('VALID: Program ', function () {
99
data = await parseAttributes('./program.valid.js');
1010
});
1111

12+
it('should have a customExample script', function () {
13+
expect(data[0].customExample).to.exist;
14+
expect(data[0].customExample.errors).to.be.empty;
15+
});
16+
1217
it('only results should exist', function () {
1318
expect(data).to.exist;
1419
expect(data[0]).to.not.be.empty;
1520
expect(data[1]).to.be.empty;
16-
expect(data[0].example.errors).to.be.empty;
21+
expect(data[0].customExample.errors).to.be.empty;
1722
});
1823
});

0 commit comments

Comments
 (0)