Skip to content

Commit 242af11

Browse files
Localize JSX namespace to Purview (karthikv#40)
1 parent e030ea3 commit 242af11

16 files changed

+1185
-1166
lines changed

src/component.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { nanoid } from "nanoid"
22
import { StateTree, ChildMap, EventHandler } from "./purview"
33
import { PNodeRegular } from "./types/ws"
4+
import { JSX } from "./purview"
45

56
type UpdateFn<S> = (state: Readonly<S>) => Partial<S>
67

src/css.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import { JSX } from "./purview"
12
import { Properties, SimplePseudos } from "csstype"
23
import { expandProperty } from "inline-style-expand-shorthand"
34
import { lexer } from "css-tree"
45
import * as LRU from "lru-cache"
5-
import * as Purview from "./purview"
6+
import Purview, { Component } from "./purview"
67
import { isPseudoClass } from "./pseudo_classes"
78

89
type OptionalProperties = {
@@ -169,7 +170,7 @@ export function styledTag<K extends keyof JSX.IntrinsicElements>(
169170
// Even though this is a string, it must be uppercase for JSX.
170171
Tag: K,
171172
...baseCSSProperties: CSSProperties[]
172-
): new (props: JSX.IntrinsicElements[K]) => Purview.Component<
173+
): new (props: JSX.IntrinsicElements[K]) => Component<
173174
JSX.IntrinsicElements[K],
174175
{}
175176
> {

src/examples/animation.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Purview, { css } from "../purview"
1+
import Purview, { css, JSX } from "../purview"
22

33
interface AnimationState {
44
visible: boolean

src/examples/app.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Purview, { ChangeEvent, css, styledTag } from "../purview"
1+
import Purview, { ChangeEvent, css, JSX, styledTag } from "../purview"
22
import Animation from "./animation"
33

44
interface AppState {

src/examples/counter.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Normally you'd import from "purview". We use "../purview" here since we're in
22
// the Purview codebase.
3-
import Purview from "../purview"
3+
import Purview, { JSX } from "../purview"
44
import { Sequelize, QueryTypes, DataTypes } from "sequelize"
55
import * as http from "http"
66
import * as express from "express"

src/examples/simple.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as http from "http"
2-
import Purview, { InputEvent } from "../purview"
2+
import Purview, { InputEvent, JSX } from "../purview"
33
import * as express from "express"
44

55
// (1) Write components by extending Purview.Component. The two type parameters

src/helpers.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { Attrs } from "snabbdom/modules/attributes"
22
import { PNodeRegular, PNode } from "./types/ws"
3+
import { JSX } from "./purview"
4+
5+
export interface NestedArray<T> extends Array<NestedArray<T> | T> {}
36

47
type EventAttribute = keyof JSX.DOMAttributes
58

src/namespace.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import {
2+
createElem as createElemInternal,
3+
handleWebSocket as handleWebSocketInternal,
4+
render as renderInternal,
5+
renderCSS as renderCSSInternal,
6+
Component as ComponentInternal,
7+
scriptPath as scriptPathInternal,
8+
reloadOptions as reloadOptionsInternal,
9+
JSX as JSXInternal,
10+
} from "./purview"
11+
12+
export namespace Purview {
13+
export let createElem = createElemInternal
14+
export let handleWebSocket = handleWebSocketInternal
15+
export let render = renderInternal
16+
export let renderCSS = renderCSSInternal
17+
export let Component = ComponentInternal
18+
export let scriptPath = scriptPathInternal
19+
export let reloadOptions = reloadOptionsInternal
20+
export import JSX = JSXInternal
21+
}

src/purview.ts

+13-18
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as t from "io-ts"
77

88
import Component, { ComponentConstructor } from "./component"
99
import {
10+
NestedArray,
1011
tryParseJSON,
1112
mapNested,
1213
isEventAttr,
@@ -43,6 +44,7 @@ import {
4344
generateRule,
4445
getAtomicProperties,
4546
} from "./css"
47+
import { JSX } from "./types/jsx"
4648
import { EVENT_HANDLER_GRACE_PERIOD_MS } from "./constants"
4749

4850
export interface RenderOptions {
@@ -173,9 +175,9 @@ const RENDER_CSS_NOT_CALLED_ERROR =
173175
export function createElem(
174176
nodeName: string | ComponentConstructor<any, any>,
175177
attributes:
176-
| (JSX.InputHTMLAttributes<any> &
177-
JSX.TextareaHTMLAttributes<any> &
178-
JSX.OptionHTMLAttributes<any>)
178+
| (JSX.InputHTMLAttributes &
179+
JSX.TextareaHTMLAttributes &
180+
JSX.OptionHTMLAttributes)
179181
| null,
180182
...children: NestedArray<JSX.Child>
181183
): JSX.Element {
@@ -850,12 +852,10 @@ async function makeRegularElem(
850852
}
851853

852854
if (nodeName === "input") {
853-
const type = (attributes as JSX.InputHTMLAttributes<any>)
854-
.type as string
855+
const type = (attributes as JSX.InputHTMLAttributes).type as string
855856
validator = makeValidator(INPUT_TYPE_VALIDATOR[type] || t.string)
856857
} else if (nodeName === "select") {
857-
const multiple = (attributes as JSX.SelectHTMLAttributes<any>)
858-
.multiple
858+
const multiple = (attributes as JSX.SelectHTMLAttributes).multiple
859859
validator = makeValidator(multiple ? t.array(t.string) : t.string)
860860
} else if (nodeName === "textarea") {
861861
validator = makeValidator(t.string)
@@ -1266,16 +1266,11 @@ export const scriptPath = pathLib.resolve(
12661266
"browser.js",
12671267
)
12681268

1269-
// Export all values above on the default object as well.
1270-
export default {
1271-
createElem,
1272-
handleWebSocket,
1273-
render,
1274-
renderCSS,
1275-
Component,
1276-
scriptPath,
1277-
reloadOptions,
1278-
}
1269+
// Export all values above on the default object as well. Do this through
1270+
// a namespace so we can use a locally scoped JSX namespace:
1271+
// https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#locally-scoped-jsx-namespaces
1272+
import { Purview } from "./namespace"
1273+
export default Purview
12791274

12801275
// Export relevant types.
12811276
export {
@@ -1286,4 +1281,4 @@ export {
12861281
PurviewEvent,
12871282
} from "./types/ws"
12881283
export { css, styledTag, CSS } from "./css"
1289-
export * from "./types/jsx"
1284+
export { JSX, NestedArray }

0 commit comments

Comments
 (0)