Skip to content

Commit

Permalink
Merge pull request #5 from playcanvas/fix-imports-strict
Browse files Browse the repository at this point in the history
Relaxes Type Validation
  • Loading branch information
marklundin authored Aug 16, 2024
2 parents c6b0e98 + d9f00ba commit 9847d35
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 28 deletions.
18 changes: 0 additions & 18 deletions src/global.types.js

This file was deleted.

9 changes: 2 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import * as ts from 'typescript';
import { createSystem, createDefaultMapFromNodeModules, createVirtualTypeScriptEnvironment } from '@typescript/vfs';
import globalTypes from './global.types.js';
import { ScriptParser } from './parsers/script-parser.js';
import { createDefaultMapFromCDN, flatMapAnyNodes, getExportedNodes, getType, inheritsFrom, isAliasedClassDeclaration } from './utils/ts-utils.js';

const toLowerCamelCase = str => str[0].toLowerCase() + str.substring(1);

const COMPILER_OPTIONS = {
strictPropertyInitialization: false, // Allow uninitialized properties
strict: false,
skipLibCheck: true, // Skip type checking of declaration files
target: ts.ScriptTarget.ES2022, // If this version changes, the types must be updated in the /rollup.config.mjs
module: ts.ModuleKind.CommonJS,
checkJs: true, // Enable JSDoc parsing
allowJs: true,
noImplicitAny: false, // Allow implicit any
baseUrl: './',
paths: {
"playcanvas": ["/playcanvas.js"]
Expand Down Expand Up @@ -55,9 +53,6 @@ export class JSDocParser {
const system = createSystem(fsMap);
this._env = createVirtualTypeScriptEnvironment(system, Array.from(fsMap.keys()), ts, COMPILER_OPTIONS);

// Add global types to the parser
this._env.createFile('/global.d.ts', globalTypes);

return this;
}

Expand Down Expand Up @@ -205,7 +200,7 @@ export class JSDocParser {
}

// Extract JSDoc tags
const tags = member.jsDoc.map(jsdoc => jsdoc.tags?.map(tag => tag.tagName.getText()) ?? []).flat();
const tags = member.jsDoc? member.jsDoc.map(jsdoc => jsdoc.tags?.map(tag => tag.tagName.getText()) ?? []).flat() : []

const name = member.name.getText();
const type = getType(member, typeChecker);
Expand Down
3 changes: 2 additions & 1 deletion src/parsers/script-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ export class ScriptParser {
validateProgram(errors = []) {

// Report any compiler errors
const compilerDiagnostics = this.program.getSemanticDiagnostics();
const compilerDiagnostics = this.program.getSyntacticDiagnostics();

if (compilerDiagnostics.length > 0) {
for (const diagnostic of compilerDiagnostics) {
const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/program.invalid.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Script } from 'playcanvas';

class Example extends Script {
// @ts-ignore
Class Intentionally extends Invalid {
/**
* @attribute
* @type {boolean}
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/program.valid.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Script } from 'playcanvas';

// The parser should ignore https imports and not throw an error
import confetti from "https://esm.sh/canvas-confetti@1.6.0"
import * as TWEEN from 'https://cdnjs.cloudflare.com/ajax/libs/tween.js/23.1.2/tween.esm.js'

class Example extends Script {
/**
Expand All @@ -12,7 +13,9 @@ class Example extends Script {

initialize() {
confetti();
new TWEEN.Tween({ x: 0 }).to({ x: 100 }, 1000).start();
}
}

export { Example };

2 changes: 1 addition & 1 deletion test/tests/invalid/program.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ describe('INVALID: Program ', function () {
it('only errors should exist (1)', function () {
expect(data).to.exist;
expect(data[0]).to.be.empty;
expect(data[1].length).to.equal(1);
expect(data[1].length).to.equal(4);
});
});

0 comments on commit 9847d35

Please sign in to comment.