-
-
Notifications
You must be signed in to change notification settings - Fork 383
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: implement own AST parser to reduce dep (#55)
- Loading branch information
Showing
5 changed files
with
492 additions
and
223 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 |
---|---|---|
@@ -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 | ||
} | ||
} |
Submodule examples
updated
3 files
+1 −1 | with-ant-design/package.json | |
+1 −1 | with-supabase/package.json | |
+2 −2 | with-wagmi/package.json |
Oops, something went wrong.