Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .npmrc
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
link-workspace-packages = false
2 changes: 1 addition & 1 deletion packages/inula-router/babel.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the Mulan PSL v2 for more details.
*/

module.exports = {
module.exports = {
presets: ['@babel/preset-typescript', ['@babel/preset-env', { targets: { node: 'current' } }]],
plugins: [
'@babel/plugin-syntax-jsx',
Expand Down
4 changes: 2 additions & 2 deletions packages/inula-router/src/router/Link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the Mulan PSL v2 for more details.
*/

import Inula from 'openinula';
import Inula, { InulaElement } from 'openinula';
import { useContext, MouseEvent, ComponentType, Ref } from 'openinula';
import RouterContext from './context';
import { Location } from './index';
Expand All @@ -36,7 +36,7 @@ const checkTarget = (target?: any) => {
return !target || target === '_self';
};

function Link<P extends LinkProps>(props: P) {
function Link<P extends LinkProps>(props: P): InulaElement {
const { to, replace, component, onClick, target, ...other } = props;

const tag = props.tag || 'a';
Expand Down
15 changes: 9 additions & 6 deletions packages/inula/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
"homepage": "",
"bugs": "",
"license": "MulanPSL2",
"main": "index.js",
"main": "./build/inula/index.js",
"repository": {},
"engines": {
"node": ">=0.10.0"
},
"publishConfig": {
"directory": "./build/inula"
},
"scripts": {
"build": "npm run build-types && rollup --config ./scripts/rollup/rollup.config.js",
"build-types": "tsc -p tsconfig.build.json || echo \"WARNING: TSC exited with status $?\" && rollup -c ./scripts/rollup/build-types.js",
Expand All @@ -23,14 +26,14 @@
"test": "jest --config=jest.config.js",
"watch-test": "yarn test --watch --dev"
},
"types": "@types/index.d.ts",
"types": "./build/inula/@types/index.d.ts",
"exports": {
".": {
"types": "./@types/index.d.ts",
"default": "./index.js"
"types": "./build/inula/@types/index.d.ts",
"default": "./build/inula/index.js"
},
"./package.json": "./package.json",
"./jsx-runtime": "./jsx-runtime.js",
"./jsx-dev-runtime": "./jsx-dev-runtime.js"
"./jsx-runtime": "./build/inula/jsx-runtime.js",
"./jsx-dev-runtime": "./build/inula/jsx-dev-runtime.js"
}
}
18 changes: 16 additions & 2 deletions packages/inula/src/external/JSXElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
ClassicComponentClass,
ComponentClass,
ComponentState,
FunctionComponent,
FunctionComponentElement,
InulaCElement,
InulaElement,
Expand Down Expand Up @@ -156,17 +157,30 @@ export function createElement<P extends DOMAttributes<T>, T extends Element>(
...children: InulaNode[]
): DOMElement<P, T>;

export function createElement<P extends KVObject>(
type: FunctionComponent<P>,
props?: (Attributes & P) | null,
...children: InulaNode[]
): FunctionComponentElement<P>;

export function createElement<P extends KVObject>(
type: ClassType<P, ClassicComponent<P, ComponentState>, ClassicComponentClass<P>>,
props?: (ClassAttributes<ClassicComponent<P, ComponentState>> & P) | null,
...children: InulaNode[]
): InulaCElement<P, ClassicComponent<P, ComponentState>>;

export function createElement<P extends KVObject, T extends Component<P, ComponentState>, C extends ComponentClass<P>>(
type: ClassType<P, T, C>,
props?: (ClassAttributes<T> & P) | null,
...children: InulaNode[]
): InulaCElement<P, T>;

export function createElement<P extends KVObject>(
type: FunctionComponent<P> | ComponentClass<P> | string,
props?: (Attributes & P) | null,
...children: InulaNode[]
): InulaElement<P>;

// 创建Element结构体,供JSX编译时调用
export function createElement(type, setting, ...children) {
return buildElement(false, type, setting, children);
Expand Down Expand Up @@ -213,8 +227,8 @@ export function cloneElement(element, setting, ...children) {
}

// 检测结构体是否为合法的Element
export function isValidElement<P>(element: KVObject | null | undefined): element is InulaElement<P> {
return !!(element && element.vtype === TYPE_COMMON_ELEMENT);
export function isValidElement<P>(element: {} | null | undefined): element is InulaElement<P> {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Avoid using {} as a type

The {} type actually means "any non-nullish value" rather than "any object" as is often assumed. This can lead to subtle type checking issues.

Consider replacing {} with one of the following alternatives:

  • Use object if you want to accept any object
  • Use Record<string, any> if you want an object with string keys and any values
  • Use unknown if you want to accept any value and perform type checking
-export function isValidElement<P>(element: {} | null | undefined): element is InulaElement<P> {
+export function isValidElement<P>(element: object | null | undefined): element is InulaElement<P> {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export function isValidElement<P>(element: {} | null | undefined): element is InulaElement<P> {
export function isValidElement<P>(element: object | null | undefined): element is InulaElement<P> {
🧰 Tools
🪛 ESLint

[error] 230-230: Don't use {} as a type. {} actually means "any non-nullish value".

  • If you want a type meaning "any object", you probably want object instead.
  • If you want a type meaning "any value", you probably want unknown instead.
  • If you want a type meaning "empty object", you probably want Record<string, never> instead.
  • If you really want a type meaning "any non-nullish value", you probably want NonNullable<unknown> instead.

(@typescript-eslint/ban-types)

return typeof element === 'object' && element !== null && 'vtype' in element && element.vtype === TYPE_COMMON_ELEMENT;
}

// 兼容高版本的babel编译方式
Expand Down
2 changes: 1 addition & 1 deletion packages/inula/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export interface ExoticComponent<P = KVObject> {
(props: P): InulaElement | null;
}

interface ProviderProps<T> {
export interface ProviderProps<T> {
value: T;
children?: InulaNode | undefined;
}
Expand Down
Loading