Skip to content

Commit

Permalink
fix: replace swc with typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
TorstenDittmann committed Aug 20, 2023
1 parent c775193 commit 0e4f7e9
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 29 deletions.
31 changes: 27 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/process/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
"license": "MIT",
"dependencies": {
"@markdoc/markdoc": "^0.3.0",
"@swc/core": "^1.3.76",
"js-yaml": "^4.1.0",
"svelte": "*"
"svelte": "^4.0.0",
"typescript": "^5.0.0"
},
"repository": {
"type": "git",
Expand Down
67 changes: 44 additions & 23 deletions packages/process/src/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,17 @@ import { existsSync, readFileSync, writeFileSync } from 'fs';
import { load as loadYaml } from 'js-yaml';
import { parse as svelteParse, walk } from 'svelte/compiler';
import { dirname, join } from 'path';
import { parseSync as swcParse } from '@swc/core';
import type { BindingIdentifier } from '@swc/core';
import type { Config } from './config';
import * as default_schema from './default_schema';
import {
ScriptTarget,
SyntaxKind,
VariableDeclaration,
createSourceFile,
getJSDocType,
getNameOfDeclaration,
isVariableStatement,
} from 'typescript';

type Var = {
name: string;
Expand Down Expand Up @@ -151,27 +158,37 @@ export function get_component_vars(
* parse the script with swc
*/
const script = match[1];
const result = swcParse(script, {
syntax: 'typescript',
});
const source = createSourceFile(target, script, ScriptTarget.Latest, true);

/**
* find and return all exported variables
*/
return result.body.reduce<Record<string, SchemaAttribute>>((prev, node) => {
if (node.type === 'ExportDeclaration') {
if (node.declaration.type === 'VariableDeclaration') {
node.declaration.declarations.forEach((decl) => {
if (decl.id.type === 'Identifier') {
prev[decl.id.value] = {
type: ts_to_type(decl.id),
};
return source.statements.reduce<Record<string, SchemaAttribute>>(
(prev, node) => {
if (isVariableStatement(node)) {
const is_export_keyword = node.modifiers?.some(
(v) => v.kind === SyntaxKind.ExportKeyword,
);
if (is_export_keyword) {
const declaration = node.declarationList.declarations.find(
(d) => d.name.kind === SyntaxKind.Identifier,
);
const name =
getNameOfDeclaration(declaration)?.getText(source);
if (!declaration || !name) {
return prev;
}
});
const type = ts_to_type(declaration);
prev[name] = {
type,
};
}
}
}
return prev;
}, {});

return prev;
},
{},
);
}

const uc_map: Record<string, string> = {
Expand All @@ -198,19 +215,23 @@ export function sanitize_for_svelte(content: string): string {
* @param node typescript node
* @returns
*/
export function ts_to_type(node: BindingIdentifier): Var['type'] {
if (node?.typeAnnotation?.typeAnnotation.type === 'TsKeywordType') {
switch (node.typeAnnotation.typeAnnotation.kind) {
case 'string':
export function ts_to_type(declaration: VariableDeclaration): Var['type'] {
const kind = declaration.type?.kind
? declaration.type.kind
: getJSDocType(declaration.parent.parent)?.kind;
if (kind) {
switch (kind) {
case SyntaxKind.StringKeyword:
return String;
case 'number':
case SyntaxKind.NumberKeyword:
return Number;
case 'boolean':
case SyntaxKind.BooleanKeyword:
return Boolean;
default:
throw new Error('Can only handly primitive types.');
}
}

return String;
}

Expand Down

0 comments on commit 0e4f7e9

Please sign in to comment.