Skip to content

Commit

Permalink
fix: implement own AST parser to reduce dep (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
louisgv authored Jun 28, 2022
1 parent bbdc4f3 commit c128ce5
Show file tree
Hide file tree
Showing 5 changed files with 492 additions and 223 deletions.
1 change: 0 additions & 1 deletion cli/plasmo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
"node-object-hash": "2.3.10",
"process": "0.11.10",
"semver": "7.3.7",
"ts-ast-to-literal": "1.0.5",
"typescript": "4.7.4"
},
"devDependencies": {
Expand Down
11 changes: 6 additions & 5 deletions cli/plasmo/src/features/extension-devtools/content-script.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { readFile } from "fs/promises"
import astToLiteral from "ts-ast-to-literal"
import {
Node,
ScriptTarget,
Expand All @@ -13,6 +12,8 @@ import {
import type { ManifestContentScript } from "@plasmo/constants"
import { eLog, vLog } from "@plasmo/utils"

import { parseAst } from "./parse-ast"

export const extractContentScriptMetadata = async (path: string) => {
try {
const sourceContent = await readFile(path, "utf8")
Expand Down Expand Up @@ -52,11 +53,9 @@ export const extractContentScriptMetadata = async (path: string) => {

try {
if (valueNode.kind === SyntaxKind.Identifier) {
output[key] = astToLiteral(
variableDeclarationMap[valueNode.getText()]
)
output[key] = parseAst(variableDeclarationMap[valueNode.getText()])
} else {
output[key] = astToLiteral(valueNode)
output[key] = parseAst(valueNode)
}
} catch (error) {
eLog(error)
Expand All @@ -65,6 +64,8 @@ export const extractContentScriptMetadata = async (path: string) => {
return output
}, {} as ManifestContentScript)

vLog("Parsed config:", config)

return {
config
}
Expand Down
50 changes: 50 additions & 0 deletions cli/plasmo/src/features/extension-devtools/parse-ast.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Copyright (c) Plasmo Corp, [email protected], MIT Licensed
* ---
* Adapted from https://github.com/dword-design/ts-ast-to-literal/blob/master/src/index.js
* Copyright (c) Sebastian Landwehr [email protected], MIT licensed
*/
import type {
ArrayLiteralExpression,
Identifier,
LiteralExpression,
Node,
ObjectLiteralExpression,
PropertyAssignment
} from "typescript"
import { SyntaxKind } from "typescript"

export const parseAst = (node: Node) => {
switch (node.kind) {
case SyntaxKind.StringLiteral:
return (node as LiteralExpression).text
case SyntaxKind.TrueKeyword:
return true
case SyntaxKind.FalseKeyword:
return false
case SyntaxKind.NullKeyword:
return null
case SyntaxKind.NumericLiteral:
return parseFloat((node as LiteralExpression).text)
case SyntaxKind.ArrayLiteralExpression:
return (node as ArrayLiteralExpression).elements
.filter((node) => node.kind !== SyntaxKind.SpreadElement)
.map(parseAst)
case SyntaxKind.ObjectLiteralExpression:
return (node as ObjectLiteralExpression).properties
.filter(
(property) =>
property.kind === SyntaxKind.PropertyAssignment &&
(property.name.kind === SyntaxKind.Identifier ||
property.name.kind === SyntaxKind.StringLiteral)
)
.map((property: PropertyAssignment) => [
(property.name as Identifier).escapedText ||
(property.name as LiteralExpression).text,
parseAst(property.initializer)
])
.reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {})
default:
return undefined
}
}
2 changes: 1 addition & 1 deletion examples
Loading

0 comments on commit c128ce5

Please sign in to comment.