docs: add line highlight validation (#14380)
* docs: add line highlight validation * add to all projects * fixes * fixes * fix * fixes
This commit is contained in:
@@ -9,24 +9,39 @@ import {
|
||||
JSXTextExpression,
|
||||
LiteralExpression,
|
||||
ObjectExpression,
|
||||
TemplateLiteralExpression,
|
||||
VariableDeclaration,
|
||||
} from "types"
|
||||
|
||||
const ALLOWED_BODY_TYPES = ["ExpressionStatement", "ExportNamedDeclaration"]
|
||||
|
||||
export function estreeToJs(estree: Estree) {
|
||||
// TODO improve on this utility. Currently it's implemented to work
|
||||
// for specific use cases as we don't have a lot of info on other
|
||||
// use cases.
|
||||
if (
|
||||
!estree.body?.length ||
|
||||
estree.body[0].type !== "ExpressionStatement" ||
|
||||
!estree.body[0].expression
|
||||
!estree.body[0].type ||
|
||||
!ALLOWED_BODY_TYPES.includes(estree.body[0].type)
|
||||
) {
|
||||
return
|
||||
}
|
||||
|
||||
return expressionToJs(estree.body[0].expression)
|
||||
switch (estree.body[0].type) {
|
||||
case "ExpressionStatement":
|
||||
if (!estree.body[0].expression) {
|
||||
return
|
||||
}
|
||||
return expressionToJs(estree.body[0].expression)
|
||||
case "ExportNamedDeclaration":
|
||||
if (!estree.body[0].declaration) {
|
||||
return
|
||||
}
|
||||
return declarationToJs(estree.body[0].declaration)
|
||||
}
|
||||
}
|
||||
|
||||
function expressionToJs(
|
||||
export function expressionToJs(
|
||||
expression: Expression
|
||||
): ExpressionJsVar | ExpressionJsVar[] | undefined {
|
||||
switch (expression.type) {
|
||||
@@ -84,5 +99,36 @@ function expressionToJs(
|
||||
},
|
||||
data: text,
|
||||
}
|
||||
case "TemplateLiteral":
|
||||
const templateExpression = expression as TemplateLiteralExpression
|
||||
let value = ""
|
||||
let raw = ""
|
||||
templateExpression.quasis.forEach((quasi) => {
|
||||
value += quasi.value.cooked
|
||||
raw += quasi.value.raw
|
||||
})
|
||||
return {
|
||||
original: {
|
||||
type: "Literal",
|
||||
value,
|
||||
raw,
|
||||
},
|
||||
data: value,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function declarationToJs(declaration: VariableDeclaration): {
|
||||
name: string
|
||||
value: ExpressionJsVar | ExpressionJsVar[] | undefined
|
||||
} {
|
||||
if (!declaration.declarations.length) {
|
||||
throw new Error("No declarations found")
|
||||
}
|
||||
const name = declaration.declarations[0].id.name
|
||||
const value = expressionToJs(declaration.declarations[0].init)
|
||||
return {
|
||||
name,
|
||||
value,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user