Skip to content

Commit ea8ec3b

Browse files
authored
Merge pull request #195 from x0k/form-lint
Form package lint
2 parents 256128e + e098c6e commit ea8ec3b

File tree

111 files changed

+921
-986
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+921
-986
lines changed

mkfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ f/:
7777
pnpm run check
7878
t:
7979
pnpm run test $@
80+
l:
81+
pnpm run lint
82+
f:
83+
pnpm run format
8084
tui:
8185
pnpm run test:ui
8286
popd

packages/form/.prettierignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Package Managers
2+
package-lock.json
3+
pnpm-lock.yaml
4+
yarn.lock
5+
bun.lock
6+
bun.lockb
7+
.svelte-kit
8+
dist
9+
10+
# Miscellaneous
11+
/static/

packages/form/.prettierrc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"useTabs": false,
3+
"singleQuote": false,
4+
"trailingComma": "es5",
5+
"printWidth": 80,
6+
"plugins": ["prettier-plugin-svelte"],
7+
"overrides": [
8+
{
9+
"files": "*.svelte",
10+
"options": {
11+
"parser": "svelte"
12+
}
13+
}
14+
]
15+
}

packages/form/README.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,41 +19,41 @@ npm install @sjsf/form @sjsf/basic-theme @sjsf/ajv8-validator ajv@8
1919

