Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复ts中使用gogocode一系列的类型报错问题 #115

Merged
merged 2 commits into from
Dec 24, 2021

Conversation

ATQQ
Copy link
Contributor

@ATQQ ATQQ commented Dec 15, 2021

当前的类型定义,对TS中使用不是很友好,此次 PR 主要解决下述出现的问题

在ts中使用GoGoCode时类型检测报错

使用方法如下

import $ from 'gogocode'
import type { GoGoOption } from 'gogocode'

export function AST(code: string, ops?: GoGoOption) {
  return $(code, ops)
}

目前会有下面的类型报错问题

Problem1

import $ from 'gogocode'

export function AST(code: string) {
  return $(code)
}

ts报错信息

导出函数的返回类型具有或正在使用外部模块“"gogocode"”中的名称“GoGoCode.GoGoAST”,但不能为其命名。ts(4058)

image

Problem2

当前实现无法在ts中使用GoGoCode中其它类型

import $ from 'gogocode'
import type { GoGoOption } from 'gogocode'

错误提示信息

模块“"gogocode"”没有导出的成员“GoGoOption”。ts(2305)

image

@ATQQ
Copy link
Contributor Author

ATQQ commented Dec 16, 2021

TS中使用问题还挺多,晚点再 补充一下PR

const ast = AST(readFile(v))
const ImportDeclaration = ast.find({ type: 'ImportDeclaration' })
ImportDeclaration.each(node => {
  const value = node.attr('source.value') 
  if (value.includes(old)) {
    // 替换新的名称
    node.attr('source.value', value.replace(old, newValue))
    // 前面插入注释节点
    node.before({
      type: 'CommentLine',
      value: 'text',
    })
  }
})

image

@ATQQ ATQQ changed the title fix: 修复ts中使用export 导出返回gogocode实例的类型报错问题(ts-4058) fix: 修复ts中使用gogocode一系列的类型报错问题 Dec 16, 2021
@ATQQ
Copy link
Contributor Author

ATQQ commented Dec 22, 2021

@yexi-xf 大佬 Review 一波

@ediaos
Copy link

ediaos commented Dec 22, 2021

+1 ,使用 TS 无法使用类型 😳

Copy link
Contributor

@bigfengyu bigfengyu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi,这个看起来只是改了一下导出的变量名,是如何解决导出的类型问题的呢?

@ATQQ
Copy link
Contributor Author

ATQQ commented Dec 23, 2021

hi,这个看起来只是改了一下导出的变量名,是如何解决导出的类型问题的呢?

导出名称与上面命名空间一致即可namespace GoGoCode

Copy link
Contributor Author

@ATQQ ATQQ left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

导出名称与上面命名空间一致即可

namespace GoGoCode

export  GoGoCode

这样TS就能正常工作,引入类型的时候就从 namespace取类型

@bigfengyu
Copy link
Contributor

导出名称与上面命名空间一致即可

namespace GoGoCode

export  GoGoCode

这样TS就能正常工作,引入类型的时候就从 namespace取类型

学习了,感谢指教!

@bigfengyu bigfengyu merged commit c38b701 into thx:main Dec 24, 2021
@ediaos
Copy link

ediaos commented Jan 10, 2022

ASTNode 貌似没有导出来

@ATQQ
Copy link
Contributor Author

ATQQ commented Jan 11, 2022

ASTNode 貌似没有导出来

可以这样

import $, { GoGoOption, GoGoAST } from 'gogocode'

type ASTNode = GoGoAST['node']

@ediaos
Copy link

ediaos commented Jan 11, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants