@playcanvas/attribute-parser 1.0.0
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/.github/workflows/ci.yml +28 -0
- package/README.md +76 -0
- package/package.json +24 -0
- package/src/index.js +226 -0
- package/src/parsers/attribute-parser.js +280 -0
- package/src/parsers/parsing-error.js +14 -0
- package/src/parsers/script-parser.js +404 -0
- package/src/utils/attribute-utils.js +72 -0
- package/src/utils/generic-utils.js +18 -0
- package/src/utils/tag-utils.js +65 -0
- package/src/utils/ts-utils.js +501 -0
- package/test/fixtures/asset.invalid.js +26 -0
- package/test/fixtures/asset.valid.js +25 -0
- package/test/fixtures/checkbox.valid.js +23 -0
- package/test/fixtures/color.valid.js +23 -0
- package/test/fixtures/curve.invalid.js +28 -0
- package/test/fixtures/curve.valid.js +49 -0
- package/test/fixtures/entity.valid.js +23 -0
- package/test/fixtures/enum.valid.js +78 -0
- package/test/fixtures/export.import.js +3 -0
- package/test/fixtures/export.valid.js +23 -0
- package/test/fixtures/inherit.valid.js +52 -0
- package/test/fixtures/intellisense.valid.js +37 -0
- package/test/fixtures/json.invalid.js +22 -0
- package/test/fixtures/json.valid.js +103 -0
- package/test/fixtures/misc.invalid.js +25 -0
- package/test/fixtures/misc.valid.js +33 -0
- package/test/fixtures/numeric.invalid.js +38 -0
- package/test/fixtures/numeric.valid.js +63 -0
- package/test/fixtures/playcanvas.d.ts +47963 -0
- package/test/fixtures/program.invalid.js +11 -0
- package/test/fixtures/text.valid.js +30 -0
- package/test/fixtures/vector.invalid.js +13 -0
- package/test/fixtures/vector.valid.js +23 -0
- package/test/tests/invalid/asset.test.js +48 -0
- package/test/tests/invalid/curve.test.js +24 -0
- package/test/tests/invalid/json.test.js +28 -0
- package/test/tests/invalid/misc.test.js +42 -0
- package/test/tests/invalid/numeric.test.js +56 -0
- package/test/tests/invalid/program.test.js +18 -0
- package/test/tests/invalid/vector.test.js +29 -0
- package/test/tests/valid/asset.test.js +51 -0
- package/test/tests/valid/checkbox.test.js +50 -0
- package/test/tests/valid/color.test.js +50 -0
- package/test/tests/valid/curve.test.js +77 -0
- package/test/tests/valid/entity.test.js +50 -0
- package/test/tests/valid/enum.test.js +74 -0
- package/test/tests/valid/export.test.js +56 -0
- package/test/tests/valid/inherit.test.js +103 -0
- package/test/tests/valid/intellisense.test.js +36 -0
- package/test/tests/valid/json.test.js +150 -0
- package/test/tests/valid/misc.test.js +47 -0
- package/test/tests/valid/numeric.test.js +104 -0
- package/test/tests/valid/text.test.js +59 -0
- package/test/tests/valid/vector.test.js +54 -0
- package/test/utils.js +53 -0
|
@@ -0,0 +1,54 @@
|
|
|
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: Vector attribute', function () {
|
|
8
|
+
let data;
|
|
9
|
+
before(async function () {
|
|
10
|
+
data = await parseAttributes('./vector.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
|
+
|
|
19
|
+
it('Example: should exist without errors', function () {
|
|
20
|
+
expect(data[0]?.example).to.exist;
|
|
21
|
+
expect(data[0].example.attributes).to.not.be.empty;
|
|
22
|
+
expect(data[0].example.errors).to.be.empty;
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('a: should be a vector attribute', function () {
|
|
26
|
+
expect(data[0].example.attributes.a).to.exist;
|
|
27
|
+
expect(data[0].example.attributes.a.name).to.equal('a');
|
|
28
|
+
expect(data[0].example.attributes.a.type).to.equal('vec2');
|
|
29
|
+
expect(data[0].example.attributes.a.array).to.equal(false);
|
|
30
|
+
expect(data[0].example.attributes.a.default).to.eql([0, 0]);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('b: should be a vector attribute with a default value', function () {
|
|
34
|
+
expect(data[0].example.attributes.b).to.exist;
|
|
35
|
+
expect(data[0].example.attributes.b.name).to.equal('b');
|
|
36
|
+
expect(data[0].example.attributes.b.type).to.equal('vec3');
|
|
37
|
+
expect(data[0].example.attributes.b.array).to.equal(false);
|
|
38
|
+
expect(data[0].example.attributes.b.default).to.eql([1, 2, 3]);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('c: should be a vector attribute array with a size', function () {
|
|
42
|
+
expect(data[0].example.attributes.c).to.exist;
|
|
43
|
+
expect(data[0].example.attributes.c.name).to.equal('c');
|
|
44
|
+
expect(data[0].example.attributes.c.type).to.equal('vec4');
|
|
45
|
+
expect(data[0].example.attributes.c.array).to.equal(true);
|
|
46
|
+
expect(data[0].example.attributes.c.size).to.equal(2);
|
|
47
|
+
expect(data[0].example.attributes.c.default).equal(null);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('d: should not be recognized as an attribute (extends vector and not is vector)', function () {
|
|
51
|
+
expect(data[0].example.attributes.d).to.not.exist;
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
});
|
package/test/utils.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import fs from 'fs/promises';
|
|
2
|
+
import { fileURLToPath } from 'url';
|
|
3
|
+
import path, { dirname } from 'path';
|
|
4
|
+
import { JSDocParser } from '../src/index.js';
|
|
5
|
+
|
|
6
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
7
|
+
const __dirname = dirname(__filename);
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Reads the contents of a file and returns an array containing the file name and its contents.
|
|
11
|
+
* @param {string} filePath - The path to the file.
|
|
12
|
+
* @returns {Promise<[string, string]>} A promise that resolves to an array containing the file name and its contents.
|
|
13
|
+
*/
|
|
14
|
+
async function getFileData(filePath) {
|
|
15
|
+
try {
|
|
16
|
+
const contents = await fs.readFile(path.join(__dirname, 'fixtures', filePath), 'utf8');
|
|
17
|
+
return [path.join('/', filePath), contents];
|
|
18
|
+
} catch (error) {
|
|
19
|
+
console.error(`Error reading file ${filePath}: ${error}`);
|
|
20
|
+
return [path.basename(filePath), null]; // Return null for contents if there was an error
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Fetches the contents of multiple files.
|
|
26
|
+
* @param {string[]} filePaths - An array of file paths.
|
|
27
|
+
* @returns {Promise<[string, string][]>} A promise that resolves to an array containing the file names and their contents.
|
|
28
|
+
*/
|
|
29
|
+
export function fetchScripts(filePaths) {
|
|
30
|
+
return Promise.all(filePaths.map(getFileData));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @param {string[]} paths - The path to the example file.
|
|
35
|
+
* @returns {Promise<[results: any,
|
|
36
|
+
* errors: import('../src/parsers/script-parser.js').ValidationError[]]>} A promise that resolves to
|
|
37
|
+
* an array of attribute data.
|
|
38
|
+
*/
|
|
39
|
+
export async function parseAttributes(...paths) {
|
|
40
|
+
const parser = new JSDocParser();
|
|
41
|
+
await parser.init();
|
|
42
|
+
const scriptContents = await fetchScripts([...paths, './playcanvas.d.ts']);
|
|
43
|
+
parser.updateProgram(scriptContents);
|
|
44
|
+
return parser.parseAttributes(paths[0]);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export async function getAttributes(...paths) {
|
|
48
|
+
const parser = new JSDocParser();
|
|
49
|
+
await parser.init();
|
|
50
|
+
const scriptContents = await fetchScripts([...paths, './playcanvas.d.ts']);
|
|
51
|
+
parser.updateProgram(scriptContents);
|
|
52
|
+
return parser.getAttributes(paths[0]);
|
|
53
|
+
}
|