2020
```svelte
2121
<script lang="ts">
22-
import { createForm, BasicForm, type Schema } from '@sjsf/form';
23-
import { resolver } from '@sjsf/form/resolvers/basic';
24-
import { translation } from '@sjsf/form/translations/en';
25-
import { theme } from '@sjsf/basic-theme';
22+
import { createForm, BasicForm, type Schema } from "@sjsf/form";
23+
import { resolver } from "@sjsf/form/resolvers/basic";
24+
import { translation } from "@sjsf/form/translations/en";
25+
import { theme } from "@sjsf/basic-theme";
2626
import { createFormValidator } from "@sjsf/ajv8-validator";
2727
2828
const validator = createFormValidator();
2929
3030
const schema: Schema = {
31-
title: 'Tasks',
32-
type: 'array',
31+
title: "Tasks",
32+
type: "array",
3333
items: {
34-
type: 'object',
34+
type: "object",
3535
properties: {
3636
name: {
37-
type: 'string',
38-
title: 'Name',
37+
type: "string",
38+
title: "Name",
3939
},
4040
description: {
41-
type: 'string',
42-
title: 'Description',
41+
type: "string",
42+
title: "Description",
4343
},
4444
},
45-
required: ["name"]
45+
required: ["name"],
4646
},
47-
}
47+
};
4848
4949
const form = createForm({
5050
theme,
5151
schema,
52-
resolver
52+
resolver,
5353
validator,
5454
translation,
55-
onSubmit: console.log
56-
})
55+
onSubmit: console.log,
56+
});
5757
</script>
5858
5959
<BasicForm {form} />

packages/form/eslint.config.mjs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// @ts-check
2+
import { fileURLToPath } from "node:url";
3+
import { defineConfig } from "eslint/config";
4+
import { includeIgnoreFile } from "@eslint/compat";
5+
import js from "@eslint/js";
6+
import ts from "typescript-eslint";
7+
import svelte from "eslint-plugin-svelte";
8+
import prettier from "eslint-config-prettier";
9+
import globals from "globals";
10+
11+
import svelteConfig from "./svelte.config.js";
12+
13+
const gitignorePath = fileURLToPath(
14+
new URL("../../.gitignore", import.meta.url)
15+
);
16+
17+
export default defineConfig(
18+
includeIgnoreFile(gitignorePath),
19+
js.configs.recommended,
20+
ts.configs.recommendedTypeChecked,
21+
...svelte.configs.recommended,
22+
prettier,
23+
...svelte.configs.prettier,
24+
{
25+
languageOptions: {
26+
globals: { ...globals.browser, ...globals.node },
27+
},
28+
rules: { "no-undef": "off" },
29+
},
30+
{
31+
languageOptions: {
32+
parserOptions: {
33+
tsconfigRootDir: import.meta.dirname,
34+
projectService: {
35+
allowDefaultProject: ["*.ts", "*.js", "*.mjs"],
36+
},
37+
extraFileExtensions: [".svelte"],
38+
parser: ts.parser,
39+
svelteConfig,
40+
},
41+
},
42+
},
43+
{
44+
rules: {
45+
"require-yield": "off",
46+
"@typescript-eslint/no-empty-object-type": "off",
47+
"@typescript-eslint/no-explicit-any": "off",
48+
"svelte/prefer-svelte-reactivity": "off",
49+
"@typescript-eslint/no-deprecated": "warn",
50+
"@typescript-eslint/no-unused-expressions": "warn",
51+
"@typescript-eslint/no-unused-vars": [
52+
"error",
53+
{
54+
args: "all",
55+
argsIgnorePattern: "^_",
56+
caughtErrors: "all",
57+
caughtErrorsIgnorePattern: "^_",
58+
destructuredArrayIgnorePattern: "^_",
59+
varsIgnorePattern: "^_",
60+
ignoreRestSiblings: true,
61+
},
62+
],
63+
},
64+
}
65+
);

packages/form/package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@
3131
"test": "vitest run --typecheck --exclude '.svelte-kit/**'",
3232
"bench": "vitest bench --exclude '.svelte-kit/**'",
3333
"build": "svelte-package && publint",
34-
"check": "svelte-check --tsconfig ./tsconfig.json"
34+
"check": "svelte-check --tsconfig ./tsconfig.json",
35+
"format": "prettier --write .",
36+
"lint": "prettier --check . && eslint ."
3537
},
3638
"dependencies": {
39+
"@standard-schema/spec": "^1.0.0",
3740
"@types/json-schema": "^7.0.15",
3841
"@types/json-schema-merge-allof": "^0.6.5",
39-
"@standard-schema/spec": "^1.0.0",
4042
"json-schema-merge-allof": "^0.8.1",
4143
"jsonpointer": "^5.0.1"
4244
},
@@ -49,6 +51,7 @@
4951
"ajv": "catalog:",
5052
"allof-merge": "^0.6.7",
5153
"deep-freeze-es6": "^4.0.1",
54+
"globals": "catalog:",
5255
"json-schema-compare": "^0.2.2",
5356
"svelte": "catalog:"
5457
},
@@ -145,4 +148,4 @@
145148
"default": "./dist/static-analysis.js"
146149
}
147150
}
148-
}
151+
}

packages/form/src/core/constant-schema.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ export function isSchemaOfConstantValue(schema: Schema): boolean {
1212
}
1313

1414
export function getSchemaConstantValue(schema: Schema) {
15-
const enumValues = schema.enum
15+
const enumValues = schema.enum;
1616
if (Array.isArray(enumValues) && enumValues.length === 1) {
1717
return enumValues[0]!;
1818
}
19-
const constant = schema.const
19+
const constant = schema.const;
2020
if (constant !== undefined) {
21-
return constant
21+
return constant;
2222
}
23-
throw new Error('schema cannot be inferred as a constant');
23+
throw new Error("schema cannot be inferred as a constant");
2424
}

packages/form/src/core/deep-equal.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export function isSchemaValueDeepEqual(
3838
}
3939
return Object.keys(b).length === aKeys.length;
4040
}
41-
return a !== a && b !== b
41+
return a !== a && b !== b;
4242
}
4343

4444
export const isSchemaDeepEqual = isSchemaValueDeepEqual as (

packages/form/src/core/default-state.test.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
2+
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
13
// This file was copied and modified from https://github.com/rjsf-team/react-jsonschema-form/blob/b12175679079be956570349771dddd65406b3773/packages/utils/test/schema/getDefaultFormStateTest.ts
24
// Licensed under the Apache License, Version 2.0.
35
// Modifications made by Roman Krasilnikov.
@@ -1089,7 +1091,8 @@ describe("getDefaultFormState2()", () => {
10891091
emptyObjectFields: "populateAllDefaults",
10901092
allOf: "skipDefaults",
10911093
arrayMinItems: {
1092-
populate: "populate" as any,
1094+
// CHANGED: I don't know why this line is here.
1095+
// populate: "all" as any,
10931096
mergeExtraDefaults: false,
10941097
},
10951098
mergeDefaultsIntoFormData: "useFormDataIfPresent",
@@ -6343,8 +6346,8 @@ describe("getDefaultFormState2()", () => {
63436346
expect(Array.isArray(result.config.items)).toBe(true);
63446347

63456348
// Verify objects are independent instances - modifying one shouldn't affect the other
6346-
(result.config.items[0] as any).field = "test-value-1";
6347-
expect((result.config.items[1] as any).field).toBeUndefined();
6349+
result.config.items[0].field = "test-value-1";
6350+
expect(result.config.items[1].field).toBeUndefined();
63486351
expect(result.config.items[0]).not.toBe(result.config.items[1]);
63496352
});
63506353

@@ -6361,8 +6364,8 @@ describe("getDefaultFormState2()", () => {
63616364
expect(Array.isArray(result.config.items)).toBe(true);
63626365

63636366
// Verify objects are independent instances - modifying one shouldn't affect the other
6364-
(result.config.items[0] as any).field = "test-value-1";
6365-
expect((result.config.items[1] as any).field).toBeUndefined();
6367+
result.config.items[0].field = "test-value-1";
6368+
expect(result.config.items[1].field).toBeUndefined();
63666369
expect(result.config.items[0]).not.toBe(result.config.items[1]);
63676370
});
63686371

@@ -6398,9 +6401,9 @@ describe("getDefaultFormState2()", () => {
63986401
expect(Array.isArray(result)).toBe(true);
63996402

64006403
// Verify objects are independent instances
6401-
(result[0] as any).field = "test-value-1";
6402-
(result[1] as any).field = "test-value-2";
6403-
expect((result[2] as any).field).toBeUndefined();
6404+
result[0].field = "test-value-1";
6405+
result[1].field = "test-value-2";
6406+
expect(result[2].field).toBeUndefined();
64046407
expect(result[0]).not.toBe(result[1]);
64056408
expect(result[1]).not.toBe(result[2]);
64066409
expect(result[0]).not.toBe(result[2]);
@@ -6442,8 +6445,8 @@ describe("getDefaultFormState2()", () => {
64426445
expect(Array.isArray(result)).toBe(true);
64436446

64446447
// Verify objects are independent instances - modifying one shouldn't affect the other
6445-
(result[0] as any).field = "modified-value";
6446-
expect((result[1] as any).field).toBe("default-value");
6448+
result[0].field = "modified-value";
6449+
expect(result[1].field).toBe("default-value");
64476450
expect(result[0]).not.toBe(result[1]);
64486451
});
64496452

@@ -6488,10 +6491,10 @@ describe("getDefaultFormState2()", () => {
64886491
expect(Array.isArray(result)).toBe(true);
64896492

64906493
// Verify nested objects are independent instances
6491-
(result[0] as any).nested.value = "modified-nested-value";
6492-
expect((result[1] as any).nested.value).toBe("nested-default");
6494+
result[0].nested.value = "modified-nested-value";
6495+
expect(result[1].nested.value).toBe("nested-default");
64936496
expect(result[0]).not.toBe(result[1]);
6494-
expect((result[0] as any).nested).not.toBe((result[1] as any).nested);
6497+
expect(result[0].nested).not.toBe(result[1].nested);
64956498
});
64966499
});
64976500
});

packages/form/src/core/default-state.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Modifications made by Roman Krasilnikov.
44

55
import { isRecordEmpty, isObject } from "@/lib/object.js";
6-
import { isSchemaObject } from '@/lib/json-schema/index.js'
6+
import { isSchemaObject } from "@/lib/json-schema/index.js";
77

88
import { resolveDependencies, retrieveSchema } from "./resolve.js";
99
import {
@@ -51,9 +51,11 @@ export function getDefaultValueForType(type: SchemaType) {
5151
return "";
5252
case "null":
5353
return null;
54-
default:
54+
default: {
5555
const n: never = type;
56+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
5657
throw new Error(`Unsupported schema type: ${n}`);
58+
}
5759
}
5860
}
5961

0 commit comments

Comments
 (0)