From 12be729eade0a9a4ed43a548a0f9d3b9064fdfa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sampo=20Kivist=C3=B6?= Date: Thu, 13 Oct 2022 12:14:41 +0300 Subject: [PATCH] typescript: Allow setting ref to null and ran prettier on source files --- packages/inferno-compat/src/index.ts | 2 +- packages/inferno-create-class/src/index.ts | 2 +- packages/inferno-extras/src/findDOMNode.ts | 4 +-- packages/inferno/__tests__/types.spec.tsx | 34 ++++++++++++++++------ packages/inferno/src/core/component.ts | 2 +- packages/inferno/src/core/types.ts | 12 ++++---- scripts/rollup/plugins/index.js | 24 +++++++-------- 7 files changed, 49 insertions(+), 31 deletions(-) diff --git a/packages/inferno-compat/src/index.ts b/packages/inferno-compat/src/index.ts index 09a6f436b..85aea5128 100644 --- a/packages/inferno-compat/src/index.ts +++ b/packages/inferno-compat/src/index.ts @@ -294,7 +294,7 @@ abstract class PureComponent extends Component { } interface ContextProps { - children?: Inferno.InfernoChild | undefined + children?: Inferno.InfernoChild | undefined; context: any; } diff --git a/packages/inferno-create-class/src/index.ts b/packages/inferno-create-class/src/index.ts index b8baef929..385581370 100644 --- a/packages/inferno-create-class/src/index.ts +++ b/packages/inferno-create-class/src/index.ts @@ -1,4 +1,4 @@ -import {Component, Inferno} from 'inferno'; +import { Component, Inferno } from 'inferno'; import { isFunction, throwError } from 'inferno-shared'; export interface Mixin extends Component { diff --git a/packages/inferno-extras/src/findDOMNode.ts b/packages/inferno-extras/src/findDOMNode.ts index 70249aa54..968db50a1 100644 --- a/packages/inferno-extras/src/findDOMNode.ts +++ b/packages/inferno-extras/src/findDOMNode.ts @@ -1,4 +1,4 @@ -import {Component, VNode, findDOMFromVNode} from 'inferno'; +import { Component, VNode, findDOMFromVNode } from 'inferno'; export function findDOMNode(ref: VNode | Component | Node): Node | null { if (ref && (ref as Node).nodeType) { @@ -14,7 +14,7 @@ export function findDOMNode(ref: VNode | Component | Node): Node | null { } if ((ref as VNode).flags) { - return findDOMFromVNode((ref as VNode), true); + return findDOMFromVNode(ref as VNode, true); } return null; diff --git a/packages/inferno/__tests__/types.spec.tsx b/packages/inferno/__tests__/types.spec.tsx index b67b9680e..ca67ddf1d 100644 --- a/packages/inferno/__tests__/types.spec.tsx +++ b/packages/inferno/__tests__/types.spec.tsx @@ -1,5 +1,5 @@ -import {Component, createRef, Inferno, linkEvent, render} from 'inferno'; -import {emptyFn} from "inferno-shared"; +import { Component, createRef, Inferno, linkEvent, Ref, render } from 'inferno'; +import { emptyFn } from 'inferno-shared'; describe('top level context', () => { let container; @@ -167,9 +167,12 @@ describe('top level context', () => { describe('ChildFlags', function () { it('Should allow special flags on all elements', function () { const refObj = createRef(); - const text = "foobar"; - const row =
{text}
; - + const text = 'foobar'; + const row = ( +
+ {text} +
+ ); render(
@@ -181,9 +184,12 @@ describe('top level context', () => { it('Should allow special flags on SVG', function () { const refObj = createRef(); - const text = "foobar"; - const row =
{text}
; - + const text = 'foobar'; + const row = ( +
+ {text} +
+ ); render( @@ -200,5 +206,15 @@ describe('top level context', () => { expect(row).not.toBeNull(); expect(aria).not.toBeNull(); }); - }) + + it('Should allow null for ref attribute', function () { + const obj: { + refWrap: Ref | null; + } = { + refWrap: null + }; + + render(
, container); + }); + }); }); diff --git a/packages/inferno/src/core/component.ts b/packages/inferno/src/core/component.ts index f8c39a2dd..c1f297a47 100644 --- a/packages/inferno/src/core/component.ts +++ b/packages/inferno/src/core/component.ts @@ -145,7 +145,7 @@ export abstract class Component

implements IComponent { public $F: boolean = false; // Force update flag constructor(props?: P, context?: any) { - this.props = (props || (EMPTY_OBJ)) as Readonly<{ children?: Inferno.InfernoNode | undefined }> & Readonly

; + this.props = (props || EMPTY_OBJ) as Readonly<{ children?: Inferno.InfernoNode | undefined }> & Readonly

; this.context = context || EMPTY_OBJ; // context should not be mutable } diff --git a/packages/inferno/src/core/types.ts b/packages/inferno/src/core/types.ts index d1dac8014..9f7555f49 100644 --- a/packages/inferno/src/core/types.ts +++ b/packages/inferno/src/core/types.ts @@ -11,9 +11,11 @@ export interface LinkedEvent { export interface IComponent { // Public state: S | null; - props: Readonly<{ - children?: Inferno.InfernoNode; - } & P>; + props: Readonly< + { + children?: Inferno.InfernoNode; + } & P + >; context?: any; displayName?: string; refs?: any; @@ -171,7 +173,7 @@ export declare namespace Inferno { $ChildFlag?: number; } interface ClassAttributes extends Attributes { - ref?: Ref | RefObject | undefined; + ref?: Ref | RefObject | null | undefined; } interface InfernoElement

{ @@ -1881,7 +1883,7 @@ declare global { type LibraryManagedAttributes = InfernoManagedAttributes; - interface IntrinsicAttributes extends Inferno.Attributes { } + interface IntrinsicAttributes extends Inferno.Attributes {} interface IntrinsicAttributes extends Inferno.Attributes, Refs {} interface IntrinsicClassAttributes extends Inferno.ClassAttributes {} diff --git a/scripts/rollup/plugins/index.js b/scripts/rollup/plugins/index.js index d0abd939d..c03107d4c 100644 --- a/scripts/rollup/plugins/index.js +++ b/scripts/rollup/plugins/index.js @@ -3,7 +3,7 @@ import nodeResolve from 'rollup-plugin-node-resolve'; import replacePlugin from 'rollup-plugin-replace'; import { terser } from 'rollup-plugin-terser'; import { aliasPlugin } from './alias.js'; -import babel from "@rollup/plugin-babel"; +import babel from '@rollup/plugin-babel'; export function createPlugins(version, options) { const plugins = [ @@ -18,17 +18,17 @@ export function createPlugins(version, options) { }) ]; - plugins.push(babel({ - exclude: 'node_modules/**', - sourceMaps: false, - babelrc: false, - presets: !options.esnext ? [['@babel/env', {loose: true, modules: false}]] : null, - babelHelpers: 'runtime', - skipPreflightCheck: true, - plugins: [ - ["@babel/plugin-proposal-class-properties", {"loose": true}] - ] - })); + plugins.push( + babel({ + exclude: 'node_modules/**', + sourceMaps: false, + babelrc: false, + presets: !options.esnext ? [['@babel/env', { loose: true, modules: false }]] : null, + babelHelpers: 'runtime', + skipPreflightCheck: true, + plugins: [['@babel/plugin-proposal-class-properties', { loose: true }]] + }) + ); const replaceValues = { 'process.env.INFERNO_VERSION': JSON.stringify(options.version)