Skip to content

Commit

Permalink
doc: add find import usage
Browse files Browse the repository at this point in the history
fix #636
  • Loading branch information
HerringtonDarkholme committed Jan 20, 2025
1 parent a57f264 commit eb29be7
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
1 change: 1 addition & 0 deletions website/catalog/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Feel free to join our [Discord](https://discord.gg/4YZjf6htSQ) channel and ask @
* [No `console` except in `catch` block](/catalog/typescript/#no-console-except-in-catch-block)
* [Find Import File without Extension](/catalog/typescript/#find-import-file-without-extension)
* [Migrate XState to V5 from V4](/catalog/typescript/#migrate-xstate-to-v5-from-v4)
* [Find Import Usage](/catalog/typescript/#find-import-usage)
* [TSX](/catalog/tsx/)
* [Avoid `&&` short circuit in JSX](/catalog/tsx/#avoid-short-circuit-in-jsx)
* [Rewrite MobX Component Style](/catalog/tsx/#rewrite-mobx-component-style)
Expand Down
46 changes: 46 additions & 0 deletions website/catalog/typescript/find-import-usage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
## Find Import Usage

* [Playground Link](/playground.html#eyJtb2RlIjoiQ29uZmlnIiwibGFuZyI6InR5cGVzY3JpcHQiLCJxdWVyeSI6IiIsInJld3JpdGUiOiIiLCJzdHJpY3RuZXNzIjoicmVsYXhlZCIsInNlbGVjdG9yIjoiIiwiY29uZmlnIjoicnVsZTpcbiAgIyB0aGUgdXNhZ2VcbiAga2luZDogaWRlbnRpZmllclxuICBwYXR0ZXJuOiAkTU9EXG4gICMgaXRzIHJlbGF0aW9uc2hpcCB0byB0aGUgcm9vdFxuICBpbnNpZGU6XG4gICAgc3RvcEJ5OiBlbmRcbiAgICBraW5kOiBwcm9ncmFtXG4gICAgIyBhbmQgYmFjayBkb3duIHRvIHRoZSBpbXBvcnQgc3RhdGVtZW50XG4gICAgaGFzOlxuICAgICAga2luZDogaW1wb3J0X3N0YXRlbWVudFxuICAgICAgIyBhbmQgZGVlcGVyIGludG8gdGhlIGltcG9ydCBzdGF0ZW1lbnQgbG9va2luZyBmb3IgdGhlIG1hdGNoaW5nIGlkZW50aWZpZXJcbiAgICAgIGhhczpcbiAgICAgICAgc3RvcEJ5OiBlbmRcbiAgICAgICAga2luZDogaW1wb3J0X3NwZWNpZmllclxuICAgICAgICBwYXR0ZXJuOiAkTU9EICMgc2FtZSBwYXR0ZXJuIGFzIHRoZSB1c2FnZSBpcyBlbmZvcmNlZCBoZXJlIiwic291cmNlIjoiaW1wb3J0IHsgTW9uZ29DbGllbnQgfSBmcm9tICdtb25nb2RiJztcbmNvbnN0IHVybCA9ICdtb25nb2RiOi8vbG9jYWxob3N0OjI3MDE3JztcbmFzeW5jIGZ1bmN0aW9uIHJ1bigpIHtcbiAgY29uc3QgY2xpZW50ID0gbmV3IE1vbmdvQ2xpZW50KHVybCk7XG59XG4ifQ==)

### Description

It is common to find the usage of an imported module in a codebase. This rule helps you to find the usage of an imported module in your codebase.
The idea of this rule can be broken into several parts:

* Find the use of an identifier `$MOD`
* To find the import, we first need to find the root file of which `$MOD` is `inside`
* The `program` file `has` an `import` statement
* The `import` statement `has` the identifier `$MOD`

<!-- Use YAML in the example. Delete this section if use pattern. -->
### YAML
```yaml
id: find-import-usage
language: typescript
rule:
kind: identifier # ast-grep requires a kind
pattern: $MOD # the identifier to find
inside: # find the root
stopBy: end
kind: program
has: # and has the import statement
kind: import_statement
has: # look for the matching identifier
stopBy: end
kind: import_specifier
pattern: $MOD # same pattern as the usage is enforced here
```
### Example
<!-- highlight matched code in curly-brace {lineNum} -->
```ts {4}
import { MongoClient } from 'mongodb';
const url = 'mongodb://localhost:27017';
async function run() {
const client = new MongoClient(url);
}
```

### Contributed by
[Steven Love](https://github.com/StevenLove)
3 changes: 2 additions & 1 deletion website/catalog/typescript/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ However, you can use the [`languageGlobs`](/reference/sgconfig.html#languageglob
<!--@include: ./find-import-file-without-extension.md-->
<!--@include: ./migrate-xstate-v5.md-->
<!--@include: ./no-await-in-promise-all.md-->
<!--@include: ./no-console-except-catch.md-->
<!--@include: ./no-console-except-catch.md-->
<!--@include: ./find-import-usage.md-->

0 comments on commit eb29be7

Please sign in to comment.