@playcanvas/attribute-parser 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "engines": {
6
6
  "node": ">=18.0.0"
7
7
  },
8
- "version": "1.0.2",
8
+ "version": "1.0.4",
9
9
  "dependencies": {
10
10
  "@microsoft/tsdoc": "^0.15.0",
11
11
  "@typescript/vfs": "^1.6.0",
@@ -0,0 +1,18 @@
1
+ /**
2
+ * These types are use to extend the global types in the program being parsed.
3
+ * It's useful to add types to modules that are not written in TypeScript.
4
+ */
5
+
6
+ // This allows http/s modules to be imported without TypeScript errors
7
+
8
+ export default `
9
+ declare module 'http://*' {
10
+ const value: any;
11
+ export default value;
12
+ }
13
+
14
+ declare module 'https://*' {
15
+ const value: any;
16
+ export default value;
17
+ }
18
+ `
package/src/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as ts from 'typescript';
2
2
  import { createSystem, createDefaultMapFromNodeModules, createVirtualTypeScriptEnvironment } from '@typescript/vfs';
3
-
3
+ import globalTypes from './global.types.js';
4
4
  import { ScriptParser } from './parsers/script-parser.js';
5
5
  import { createDefaultMapFromCDN, flatMapAnyNodes, getExportedNodes, getType, inheritsFrom, isAliasedClassDeclaration } from './utils/ts-utils.js';
6
6
 
@@ -8,6 +8,7 @@ const toLowerCamelCase = str => str[0].toLowerCase() + str.substring(1);
8
8
 
9
9
  const COMPILER_OPTIONS = {
10
10
  strictPropertyInitialization: false, // Allow uninitialized properties
11
+ skipLibCheck: true, // Skip type checking of declaration files
11
12
  target: ts.ScriptTarget.ES2022, // If this version changes, the types must be updated in the /rollup.config.mjs
12
13
  module: ts.ModuleKind.CommonJS,
13
14
  checkJs: true, // Enable JSDoc parsing
@@ -32,14 +33,20 @@ export class JSDocParser {
32
33
  */
33
34
  _errors = [];
34
35
 
35
- async init(basePath = '') {
36
+ /**
37
+ * Initializes the JSDocParser with the standard library files
38
+ *
39
+ * @param {string} libPath - The path to standard library files
40
+ * @returns {Promise<JSDocParser>} - The initialized JSDocParser
41
+ */
42
+ async init(libPath = '') {
36
43
  if (this._env) {
37
- return;
44
+ return this;
38
45
  }
39
46
 
40
47
  let fsMap;
41
- if (basePath) {
42
- fsMap = await createDefaultMapFromCDN({ target: ts.ScriptTarget.ES2022 }, basePath, ts);
48
+ if (libPath) {
49
+ fsMap = await createDefaultMapFromCDN({ target: ts.ScriptTarget.ES2022 }, libPath, ts);
43
50
  } else {
44
51
  fsMap = await createDefaultMapFromNodeModules(COMPILER_OPTIONS, ts);
45
52
  }
@@ -47,6 +54,11 @@ export class JSDocParser {
47
54
  // Set up the virtual file system and environment
48
55
  const system = createSystem(fsMap);
49
56
  this._env = createVirtualTypeScriptEnvironment(system, Array.from(fsMap.keys()), ts, COMPILER_OPTIONS);
57
+
58
+ // Add global types to the parser
59
+ this._env.createFile('/global.d.ts', globalTypes);
60
+
61
+ return this;
50
62
  }
51
63
 
52
64
  /**
@@ -190,6 +190,11 @@ const mapAttributesToOutput = (attribute) => {
190
190
  // set the default value
191
191
  if (attribute.value !== undefined) attribute.default = attribute.value;
192
192
 
193
+ // Curve Attributes specifically should not expose a default value if it's an empty array
194
+ if (attribute.type === 'curve' && Array.isArray(attribute.value) && attribute.value.length === 0) {
195
+ delete attribute.default;
196
+ }
197
+
193
198
  // remove typeName from the output
194
199
  delete attribute.typeName;
195
200
  delete attribute.value;
@@ -21,6 +21,7 @@ class Example extends Script {
21
21
  * @resource
22
22
  */
23
23
  c;
24
+
24
25
  }
25
26
 
26
27
  export { Example };
@@ -0,0 +1,18 @@
1
+ import { Script } from 'playcanvas';
2
+
3
+ // The parser should ignore https imports and not throw an error
4
+ import confetti from "https://esm.sh/canvas-confetti@1.6.0"
5
+
6
+ class Example extends Script {
7
+ /**
8
+ * @attribute
9
+ * @type {boolean}
10
+ */
11
+ a = false;
12
+
13
+ initialize() {
14
+ confetti();
15
+ }
16
+ }
17
+
18
+ export { Example };
@@ -27,7 +27,7 @@ describe('VALID: Curve attribute', function () {
27
27
  expect(data[0].example.attributes.a.name).to.equal('a');
28
28
  expect(data[0].example.attributes.a.type).to.equal('curve');
29
29
  expect(data[0].example.attributes.a.array).to.equal(false);
30
- expect(data[0].example.attributes.a.default).to.eql([]);
30
+ expect(data[0].example.attributes.a.default).to.not.exist;
31
31
  });
32
32
 
33
33
  it('b: should be a curve attribute with a default value', function () {
@@ -43,7 +43,7 @@ describe('VALID: Curve attribute', function () {
43
43
  expect(data[0].example.attributes.c.name).to.equal('c');
44
44
  expect(data[0].example.attributes.c.type).to.equal('curve');
45
45
  expect(data[0].example.attributes.c.array).to.equal(false);
46
- expect(data[0].example.attributes.c.default).to.eql([]);
46
+ expect(data[0].example.attributes.a.default).to.not.exist;
47
47
  expect(data[0].example.attributes.c.color).to.equal('r');
48
48
  });
49
49
 
@@ -52,7 +52,7 @@ describe('VALID: Curve attribute', function () {
52
52
  expect(data[0].example.attributes.d.name).to.equal('d');
53
53
  expect(data[0].example.attributes.d.type).to.equal('curve');
54
54
  expect(data[0].example.attributes.d.array).to.equal(false);
55
- expect(data[0].example.attributes.d.default).to.eql([]);
55
+ expect(data[0].example.attributes.a.default).to.not.exist;
56
56
  expect(data[0].example.attributes.d.color).to.equal('rgb');
57
57
  expect(data[0].example.attributes.d.curves).to.deep.equal(['x', 'y', 'z']);
58
58
  });
@@ -63,7 +63,7 @@ describe('VALID: Curve attribute', function () {
63
63
  expect(data[0].example.attributes.e.type).to.equal('curve');
64
64
  expect(data[0].example.attributes.e.array).to.equal(true);
65
65
  expect(data[0].example.attributes.e.size).to.equal(2);
66
- expect(data[0].example.attributes.e.default).equal(null);
66
+ expect(data[0].example.attributes.e.default).to.not.exist;
67
67
  });
68
68
 
69
69
  it('f: should be a curve attribute in an unsual format', function () {
@@ -0,0 +1,18 @@
1
+ /* eslint-disable no-unused-expressions */
2
+ import { describe, it, before } from 'mocha';
3
+ import { expect } from 'chai';
4
+
5
+ import { parseAttributes } from '../../utils.js';
6
+
7
+ describe('VALID: Program ', function () {
8
+ let data;
9
+ before(async function () {
10
+ data = await parseAttributes('./program.valid.js');
11
+ });
12
+
13
+ it('only results should exist', function () {
14
+ expect(data).to.exist;
15
+ expect(data[0]).to.not.be.empty;
16
+ expect(data[1]).to.be.empty;
17
+ });
18
+ });