From 128d2ba5e9e158aa06d99a504637249a8ac9abf2 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Tue, 22 Nov 2022 16:52:54 +1030 Subject: [PATCH] feat(scope-manager): add support for TS4.9 satisfies expression --- .prettierignore | 1 + .../src/referencer/Referencer.ts | 9 +- .../fixtures/type-assertion/satisfies.ts | 4 + .../fixtures/type-assertion/satisfies.ts.shot | 84 +++++++++++++++++++ 4 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 packages/scope-manager/tests/fixtures/type-assertion/satisfies.ts create mode 100644 packages/scope-manager/tests/fixtures/type-assertion/satisfies.ts.shot diff --git a/.prettierignore b/.prettierignore index 88af276a1b2..bb855f41157 100644 --- a/.prettierignore +++ b/.prettierignore @@ -14,6 +14,7 @@ packages/ast-spec/src/*/*/fixtures/_error_/*/fixture.ts # prettier doesn't yet support satisfies packages/ast-spec/src/expression/TSSatisfiesExpression/fixtures/ +packages/scope-manager/tests/fixtures/type-assertion/satisfies.ts # Ignore CHANGELOG.md files to avoid issues with automated release job CHANGELOG.md diff --git a/packages/scope-manager/src/referencer/Referencer.ts b/packages/scope-manager/src/referencer/Referencer.ts index 93b0270ebfb..e7b41127ba4 100644 --- a/packages/scope-manager/src/referencer/Referencer.ts +++ b/packages/scope-manager/src/referencer/Referencer.ts @@ -301,7 +301,10 @@ class Referencer extends Visitor { } protected visitTypeAssertion( - node: TSESTree.TSAsExpression | TSESTree.TSTypeAssertion, + node: + | TSESTree.TSAsExpression + | TSESTree.TSTypeAssertion + | TSESTree.TSSatisfiesExpression, ): void { this.visit(node.expression); this.visitType(node.typeAnnotation); @@ -724,6 +727,10 @@ class Referencer extends Visitor { this.close(node); } + protected TSSatisfiesExpression(node: TSESTree.TSSatisfiesExpression): void { + this.visitTypeAssertion(node); + } + protected TSTypeAliasDeclaration( node: TSESTree.TSTypeAliasDeclaration, ): void { diff --git a/packages/scope-manager/tests/fixtures/type-assertion/satisfies.ts b/packages/scope-manager/tests/fixtures/type-assertion/satisfies.ts new file mode 100644 index 00000000000..69dae24ecf2 --- /dev/null +++ b/packages/scope-manager/tests/fixtures/type-assertion/satisfies.ts @@ -0,0 +1,4 @@ +const x = 1; +type T = 1; + +x satisfies T; diff --git a/packages/scope-manager/tests/fixtures/type-assertion/satisfies.ts.shot b/packages/scope-manager/tests/fixtures/type-assertion/satisfies.ts.shot new file mode 100644 index 00000000000..9d6dcc03ce1 --- /dev/null +++ b/packages/scope-manager/tests/fixtures/type-assertion/satisfies.ts.shot @@ -0,0 +1,84 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`type-assertion satisfies 1`] = ` +ScopeManager { + variables: Array [ + ImplicitGlobalConstTypeVariable, + Variable$2 { + defs: Array [ + VariableDefinition$1 { + name: Identifier<"x">, + node: VariableDeclarator$1, + }, + ], + name: "x", + references: Array [ + Reference$1 { + identifier: Identifier<"x">, + init: true, + isRead: false, + isTypeReference: false, + isValueReference: true, + isWrite: true, + resolved: Variable$2, + writeExpr: Literal$2, + }, + Reference$2 { + identifier: Identifier<"x">, + isRead: true, + isTypeReference: false, + isValueReference: true, + isWrite: false, + resolved: Variable$2, + }, + ], + isValueVariable: true, + isTypeVariable: false, + }, + Variable$3 { + defs: Array [ + TypeDefinition$2 { + name: Identifier<"T">, + node: TSTypeAliasDeclaration$3, + }, + ], + name: "T", + references: Array [ + Reference$3 { + identifier: Identifier<"T">, + isRead: true, + isTypeReference: true, + isValueReference: false, + isWrite: false, + resolved: Variable$3, + }, + ], + isValueVariable: false, + isTypeVariable: true, + }, + ], + scopes: Array [ + GlobalScope$1 { + block: Program$4, + isStrict: false, + references: Array [ + Reference$1, + Reference$2, + Reference$3, + ], + set: Map { + "const" => ImplicitGlobalConstTypeVariable, + "x" => Variable$2, + "T" => Variable$3, + }, + type: "global", + upper: null, + variables: Array [ + ImplicitGlobalConstTypeVariable, + Variable$2, + Variable$3, + ], + }, + ], +} +`;