Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support parsing satisfies operators #5717

Merged
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -51,7 +51,7 @@ The latest version under the `canary` tag **(latest commit to `main`)** is:

### Supported TypeScript Version

**The version range of TypeScript currently supported by this parser is `>=3.3.1 <4.9.0`.**
**The version range of TypeScript currently supported by this parser is `>=3.3.1 <5.0.0`.**

These versions are what we test against.

Expand Down
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -104,10 +104,10 @@
"tmp": "^0.2.1",
"ts-node": "^10.7.0",
"tslint": "^6.1.3",
"typescript": ">=3.3.1 <4.9.0"
"typescript": ">=3.3.1 <5.0.0"
},
"resolutions": {
"typescript": "~4.8.3",
"typescript": "~4.9.3",
"@types/node": "^18.0.0",
"//": "Pin jest to v29 across the repo",
"@jest/create-cache-key-function": "^29",
Expand Down
1 change: 1 addition & 0 deletions packages/ast-spec/src/ast-node-types.ts
Expand Up @@ -145,6 +145,7 @@ export enum AST_NODE_TYPES {
TSQualifiedName = 'TSQualifiedName',
TSReadonlyKeyword = 'TSReadonlyKeyword',
TSRestType = 'TSRestType',
TSSatisfiesExpression = 'TSSatisfiesExpression',
TSStaticKeyword = 'TSStaticKeyword',
TSStringKeyword = 'TSStringKeyword',
TSSymbolKeyword = 'TSSymbolKeyword',
Expand Down
10 changes: 10 additions & 0 deletions packages/ast-spec/src/expression/TSSatisfiesExpression/spec.ts
@@ -0,0 +1,10 @@
import type { AST_NODE_TYPES } from '../../ast-node-types';
import type { BaseNode } from '../../base/BaseNode';
import type { Expression } from '../../unions/Expression';
import type { TypeNode } from '../../unions/TypeNode';

export interface TSSatisfiesExpression extends BaseNode {
type: AST_NODE_TYPES.TSSatisfiesExpression;
expression: Expression;
typeAnnotation: TypeNode;
}
1 change: 1 addition & 0 deletions packages/ast-spec/src/expression/spec.ts
Expand Up @@ -23,6 +23,7 @@ export * from './TSAsExpression/spec';
export * from './TSEmptyBodyFunctionExpression/spec';
export * from './TSInstantiationExpression/spec';
export * from './TSNonNullExpression/spec';
export * from './TSSatisfiesExpression/spec';
export * from './TSTypeAssertion/spec';
export * from './TaggedTemplateExpression/spec';
export * from './TemplateLiteral/spec';
Expand Down
2 changes: 2 additions & 0 deletions packages/ast-spec/src/unions/Expression.ts
Expand Up @@ -25,6 +25,7 @@ import type { ThisExpression } from '../expression/ThisExpression/spec';
import type { TSAsExpression } from '../expression/TSAsExpression/spec';
import type { TSInstantiationExpression } from '../expression/TSInstantiationExpression/spec';
import type { TSNonNullExpression } from '../expression/TSNonNullExpression/spec';
import type { TSSatisfiesExpression } from '../expression/TSSatisfiesExpression/spec';
import type { TSTypeAssertion } from '../expression/TSTypeAssertion/spec';
import type { UnaryExpression } from '../expression/UnaryExpression/spec';
import type { UpdateExpression } from '../expression/UpdateExpression/spec';
Expand Down Expand Up @@ -72,6 +73,7 @@ export type Expression =
| TSAsExpression
| TSInstantiationExpression
| TSNonNullExpression
| TSSatisfiesExpression
| TSTypeAssertion
| UnaryExpression
| UpdateExpression
Expand Down
2 changes: 2 additions & 0 deletions packages/ast-spec/src/unions/Node.ts
Expand Up @@ -53,6 +53,7 @@ import type { TSAsExpression } from '../expression/TSAsExpression/spec';
import type { TSEmptyBodyFunctionExpression } from '../expression/TSEmptyBodyFunctionExpression/spec';
import type { TSInstantiationExpression } from '../expression/TSInstantiationExpression/spec';
import type { TSNonNullExpression } from '../expression/TSNonNullExpression/spec';
import type { TSSatisfiesExpression } from '../expression/TSSatisfiesExpression/spec';
import type { TSTypeAssertion } from '../expression/TSTypeAssertion/spec';
import type { UnaryExpression } from '../expression/UnaryExpression/spec';
import type { UpdateExpression } from '../expression/UpdateExpression/spec';
Expand Down Expand Up @@ -305,6 +306,7 @@ export type Node =
| TSQualifiedName
| TSReadonlyKeyword
| TSRestType
| TSSatisfiesExpression
| TSStaticKeyword
| TSStringKeyword
| TSSymbolKeyword
Expand Down
18 changes: 12 additions & 6 deletions packages/scope-manager/src/lib/dom.ts
Expand Up @@ -66,7 +66,7 @@ export const dom = {
DeviceMotionEventInit: TYPE,
DeviceMotionEventRotationRateInit: TYPE,
DeviceOrientationEventInit: TYPE,
DisplayMediaStreamConstraints: TYPE,
DisplayMediaStreamOptions: TYPE,
DocumentTimelineOptions: TYPE,
DoubleRange: TYPE,
DragEventInit: TYPE,
Expand Down Expand Up @@ -143,7 +143,6 @@ export const dom = {
MediaMetadataInit: TYPE,
MediaPositionState: TYPE,
MediaQueryListEventInit: TYPE,
MediaRecorderErrorEventInit: TYPE,
MediaRecorderOptions: TYPE,
MediaSessionActionDetails: TYPE,
MediaStreamAudioSourceOptions: TYPE,
Expand Down Expand Up @@ -184,6 +183,7 @@ export const dom = {
PeriodicWaveConstraints: TYPE,
PeriodicWaveOptions: TYPE,
PermissionDescriptor: TYPE,
PictureInPictureEventInit: TYPE,
PointerEventInit: TYPE,
PopStateEventInit: TYPE,
PositionOptions: TYPE,
Expand Down Expand Up @@ -243,6 +243,7 @@ export const dom = {
RTCStats: TYPE,
RTCTrackEventInit: TYPE,
RTCTransportStats: TYPE,
ReadableStreamGetReaderOptions: TYPE,
ReadableStreamReadDoneResult: TYPE,
ReadableStreamReadValueResult: TYPE,
ReadableWritablePair: TYPE,
Expand Down Expand Up @@ -284,6 +285,8 @@ export const dom = {
TransitionEventInit: TYPE,
UIEventInit: TYPE,
ULongRange: TYPE,
UnderlyingByteSource: TYPE,
UnderlyingDefaultSource: TYPE,
UnderlyingSink: TYPE,
UnderlyingSource: TYPE,
ValidityStateFlags: TYPE,
Expand Down Expand Up @@ -350,10 +353,13 @@ export const dom = {
CSSConditionRule: TYPE_VALUE,
CSSCounterStyleRule: TYPE_VALUE,
CSSFontFaceRule: TYPE_VALUE,
CSSFontPaletteValuesRule: TYPE_VALUE,
CSSGroupingRule: TYPE_VALUE,
CSSImportRule: TYPE_VALUE,
CSSKeyframeRule: TYPE_VALUE,
CSSKeyframesRule: TYPE_VALUE,
CSSLayerBlockRule: TYPE_VALUE,
CSSLayerStatementRule: TYPE_VALUE,
CSSMediaRule: TYPE_VALUE,
CSSNamespaceRule: TYPE_VALUE,
CSSPageRule: TYPE_VALUE,
Expand Down Expand Up @@ -449,6 +455,7 @@ export const dom = {
EXT_frag_depth: TYPE,
EXT_sRGB: TYPE,
EXT_shader_texture_lod: TYPE,
EXT_texture_compression_bptc: TYPE,
EXT_texture_compression_rgtc: TYPE,
EXT_texture_filter_anisotropic: TYPE,
ElementEventMap: TYPE,
Expand Down Expand Up @@ -641,7 +648,6 @@ export const dom = {
MediaQueryListEvent: TYPE_VALUE,
MediaRecorderEventMap: TYPE,
MediaRecorder: TYPE_VALUE,
MediaRecorderErrorEvent: TYPE_VALUE,
MediaSession: TYPE_VALUE,
MediaSourceEventMap: TYPE,
MediaSource: TYPE_VALUE,
Expand Down Expand Up @@ -724,6 +730,7 @@ export const dom = {
PermissionStatusEventMap: TYPE,
PermissionStatus: TYPE_VALUE,
Permissions: TYPE_VALUE,
PictureInPictureEvent: TYPE_VALUE,
PictureInPictureWindowEventMap: TYPE,
PictureInPictureWindow: TYPE_VALUE,
Plugin: TYPE_VALUE,
Expand Down Expand Up @@ -1123,8 +1130,6 @@ export const dom = {
TimerHandler: TYPE,
Transferable: TYPE,
Uint32List: TYPE,
UvmEntries: TYPE,
UvmEntry: TYPE,
VibratePattern: TYPE,
WindowProxy: TYPE,
XMLHttpRequestBodyInit: TYPE,
Expand Down Expand Up @@ -1227,7 +1232,6 @@ export const dom = {
RTCIceCandidateType: TYPE,
RTCIceComponent: TYPE,
RTCIceConnectionState: TYPE,
RTCIceCredentialType: TYPE,
RTCIceGathererState: TYPE,
RTCIceGatheringState: TYPE,
RTCIceProtocol: TYPE,
Expand All @@ -1243,6 +1247,8 @@ export const dom = {
RTCSignalingState: TYPE,
RTCStatsIceCandidatePairState: TYPE,
RTCStatsType: TYPE,
ReadableStreamReaderMode: TYPE,
ReadableStreamType: TYPE,
ReadyState: TYPE,
RecordingState: TYPE,
ReferrerPolicy: TYPE,
Expand Down
11 changes: 11 additions & 0 deletions packages/scope-manager/src/lib/es2019.intl.ts
@@ -0,0 +1,11 @@
// THIS CODE WAS AUTOMATICALLY GENERATED
// DO NOT EDIT THIS CODE BY HAND
// RUN THE FOLLOWING COMMAND FROM THE WORKSPACE ROOT TO REGENERATE:
// npx nx generate-lib @typescript-eslint/scope-manager

import type { ImplicitLibVariableOptions } from '../variable';
import { TYPE_VALUE } from './base-config';

export const es2019_intl = {
Intl: TYPE_VALUE,
} as Record<string, ImplicitLibVariableOptions>;
2 changes: 2 additions & 0 deletions packages/scope-manager/src/lib/es2019.ts
Expand Up @@ -6,6 +6,7 @@
import type { ImplicitLibVariableOptions } from '../variable';
import { es2018 } from './es2018';
import { es2019_array } from './es2019.array';
import { es2019_intl } from './es2019.intl';
import { es2019_object } from './es2019.object';
import { es2019_string } from './es2019.string';
import { es2019_symbol } from './es2019.symbol';
Expand All @@ -16,4 +17,5 @@ export const es2019 = {
...es2019_object,
...es2019_string,
...es2019_symbol,
...es2019_intl,
} as Record<string, ImplicitLibVariableOptions>;
2 changes: 2 additions & 0 deletions packages/scope-manager/src/lib/index.ts
Expand Up @@ -38,6 +38,7 @@ import { es2018_regexp } from './es2018.regexp';
import { es2019 } from './es2019';
import { es2019_array } from './es2019.array';
import { es2019_full } from './es2019.full';
import { es2019_intl } from './es2019.intl';
import { es2019_object } from './es2019.object';
import { es2019_string } from './es2019.string';
import { es2019_symbol } from './es2019.symbol';
Expand Down Expand Up @@ -124,6 +125,7 @@ const lib = {
'es2019.object': es2019_object,
'es2019.string': es2019_string,
'es2019.symbol': es2019_symbol,
'es2019.intl': es2019_intl,
'es2020.bigint': es2020_bigint,
'es2020.date': es2020_date,
'es2020.promise': es2020_promise,
Expand Down
6 changes: 6 additions & 0 deletions packages/scope-manager/src/lib/webworker.ts
Expand Up @@ -88,6 +88,7 @@ export const webworker = {
QueuingStrategyInit: TYPE,
RTCEncodedAudioFrameMetadata: TYPE,
RTCEncodedVideoFrameMetadata: TYPE,
ReadableStreamGetReaderOptions: TYPE,
ReadableStreamReadDoneResult: TYPE,
ReadableStreamReadValueResult: TYPE,
ReadableWritablePair: TYPE,
Expand All @@ -108,6 +109,8 @@ export const webworker = {
TextDecoderOptions: TYPE,
TextEncoderEncodeIntoResult: TYPE,
Transformer: TYPE,
UnderlyingByteSource: TYPE,
UnderlyingDefaultSource: TYPE,
UnderlyingSink: TYPE,
UnderlyingSource: TYPE,
VideoColorSpaceInit: TYPE,
Expand Down Expand Up @@ -157,6 +160,7 @@ export const webworker = {
EXT_frag_depth: TYPE,
EXT_sRGB: TYPE,
EXT_shader_texture_lod: TYPE,
EXT_texture_compression_bptc: TYPE,
EXT_texture_compression_rgtc: TYPE,
EXT_texture_filter_anisotropic: TYPE,
ErrorEvent: TYPE_VALUE,
Expand Down Expand Up @@ -431,6 +435,8 @@ export const webworker = {
PremultiplyAlpha: TYPE,
PushEncryptionKeyName: TYPE,
RTCEncodedVideoFrameType: TYPE,
ReadableStreamReaderMode: TYPE,
ReadableStreamType: TYPE,
ReferrerPolicy: TYPE,
RequestCache: TYPE,
RequestCredentials: TYPE,
Expand Down
2 changes: 1 addition & 1 deletion packages/scope-manager/src/referencer/Referencer.ts
Expand Up @@ -675,7 +675,7 @@ class Referencer extends Visitor {
member.id.type === AST_NODE_TYPES.Literal &&
typeof member.id.value === 'string'
) {
const name = member.id as TSESTree.StringLiteral;
const name = member.id;
this.currentScope().defineLiteralIdentifier(
name,
new TSEnumMemberDefinition(name, member),
Expand Down
@@ -0,0 +1,11 @@
x satisfies T;
x < y satisfies boolean; // (x < y) satisfies boolean;
x === 1 satisfies number; // x === (1 satisfies number);
x satisfies any satisfies T;

const t2 = { a: 1, b: 1 } satisfies I1; // Error
const t3 = { } satisfies I1; // Error
const t4: T1 = { a: "a" } satisfies T1; // Ok
const t5 = (m => m.substring(0)) satisfies T2; // Ok
const t6 = [1, 2] satisfies [number, number];
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
const t7 = (async (context) => context.value) satisfies (context: { value: string }) => Promise<string>;
1 change: 1 addition & 0 deletions packages/types/src/lib.ts
Expand Up @@ -46,6 +46,7 @@ type Lib =
| 'es2019.object'
| 'es2019.string'
| 'es2019.symbol'
| 'es2019.intl'
| 'es2020.bigint'
| 'es2020.date'
| 'es2020.promise'
Expand Down
8 changes: 8 additions & 0 deletions packages/typescript-estree/src/convert.ts
Expand Up @@ -2954,6 +2954,14 @@ export class Converter {
});
}

case SyntaxKind.SatisfiesExpression: {
return this.createNode<TSESTree.TSSatisfiesExpression>(node, {
type: AST_NODE_TYPES.TSSatisfiesExpression,
expression: this.convertChild(node.expression),
typeAnnotation: this.convertChild(node.type),
});
}

default:
return this.deeplyCopy(node);
}
Expand Down
Expand Up @@ -6,7 +6,7 @@ import type { ParseSettings } from './index';
* This needs to be kept in sync with the top-level README.md in the
* typescript-eslint monorepo
*/
const SUPPORTED_TYPESCRIPT_VERSIONS = '>=3.3.1 <4.9.0';
const SUPPORTED_TYPESCRIPT_VERSIONS = '>=3.3.1 <5.0.0';

/*
* The semver package will ignore prerelease ranges, and we don't want to explicitly document every one
Expand Down
Expand Up @@ -185,6 +185,7 @@ export interface EstreeToTsNodeTypes {
[AST_NODE_TYPES.TSInterfaceHeritage]: ts.ExpressionWithTypeArguments;
[AST_NODE_TYPES.TSIntersectionType]: ts.IntersectionTypeNode;
[AST_NODE_TYPES.TSInstantiationExpression]: ts.ExpressionWithTypeArguments;
[AST_NODE_TYPES.TSSatisfiesExpression]: ts.SatisfiesExpression;
[AST_NODE_TYPES.TSLiteralType]: ts.LiteralTypeNode;
[AST_NODE_TYPES.TSMappedType]: ts.MappedTypeNode;
[AST_NODE_TYPES.TSMethodSignature]:
Expand Down
1 change: 1 addition & 0 deletions packages/typescript-estree/src/ts-estree/ts-nodes.ts
Expand Up @@ -180,6 +180,7 @@ export type TSNode =
| ts.UnparsedSource
| ts.JsonMinusNumericLiteral
| ts.TemplateLiteralTypeNode
| ts.SatisfiesExpression

// JSDoc: Unsupported
| ts.JSDoc
Expand Down
Expand Up @@ -477,6 +477,11 @@ tester.addFixturePatternConfig('typescript/expressions', {
* @see https://github.com/babel/babel/issues/14613
*/
'instantiation-expression',
/**
* TS 4.9 `satisfies` operator has not been implemented in Babel yet.
* @see https://github.com/babel/babel/pull/14211
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
*/
'satisfies-expression',
],
});

Expand Down
Expand Up @@ -2652,6 +2652,8 @@ exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" e

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/expressions/optional-call-expression-type-arguments.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/expressions/satisfies-expression.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/expressions/tagged-template-expression-type-arguments.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/namespaces-and-modules/ambient-module-declaration-with-import.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
Expand Down