版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

пре 1 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # ast-walker-scope [![npm](https://img.shields.io/npm/v/ast-walker-scope.svg)](https://npmjs.com/package/ast-walker-scope)
  2. [![Unit Test](https://github.com/sxzz/ast-walker-scope/actions/workflows/unit-test.yml/badge.svg)](https://github.com/sxzz/ast-walker-scope/actions/workflows/unit-test.yml)
  3. Traverse Babel AST with scope information.
  4. Inherited from [estree-walker](https://github.com/Rich-Harris/estree-walker).
  5. ## Install
  6. ```bash
  7. npm i ast-walker-scope
  8. ```
  9. ## Usage
  10. ### Basic Example
  11. For a real example, you can refer to [example.ts](./example.ts)
  12. ```ts
  13. import { walk } from 'ast-walker-scope'
  14. const code = `
  15. const a = 'root level'
  16. {
  17. const a = 'second level'
  18. let secondLevel = true
  19. console.log(a, secondLevel)
  20. }
  21. var err = undefined
  22. try {
  23. } catch (err) {
  24. console.log(err)
  25. }
  26. console.log(a)
  27. `.trim()
  28. walk(code, {
  29. leave(this, node) {
  30. if (node.type === 'CallExpression') {
  31. console.log(`\nLevel: ${this.level}`)
  32. for (const [name, node] of Object.entries(this.scope)) {
  33. console.log(
  34. `variable ${name} is located at line ${node.loc?.start.line}, column ${node.loc?.start.column}`
  35. )
  36. }
  37. }
  38. },
  39. })
  40. ```
  41. Output:
  42. ```
  43. Level: 2
  44. variable a is located at line 4, column 8
  45. variable secondLevel is located at line 5, column 6
  46. Level: 2
  47. variable a is located at line 1, column 6
  48. variable err is located at line 12, column 9
  49. Level: 1
  50. variable a is located at line 1, column 6
  51. variable err is located at line 9, column 4
  52. ```
  53. ## Typings
  54. ```ts
  55. export type Scope = Record<string, Node>
  56. export interface HookContext extends WalkerContext {
  57. // inherited from estree-walker
  58. skip: () => void
  59. remove: () => void
  60. replace: (node: Node) => void
  61. // arguments of estree-walker hook
  62. parent: Node
  63. key: string
  64. index: number
  65. // scope info
  66. scope: Scope
  67. scopes: Scope[]
  68. level: number
  69. }
  70. ```
  71. ## Sponsors
  72. <p align="center">
  73. <a href="https://cdn.jsdelivr.net/gh/sxzz/sponsors/sponsors.svg">
  74. <img src='https://cdn.jsdelivr.net/gh/sxzz/sponsors/sponsors.svg'/>
  75. </a>
  76. </p>
  77. ## Credits
  78. - [@vue/reactivity-transform](https://github.com/vuejs/core/blob/v3.2.37/packages/reactivity-transform/src/reactivityTransform.ts) - almost copy-like referenced
  79. ## License
  80. [MIT](./LICENSE) License © 2022 [三咲智子](https://github.com/sxzz)