-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
110 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
pattern identifier_scope($name) { | ||
or { | ||
statement_block($statements) where { | ||
$statements <: some variable($declarations) where { | ||
$declarations <: contains variable_declarator(name=$name) | ||
} | ||
}, | ||
function($parameters) where { | ||
$parameters <: contains $name | ||
}, | ||
arrow_function($parameters) where { | ||
$parameters <: contains $name | ||
}, | ||
function_declaration($parameters) where { | ||
$parameters <: contains $name | ||
}, | ||
for_in_statement() as $statement where { | ||
$statement <: contains $name | ||
}, | ||
for_statement() as $statement where { | ||
$statement <: contains $name | ||
}, | ||
`try { $_ } catch($catch) { $_ }` where { | ||
$catch <: contains $name | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
--- | ||
tags: | ||
- docs | ||
- full-examples | ||
--- | ||
|
||
# Variable Scoping with `identifier_scope` | ||
|
||
Default Grit patterns are not generally aware of variable scoping, but you can use the `identifier_scope` pattern to find (or exclude) [scopes](https://developer.mozilla.org/en-US/docs/Glossary/Scope) where an identifier has been _locally_ defined. | ||
|
||
This is most often used when you want to target an import from a shared module but exclude scopes where the identifier is shadowed locally. | ||
|
||
For example, this pattern would rename `t` from the `translation` library to `translate` unless `t` is shadowed locally: | ||
|
||
```grit | ||
language js | ||
`t` as $t => `translate` where { | ||
$t <: imported_from(from=`"translation"`), | ||
$t <: not within identifier_scope(name=`t`) | ||
} | ||
``` | ||
|
||
Here is a simple example file where `t` is shadowed locally: | ||
|
||
```js | ||
import { t } from 'translation'; | ||
|
||
console.log(t('hello world')); | ||
|
||
function normal() { | ||
console.log(t('hello world')); | ||
} | ||
|
||
// t is an argument to this function, so the global t is not used and we should *not* rename it here. | ||
function shadowed(t) { | ||
console.log(t('hello world')); | ||
} | ||
``` | ||
|
||
When we rewrite it, the shadowed `t` is not renamed: | ||
|
||
```js | ||
import { translate } from 'translation'; | ||
|
||
console.log(translate('hello world')); | ||
|
||
function normal() { | ||
console.log(translate('hello world')); | ||
} | ||
|
||
// t is an argument to this function, so the global t is not used and we should *not* rename it here. | ||
function shadowed(t) { | ||
console.log(t('hello world')); | ||
} | ||
``` |