New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@hiya620/ts-parser

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@hiya620/ts-parser

> TODO: description

latest
npmnpm
Version
1.0.2
Version published
Maintainers
1
Created
Source

ts-parser

解析typescript代码,

使用

const { AST } = require('ts-parser')
const ast = new AST({filePath: path.join(__dirname, 'demo.ts')})
console.log(ast.config)

ast.config :

  • imports - 解析import声明生成的数据
  • interfaces - 解析interface声明生成的数据
  • enums - 解析enum声明生成的数据
  • types - 解析type声明生成的数据

功能

解析import声明

例如

import { Base, Time } from './base'
import Props from './props'
import Main, { Props as BaseProps } from './type'
import './style.scss'

生成结果:

interface ImportDeclaration {
  from: string
  defaultSpecifier?: string
  specifiers?: Array<{
    imported: string
    local: string
  }>
}

解析enum声明

例如

export enum Enabled {
  DISABLED = 0,
  ENABLED = 1
}

生成结果:

export interface TSEnumDeclaration {
  /** 枚举名 */
  name: string
  /** 枚举成员 */
  members: Array<{
    label: string
    value: string | number | boolean
  }>
}

解析interface声明

例如

export interface Time {
  createTime?: string // 创建时间
  updateTime?: string // 更新时间
}

export interface Category extends Base, Time {
  id?: string // 类目id
  categoryName?: string | number // 类目名称
  categoryCode?: string // 类目名称的短码
}

生成结果:

interface FieldType {
  simpleType?: string // 简单类型:string, number, boolean
  referenceType?: {
    name?: string
    body?: TSInterfaceDeclaration['body']
  } // 引用类型:interface, enum, type等
  literalType?: string // 文本值类型:1 | 2
  unionType?: Array<FieldType> // 联合类型,例如 string|number
}

interface TSInterfaceDeclaration {
  /** 接口名 */
  name: string
  body: {
    [key: string]: {
      key: string
      optional?: boolean // 是否必须
      isArray?: boolean // 是否数组
    } & FieldType
  }
  params?: Array<{
    argName: string
    defaultValue?: string
  }> // 参数,例如 interface Base<T = any> {}
  extends?: Array<string>
}

解析支持的写法:

  • 带继承extends,例如 interface Person extends Base {}
  • 带参数<T = any>,例如 interface Person<T = any> { }
  • 成员类型为固定值,id?: '123456'
  • 成员类型为基础类,id?: string
  • 成员类型为联合类型,id?: number | string
  • 成员类型为interface/enum/type等类型,category?: Category
  • 成员类型为person?: { id: string; name: string }
  • 成员类型为数组,ids?: string[]ids?: Array<string>

解析type声明

例如

export type Size = 'sm' | 'md' | 'lg'
export type NumberType = number
export type SimpleType = number | boolean | string | null | undefined 
export type Info = Base & Time
export type Person = { id: string; name: string }
export type Persons = Person[]

生成结果:

interface TSTypeAliasDeclaration extends FieldType {
  name: string
  isArray?: boolean
}

FAQs

Package last updated on 23 Aug 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts