Skip to content

Commit

Permalink
fix(js_semantic): add semantic scope to mapped types (biomejs#958)
Browse files Browse the repository at this point in the history
  • Loading branch information
Conaclos authored and yossydev committed Dec 3, 2023
1 parent af990c0 commit 3305ae4
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom

- Fix [#918](https://github.com/biomejs/biome/issues/918), [useSimpleNumberKeys](https://biomejs.dev/linter/rules/use-simple-number-keys) no longer repports false positive on comments. Contributed by @kalleep

- Fix [#953](https://github.com/biomejs/biome/issues/953), [noRedeclare](https://biomejs.dev/linter/rules/no-redeclare) no longer reports type parameters with the same name in different mapped types as redeclarations. Contributed by @Conaclos

### Parser

## 1.4.0 (2023-11-27)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// See https://github.com/biomejs/biome/issues/953
type X = never;
type Y = never;
export const MyMappingPbToGql: {
[key in X]: never;
} = {};
export const MyOtherMappingPbToGql: {
[key in Y]: never;
} = {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: valid-mapped-types.ts
---
# Input
```js
// See https://github.com/biomejs/biome/issues/953
type X = never;
type Y = never;
export const MyMappingPbToGql: {
[key in X]: never;
} = {};
export const MyOtherMappingPbToGql: {
[key in Y]: never;
} = {};

```


28 changes: 22 additions & 6 deletions crates/biome_js_semantic/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ impl SemanticEventExtractor {
/// See [SemanticEvent] for a more detailed description of which events [SyntaxNode] generates.
#[inline]
pub fn enter(&mut self, node: &JsSyntaxNode) {
// If you push a scope for a given node type, don't forget to also update `Self::leave`.
match node.kind() {
JS_IDENTIFIER_BINDING | TS_IDENTIFIER_BINDING | TS_TYPE_PARAMETER_NAME => {
self.enter_identifier_binding(&AnyJsIdentifierBinding::unwrap_cast(node.clone()));
Expand Down Expand Up @@ -355,9 +356,15 @@ impl SemanticEventExtractor {
fn enter_any_type(&mut self, node: &AnyTsType) {
if node.in_conditional_true_type() {
self.push_conditional_true_scope(node);
} else if let Some(node) = node.as_ts_function_type() {
return;
}
let node = node.syntax();
if matches!(
node.kind(),
JsSyntaxKind::TS_FUNCTION_TYPE | JsSyntaxKind::TS_MAPPED_TYPE
) {
self.push_scope(
node.syntax().text_range(),
node.text_range(),
ScopeHoisting::DontHoistDeclarationsToParent,
false,
);
Expand Down Expand Up @@ -589,8 +596,9 @@ impl SemanticEventExtractor {
#[inline]
pub fn leave(&mut self, node: &JsSyntaxNode) {
match node.kind() {
JS_MODULE | JS_SCRIPT => self.pop_scope(node.text_range()),
JS_FUNCTION_DECLARATION
JS_MODULE
| JS_SCRIPT
| JS_FUNCTION_DECLARATION
| JS_FUNCTION_EXPORT_DEFAULT_DECLARATION
| JS_FUNCTION_EXPRESSION
| JS_ARROW_FUNCTION_EXPRESSION
Expand Down Expand Up @@ -636,8 +644,14 @@ impl SemanticEventExtractor {
fn leave_any_type(&mut self, node: &AnyTsType) {
if node.in_conditional_true_type() {
self.pop_scope(node.syntax().text_range());
} else if let Some(node) = node.as_ts_function_type() {
self.pop_scope(node.syntax().text_range());
return;
}
let node = node.syntax();
if matches!(
node.kind(),
JsSyntaxKind::TS_FUNCTION_TYPE | JsSyntaxKind::TS_MAPPED_TYPE
) {
self.pop_scope(node.text_range());
}
}

Expand Down Expand Up @@ -922,6 +936,8 @@ impl Iterator for SemanticEventIterator {
if let Some(e) = self.extractor.pop() {
break Some(e);
} else {
// Check that every scope was pop.
debug_assert!(self.extractor.scopes.is_empty());
break None;
}
}
Expand Down
2 changes: 2 additions & 0 deletions website/src/content/docs/internals/changelog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom

- Fix [#918](https://github.com/biomejs/biome/issues/918), [useSimpleNumberKeys](https://biomejs.dev/linter/rules/use-simple-number-keys) no longer repports false positive on comments. Contributed by @kalleep

- Fix [#953](https://github.com/biomejs/biome/issues/953), [noRedeclare](https://biomejs.dev/linter/rules/no-redeclare) no longer reports type parameters with the same name in different mapped types as redeclarations. Contributed by @Conaclos

### Parser

## 1.4.0 (2023-11-27)
Expand Down

0 comments on commit 3305ae4

Please sign in to comment.