Skip to content

Commit eb29be7

Browse files
doc: add find import usage
fix #636
1 parent a57f264 commit eb29be7

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

website/catalog/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Feel free to join our [Discord](https://discord.gg/4YZjf6htSQ) channel and ask @
3737
* [No `console` except in `catch` block](/catalog/typescript/#no-console-except-in-catch-block)
3838
* [Find Import File without Extension](/catalog/typescript/#find-import-file-without-extension)
3939
* [Migrate XState to V5 from V4](/catalog/typescript/#migrate-xstate-to-v5-from-v4)
40+
* [Find Import Usage](/catalog/typescript/#find-import-usage)
4041
* [TSX](/catalog/tsx/)
4142
* [Avoid `&&` short circuit in JSX](/catalog/tsx/#avoid-short-circuit-in-jsx)
4243
* [Rewrite MobX Component Style](/catalog/tsx/#rewrite-mobx-component-style)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
## Find Import Usage
2+
3+
* [Playground Link](/playground.html#eyJtb2RlIjoiQ29uZmlnIiwibGFuZyI6InR5cGVzY3JpcHQiLCJxdWVyeSI6IiIsInJld3JpdGUiOiIiLCJzdHJpY3RuZXNzIjoicmVsYXhlZCIsInNlbGVjdG9yIjoiIiwiY29uZmlnIjoicnVsZTpcbiAgIyB0aGUgdXNhZ2VcbiAga2luZDogaWRlbnRpZmllclxuICBwYXR0ZXJuOiAkTU9EXG4gICMgaXRzIHJlbGF0aW9uc2hpcCB0byB0aGUgcm9vdFxuICBpbnNpZGU6XG4gICAgc3RvcEJ5OiBlbmRcbiAgICBraW5kOiBwcm9ncmFtXG4gICAgIyBhbmQgYmFjayBkb3duIHRvIHRoZSBpbXBvcnQgc3RhdGVtZW50XG4gICAgaGFzOlxuICAgICAga2luZDogaW1wb3J0X3N0YXRlbWVudFxuICAgICAgIyBhbmQgZGVlcGVyIGludG8gdGhlIGltcG9ydCBzdGF0ZW1lbnQgbG9va2luZyBmb3IgdGhlIG1hdGNoaW5nIGlkZW50aWZpZXJcbiAgICAgIGhhczpcbiAgICAgICAgc3RvcEJ5OiBlbmRcbiAgICAgICAga2luZDogaW1wb3J0X3NwZWNpZmllclxuICAgICAgICBwYXR0ZXJuOiAkTU9EICMgc2FtZSBwYXR0ZXJuIGFzIHRoZSB1c2FnZSBpcyBlbmZvcmNlZCBoZXJlIiwic291cmNlIjoiaW1wb3J0IHsgTW9uZ29DbGllbnQgfSBmcm9tICdtb25nb2RiJztcbmNvbnN0IHVybCA9ICdtb25nb2RiOi8vbG9jYWxob3N0OjI3MDE3JztcbmFzeW5jIGZ1bmN0aW9uIHJ1bigpIHtcbiAgY29uc3QgY2xpZW50ID0gbmV3IE1vbmdvQ2xpZW50KHVybCk7XG59XG4ifQ==)
4+
5+
### Description
6+
7+
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.
8+
The idea of this rule can be broken into several parts:
9+
10+
* Find the use of an identifier `$MOD`
11+
* To find the import, we first need to find the root file of which `$MOD` is `inside`
12+
* The `program` file `has` an `import` statement
13+
* The `import` statement `has` the identifier `$MOD`
14+
15+
<!-- Use YAML in the example. Delete this section if use pattern. -->
16+
### YAML
17+
```yaml
18+
id: find-import-usage
19+
language: typescript
20+
rule:
21+
kind: identifier # ast-grep requires a kind
22+
pattern: $MOD # the identifier to find
23+
inside: # find the root
24+
stopBy: end
25+
kind: program
26+
has: # and has the import statement
27+
kind: import_statement
28+
has: # look for the matching identifier
29+
stopBy: end
30+
kind: import_specifier
31+
pattern: $MOD # same pattern as the usage is enforced here
32+
```
33+
34+
### Example
35+
36+
<!-- highlight matched code in curly-brace {lineNum} -->
37+
```ts {4}
38+
import { MongoClient } from 'mongodb';
39+
const url = 'mongodb://localhost:27017';
40+
async function run() {
41+
const client = new MongoClient(url);
42+
}
43+
```
44+
45+
### Contributed by
46+
[Steven Love](https://github.com/StevenLove)

website/catalog/typescript/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ However, you can use the [`languageGlobs`](/reference/sgconfig.html#languageglob
1313
<!--@include: ./find-import-file-without-extension.md-->
1414
<!--@include: ./migrate-xstate-v5.md-->
1515
<!--@include: ./no-await-in-promise-all.md-->
16-
<!--@include: ./no-console-except-catch.md-->
16+
<!--@include: ./no-console-except-catch.md-->
17+
<!--@include: ./find-import-usage.md-->

0 commit comments

Comments
 (0)