Skip to content

Commit

Permalink
Resolve lint violations
Browse files Browse the repository at this point in the history
  • Loading branch information
ryota-ka committed Nov 13, 2023
1 parent 8df5e98 commit 5ad383b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,9 @@ describe(jsx, () => {
test('conditional rendering', () => {
expect(
<div>
{/* eslint-disable-next-line @typescript-eslint/no-unnecessary-condition */}
{true && <span>shown</span>}
{/* eslint-disable-next-line @typescript-eslint/no-unnecessary-condition */}
{false && <span>hidden</span>}
{null}
{undefined}
Expand Down
19 changes: 16 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,34 @@ const canonicalizeVNodeData = (orig: VNodeData): VNodeData => {
const data: VNodeData = {};

for (const key in orig) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const v = orig[key];

if (v === undefined) {
continue;
}

if (key === '$attrs' || key === 'attrs') {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
data.attrs = Object.assign(v, data.attrs ?? {});
} else if (key.startsWith('aria-')) {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
data.attrs ??= {};
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
data.attrs[key] = v;
} else if (key === '$class' || key === 'class') {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
data.class = v;
} else if (key === 'className' || key === 'id') {
// skipping in favor of sel
} else if (key === '$data' || key === 'data' || key === 'dataset') {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
data.dataset = Object.assign(v, data.dataset ?? {});
} else if (key.startsWith('data-')) {
const k = kebab2camel(key.replace('data-', ''));
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
data.dataset ??= {};
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
data.dataset[k] = v;
} else if (key === '$hook' || key === 'hook') {
data.hook = v;

Check warning on line 49 in src/index.ts

View workflow job for this annotation

GitHub Actions / Build

Unsafe assignment of an `any` value
Expand All @@ -55,14 +63,17 @@ const canonicalizeVNodeData = (orig: VNodeData): VNodeData => {
data.on[k] = v;

Check warning on line 63 in src/index.ts

View workflow job for this annotation

GitHub Actions / Build

Unsafe assignment of an `any` value
}
} else if (key === 'list' || key === 'role') {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
data.attrs ??= {};
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
data.attrs[key] = v;
} else if (key === '$style' || key === 'style') {
data.style = v;

Check warning on line 71 in src/index.ts

View workflow job for this annotation

GitHub Actions / Build

Unsafe assignment of an `any` value
} else if (key.startsWith('$')) {
const mod = key.substring(1);
data[mod] = v;

Check warning on line 74 in src/index.ts

View workflow job for this annotation

GitHub Actions / Build

Unsafe assignment of an `any` value
} else {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
data.props ??= {};
data.props[key] = v;

Check warning on line 78 in src/index.ts

View workflow job for this annotation

GitHub Actions / Build

Unsafe assignment of an `any` value
}
Expand Down Expand Up @@ -129,6 +140,7 @@ export const jsx = (
let vnode = tag(data, flatChildren);

// when a primitive value is returned from the function component
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (typeof vnode !== 'object' || vnode === null) {
vnode = flatten([vnode], [])[0]!;
}
Expand All @@ -144,11 +156,11 @@ export const jsx = (

// intrinsic elements
let sel = tag;
const id = data['id'] as string;
const id = data['id'] as string | undefined;
if (id !== undefined) {
sel += `#${id}`;
}
const className = data['className'] as string;
const className = data['className'] as string | undefined;
if (className !== undefined) {
for (const cls of className.trim().split(' ')) {
sel += `.${cls}`;
Expand All @@ -157,6 +169,7 @@ export const jsx = (

const canonicalizedData = canonicalizeVNodeData(data);

// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (flatChildren.length === 1 && flatChildren[0]?.sel === undefined && typeof flatChildren[0]?.text !== undefined) {
return {
children: undefined,
Expand All @@ -173,7 +186,7 @@ export const jsx = (
data: canonicalizedData,
elm: undefined,
sel,
key: (data?.['$key'] as Key) ?? (data?.['key'] as Key),
key: (data?.['$key'] as Key | undefined) ?? (data?.['key'] as Key | undefined),
text: undefined,
};

Expand Down

0 comments on commit 5ad383b

Please sign in to comment.