用工小程序前端代码
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.

70 lines
2.0 KiB

6 months ago
  1. [![npm version](https://img.shields.io/npm/v/eslint-scope.svg)](https://www.npmjs.com/package/eslint-scope)
  2. [![Downloads](https://img.shields.io/npm/dm/eslint-scope.svg)](https://www.npmjs.com/package/eslint-scope)
  3. [![Build Status](https://github.com/eslint/eslint-scope/workflows/CI/badge.svg)](https://github.com/eslint/eslint-scope/actions)
  4. # ESLint Scope
  5. ESLint Scope is the [ECMAScript](http://www.ecma-international.org/publications/standards/Ecma-262.htm) scope analyzer used in ESLint. It is a fork of [escope](http://github.com/estools/escope).
  6. ## Install
  7. ```
  8. npm i eslint-scope --save
  9. ```
  10. ## 📖 Usage
  11. To use in an ESM file:
  12. ```js
  13. import * as eslintScope from 'eslint-scope';
  14. ```
  15. To use in a CommonJS file:
  16. ```js
  17. const eslintScope = require('eslint-scope');
  18. ```
  19. Example:
  20. ```js
  21. import * as eslintScope from 'eslint-scope';
  22. import * as espree from 'espree';
  23. import estraverse from 'estraverse';
  24. const ast = espree.parse(code, { range: true });
  25. const scopeManager = eslintScope.analyze(ast);
  26. const currentScope = scopeManager.acquire(ast); // global scope
  27. estraverse.traverse(ast, {
  28. enter (node, parent) {
  29. // do stuff
  30. if (/Function/.test(node.type)) {
  31. currentScope = scopeManager.acquire(node); // get current function scope
  32. }
  33. },
  34. leave(node, parent) {
  35. if (/Function/.test(node.type)) {
  36. currentScope = currentScope.upper; // set to parent scope
  37. }
  38. // do stuff
  39. }
  40. });
  41. ```
  42. ## Contributing
  43. Issues and pull requests will be triaged and responded to as quickly as possible. We operate under the [ESLint Contributor Guidelines](http://eslint.org/docs/developer-guide/contributing), so please be sure to read them before contributing. If you're not sure where to dig in, check out the [issues](https://github.com/eslint/eslint-scope/issues).
  44. ## Build Commands
  45. * `npm test` - run all linting and tests
  46. * `npm run lint` - run all linting
  47. ## License
  48. ESLint Scope is licensed under a permissive BSD 2-clause license.