-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create unified types for nodes (#2201)
Create unified types for nodes There is no standardised type for node variables, resulting in several undesirable issues: * `as unknown as` typecasts throughout the codebase * unnecessary checks on node attributes to satisfy the linter * incompatible types, treated as interchangeable in conversions Let's add common MbNode, TextElement and NodeOrText types that enforce attributes for nodes throughout the codebase. Doing so avoids the abovementioned issues, and formalises our view of the relationship between cheerio.Element, DomElement, and all the other attributes expected from node variables.
- Loading branch information
Showing
5 changed files
with
90 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,22 @@ | ||
import cheerio from 'cheerio'; | ||
import { DomElement } from 'htmlparser2'; | ||
import pick from 'lodash/pick'; | ||
import { MbNode, NodeOrText } from '../utils/node'; | ||
|
||
const _ = { pick }; | ||
|
||
export function createErrorNode(element: DomElement, error: any) { | ||
export function createErrorNode(element: NodeOrText, error: any) { | ||
const errorElement = cheerio.parseHTML( | ||
`<div style="color: red">${error.message}</div>`, undefined, true, | ||
)[0]; | ||
return Object.assign(element, _.pick(errorElement, ['name', 'attribs', 'children'])); | ||
return Object.assign(element, _.pick(errorElement, ['name', 'attribs', 'children'])) as MbNode; | ||
} | ||
|
||
export function createEmptyNode() { | ||
return cheerio.parseHTML('<div></div>', undefined, true)[0]; | ||
} | ||
|
||
export function createSlotTemplateNode(slotName: string, content: string): DomElement[] { | ||
export function createSlotTemplateNode(slotName: string, content: string): MbNode[] { | ||
return cheerio.parseHTML( | ||
`<template #${slotName}>${content}</template>`, undefined, true, | ||
) as unknown as DomElement[]; | ||
) as unknown as MbNode[]; | ||
} |
Oops, something went wrong.