Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Script Re-exports #8

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
/**
* Returns all the valid ESM Scripts within a file
* @param {string} fileName - The file name in the program to check
* @returns {import('typescript').Node[]} - An array of any exported ESM Script nodes within the file
* @returns {Map<string, import('typescript').Node>} - A map of valid ESM Script <names, nodes> within the file
*/
getAllEsmScripts(fileName) {

Expand All @@ -123,9 +123,15 @@

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

const esmScripts = new Map()

Check failure on line 126 in src/index.js

View workflow job for this annotation

GitHub Actions / Unit Test

Missing semicolon
marklundin marked this conversation as resolved.
Show resolved Hide resolved
// Check if the file exports a class that inherits from `Script`
return Array.from(nodes).filter(node => isAliasedClassDeclaration(node, typeChecker) && inheritsFrom(node, typeChecker, esmScriptClass));
nodes.forEach((node, name) => {
if (isAliasedClassDeclaration(node, typeChecker) && inheritsFrom(node, typeChecker, esmScriptClass)) {
esmScripts.set(name, node);
}
});

return esmScripts;
}

/**
Expand All @@ -146,9 +152,8 @@
const nodes = this.getAllEsmScripts(fileName);

// Extract attributes from each script
nodes.forEach((node) => {
const name = toLowerCamelCase(node.name.text);
const opts = results[name] = { attributes: {}, errors: [] };
nodes.forEach((node, name) => {
const opts = results[toLowerCamelCase(name)] = { attributes: {}, errors: [] };
this.parser.extractAttributes(node, opts);
});

Expand Down
10 changes: 6 additions & 4 deletions src/utils/ts-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function resolveAliasedSymbol(typeChecker, symbol) {
* Returns an array of exported nodes from a TypeScript source file.
* @param {import('typescript').Program} program - The TypeScript program
* @param {import('typescript').SourceFile} sourceFile - The TypeScript source file
* @returns {Set<import('typescript').Node>} - A Set of exported nodes
* @returns {let map: Map<string, Set<import('typescript').Node>>} - A Map of exported nodes
*/
export function getExportedNodes(program, sourceFile) {
if (!program || !sourceFile) {
Expand All @@ -62,16 +62,18 @@ export function getExportedNodes(program, sourceFile) {
const exportedSymbols = typeChecker.getExportsOfModule(moduleSymbol);

// Find the actual declaration nodes for each exported symbol
const exportedNodes = [];
const exportedNodes = new Map();

exportedSymbols.forEach((symbol) => {
const resolvedSymbol = resolveAliasedSymbol(typeChecker, symbol);
if (resolvedSymbol.declarations) {
exportedNodes.push(...resolvedSymbol.declarations);
const node = resolvedSymbol.declarations[0];
const name = symbol.name === 'default' ? node.name.getText() : symbol.name;
exportedNodes.set(name, node);
}
});

return new Set(exportedNodes);
return exportedNodes;
}

/**
Expand Down
7 changes: 6 additions & 1 deletion test/fixtures/export.import.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import { Script } from 'playcanvas';

export class ExampleImport extends Script {}
export class ExampleImport extends Script {
/**
* @attribute
*/
prop = 10;
}
7 changes: 5 additions & 2 deletions test/fixtures/export.valid.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import { ExampleImport } from './export.import.js';

class ExampleImportExtend extends ExampleImport {}

export { ExampleImport as ExampleImportExportAs } from './export.import.js';
export { ExampleImport as ExampleImportAsExport } from './export.import.js';

class Example extends Script {}
class Example extends Script {
/** @attribute */
num = 10;
}

export default class ExampleDefault extends Script {}

Expand Down
14 changes: 11 additions & 3 deletions test/tests/valid/export.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('VALID: Export Script', function () {

it('Example: should exist without attributes or errors', function () {
expect(data[0]?.example).to.exist;
expect(data[0].example.attributes).to.be.empty;
expect(data[0].example.attributes).to.not.be.empty;
expect(data[0].example.errors).to.be.empty;
});
marklundin marked this conversation as resolved.
Show resolved Hide resolved

Expand All @@ -33,13 +33,21 @@ describe('VALID: Export Script', function () {
expect(data[0].exampleDefault.errors).to.be.empty;
});

it('ExampleImportAsExport: should not exist', function () {
expect(data[0]?.exampleImportAsExport).to.exist;
expect(data[0].exampleImportAsExport.attributes).to.not.be.empty;
expect(data[0].exampleImportAsExport.errors).to.be.empty;
});

it('ExampleExportAs: should not exist', function () {
expect(data[0]?.exampleExportAs).to.not.exist;
expect(data[0]?.exampleExportAs).to.exist;
expect(data[0].exampleExportAs.attributes).to.not.be.empty;
expect(data[0].exampleExportAs.errors).to.be.empty;
});

it('ExampleImport: should exist without attributes or errors', function () {
expect(data[0]?.exampleImport).to.exist;
expect(data[0].exampleImport.attributes).to.be.empty;
expect(data[0].exampleImport.attributes).to.not.be.empty;
expect(data[0].exampleImport.errors).to.be.empty;
});

Expand Down
Loading