jsxpression 0.2.0 → 0.2.1
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 +6 -6
- package/src/analyze/analyze.test.ts +50 -46
- package/src/evaluate/evaluate.test.ts +3 -3
- package/src/parse/parse.test.ts +15 -17
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jsxpression",
|
|
3
|
-
"version": "0.2.
|
|
4
|
-
"gitHead": "
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"gitHead": "b5e3be6d241550a3cdc17fe18ff20923d2fba3ce",
|
|
5
5
|
"description": "Validate and safely render JSX expressions",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"author": "Divid AB <info@divid.se>",
|
|
@@ -19,13 +19,13 @@
|
|
|
19
19
|
"exports": {
|
|
20
20
|
".": "./lib/index.js"
|
|
21
21
|
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"test": "vitest run src/**/*.test.ts"
|
|
24
|
+
},
|
|
22
25
|
"dependencies": {
|
|
23
26
|
"acorn": "8.15.0",
|
|
24
27
|
"acorn-typescript": "1.4.13",
|
|
25
28
|
"acorn-walk": "8.3.4",
|
|
26
29
|
"zod": "^4.4.3"
|
|
27
|
-
},
|
|
28
|
-
"scripts": {
|
|
29
|
-
"test": "vitest run src/**/*.test.ts"
|
|
30
30
|
}
|
|
31
|
-
}
|
|
31
|
+
}
|
|
@@ -321,58 +321,62 @@ describe("analyze - consolidated tests", () => {
|
|
|
321
321
|
const requiredParams = methodDef.params.filter((p) => p.required);
|
|
322
322
|
const nonVariadicParams = methodDef.params.filter((p) => !p.variadic);
|
|
323
323
|
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
if (objectName === "Math") {
|
|
329
|
-
testExpression = `<Text>{Math.${methodName}()}</Text>`;
|
|
330
|
-
} else if (objectName === "Array.prototype") {
|
|
331
|
-
testExpression = `<Text>{items.${methodName}()}</Text>`;
|
|
332
|
-
} else if (objectName === "String.prototype") {
|
|
333
|
-
testExpression = `<Text>{text.${methodName}()}</Text>`;
|
|
334
|
-
} else {
|
|
335
|
-
testExpression = `<Text>{${objectName}.${methodName}()}</Text>`;
|
|
336
|
-
}
|
|
324
|
+
it(`should validate ${displayName} minimum parameters`, () => {
|
|
325
|
+
if (requiredParams.length === 0) {
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
337
328
|
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
})
|
|
343
|
-
|
|
329
|
+
let testExpression: string = "";
|
|
330
|
+
|
|
331
|
+
if (objectName === "Math") {
|
|
332
|
+
testExpression = `<Text>{Math.${methodName}()}</Text>`;
|
|
333
|
+
} else if (objectName === "Array.prototype") {
|
|
334
|
+
testExpression = `<Text>{items.${methodName}()}</Text>`;
|
|
335
|
+
} else if (objectName === "String.prototype") {
|
|
336
|
+
testExpression = `<Text>{text.${methodName}()}</Text>`;
|
|
337
|
+
} else {
|
|
338
|
+
testExpression = `<Text>{${objectName}.${methodName}()}</Text>`;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
const ast = parse(testExpression);
|
|
342
|
+
const result = analyze(ast, baseSchema);
|
|
343
|
+
expect(result.hasWarnings).toBe(true);
|
|
344
|
+
expect(result.warnings[0].message).toContain(`${displayName} expects at least ${requiredParams.length} parameter`);
|
|
345
|
+
});
|
|
344
346
|
|
|
345
347
|
// Test maximum parameters for non-variadic methods
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
}
|
|
364
|
-
} else if (objectName === "String.prototype") {
|
|
365
|
-
testExpression = `<Text>{text.${methodName}(${extraParams})}</Text>`;
|
|
348
|
+
it(`should validate ${displayName} maximum parameters`, () => {
|
|
349
|
+
if (!(nonVariadicParams.length === methodDef.params.length && methodDef.params.length > 0)) {
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// Create a call with too many parameters
|
|
354
|
+
const maxParams = methodDef.params.length;
|
|
355
|
+
const extraParams = Array(maxParams + 2)
|
|
356
|
+
.fill("1")
|
|
357
|
+
.join(", ");
|
|
358
|
+
|
|
359
|
+
let testExpression: string = "";
|
|
360
|
+
|
|
361
|
+
if (objectName === "Math") {
|
|
362
|
+
testExpression = `<Text>{Math.${methodName}(${extraParams})}</Text>`;
|
|
363
|
+
} else if (objectName === "Array.prototype") {
|
|
364
|
+
if (methodName === "map" || methodName === "filter") {
|
|
365
|
+
testExpression = `<Text>{items.${methodName}(x => x, {}, ${extraParams})}</Text>`;
|
|
366
366
|
} else {
|
|
367
|
-
testExpression = `<Text>{
|
|
367
|
+
testExpression = `<Text>{items.${methodName}(${extraParams})}</Text>`;
|
|
368
368
|
}
|
|
369
|
+
} else if (objectName === "String.prototype") {
|
|
370
|
+
testExpression = `<Text>{text.${methodName}(${extraParams})}</Text>`;
|
|
371
|
+
} else {
|
|
372
|
+
testExpression = `<Text>{${objectName}.${methodName}(${extraParams})}</Text>`;
|
|
373
|
+
}
|
|
369
374
|
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
}
|
|
375
|
+
const ast = parse(testExpression);
|
|
376
|
+
const result = analyze(ast, baseSchema);
|
|
377
|
+
expect(result.hasWarnings).toBe(true);
|
|
378
|
+
expect(result.warnings[0].message).toContain(`${displayName} expects at most ${maxParams} parameter`);
|
|
379
|
+
});
|
|
376
380
|
});
|
|
377
381
|
});
|
|
378
382
|
});
|
|
@@ -2,7 +2,7 @@ import { describe, it, expect, vi } from "vitest";
|
|
|
2
2
|
import { compile } from "../compile/index.js";
|
|
3
3
|
import { parse } from "../parse/index.js";
|
|
4
4
|
import type { Schema } from "../schema.js";
|
|
5
|
-
import { evaluate, type Component, type EvaluateOptions, type PropsDict } from "./evaluate.js";
|
|
5
|
+
import { evaluate, type Component, type CreateElement, type EvaluateOptions, type Node as EvalNode, type PropsDict } from "./evaluate.js";
|
|
6
6
|
import { EvaluationError } from "./evaluation-error.js";
|
|
7
7
|
|
|
8
8
|
type Node = {
|
|
@@ -76,7 +76,7 @@ describe("evaluate", () => {
|
|
|
76
76
|
expect(() => {
|
|
77
77
|
// props.nested's shape isn't known statically; this test only cares that mutating it throws once frozen.
|
|
78
78
|
(props.nested as Record<string, unknown>).value = 999;
|
|
79
|
-
}).toThrow();
|
|
79
|
+
}).toThrow(TypeError);
|
|
80
80
|
return { type: "Test", props, children: [] };
|
|
81
81
|
};
|
|
82
82
|
|
|
@@ -269,7 +269,7 @@ describe("evaluate", () => {
|
|
|
269
269
|
});
|
|
270
270
|
|
|
271
271
|
it("should pass correct parameters to custom createElement", () => {
|
|
272
|
-
const mockCreateElement = vi.fn();
|
|
272
|
+
const mockCreateElement = vi.fn<CreateElement<EvalNode>>();
|
|
273
273
|
|
|
274
274
|
compileAndEvaluate("<Test x={10}>Hello</Test>", {
|
|
275
275
|
components: { Test: () => ({ type: "Test", props: {}, children: [] }) },
|
package/src/parse/parse.test.ts
CHANGED
|
@@ -2,6 +2,15 @@ import { describe, it, expect } from "vitest";
|
|
|
2
2
|
import { ParseError } from "./parse-error.js";
|
|
3
3
|
import { parse } from "./parse.js";
|
|
4
4
|
|
|
5
|
+
function getThrownError(fn: () => unknown): unknown {
|
|
6
|
+
try {
|
|
7
|
+
fn();
|
|
8
|
+
} catch (error) {
|
|
9
|
+
return error;
|
|
10
|
+
}
|
|
11
|
+
throw new Error("Expected function to throw");
|
|
12
|
+
}
|
|
13
|
+
|
|
5
14
|
describe("parse", () => {
|
|
6
15
|
it("should parse valid JSX element", () => {
|
|
7
16
|
const result = parse("<Text x={10}>Hello</Text>");
|
|
@@ -20,12 +29,6 @@ describe("parse", () => {
|
|
|
20
29
|
|
|
21
30
|
it("should throw ParseError with position for syntax error", () => {
|
|
22
31
|
expect(() => parse("<Text>Unclosed")).toThrow(ParseError);
|
|
23
|
-
|
|
24
|
-
try {
|
|
25
|
-
parse("<Text>Unclosed");
|
|
26
|
-
} catch (error) {
|
|
27
|
-
expect(error).toBeInstanceOf(ParseError);
|
|
28
|
-
}
|
|
29
32
|
});
|
|
30
33
|
|
|
31
34
|
it("should parse nested JSX", () => {
|
|
@@ -78,17 +81,12 @@ describe("parse", () => {
|
|
|
78
81
|
});
|
|
79
82
|
|
|
80
83
|
it("should preserve location information in errors", () => {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
expect(error.loc).toBeDefined();
|
|
88
|
-
expect(error.loc.line).toBe(1);
|
|
89
|
-
expect(error.loc.column).toBe(6);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
84
|
+
const error = getThrownError(() => parse("<Text>Unclosed"));
|
|
85
|
+
|
|
86
|
+
expect(error).toBeInstanceOf(ParseError);
|
|
87
|
+
expect((error as ParseError).loc).toBeDefined();
|
|
88
|
+
expect((error as ParseError).loc?.line).toBe(1);
|
|
89
|
+
expect((error as ParseError).loc?.column).toBe(6);
|
|
92
90
|
});
|
|
93
91
|
|
|
94
92
|
it("should handle invalid JSX syntax", () => {
|