Skip to content

Commit

Permalink
fix(ast-spec): extract AssignmentOperatorToText
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDeBoey committed Feb 24, 2022
1 parent 851bb90 commit 8cba48e
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 36 deletions.
@@ -0,0 +1,20 @@
import type { SyntaxKind } from 'typescript';

export interface AssignmentOperatorToText {
[SyntaxKind.EqualsToken]: '=';
[SyntaxKind.PlusEqualsToken]: '+=';
[SyntaxKind.MinusEqualsToken]: '-=';
[SyntaxKind.AsteriskEqualsToken]: '*=';
[SyntaxKind.AsteriskAsteriskEqualsToken]: '**=';
[SyntaxKind.SlashEqualsToken]: '/=';
[SyntaxKind.PercentEqualsToken]: '%=';
[SyntaxKind.LessThanLessThanEqualsToken]: '<<=';
[SyntaxKind.GreaterThanGreaterThanEqualsToken]: '>>=';
[SyntaxKind.GreaterThanGreaterThanGreaterThanEqualsToken]: '>>>=';
[SyntaxKind.AmpersandEqualsToken]: '&=';
[SyntaxKind.BarEqualsToken]: '|=';
[SyntaxKind.BarBarEqualsToken]: '||=';
[SyntaxKind.AmpersandAmpersandEqualsToken]: '&&=';
[SyntaxKind.QuestionQuestionEqualsToken]: '??=';
[SyntaxKind.CaretEqualsToken]: '^=';
}
22 changes: 5 additions & 17 deletions packages/ast-spec/src/expression/AssignmentExpression/spec.ts
@@ -1,26 +1,14 @@
import type { AST_NODE_TYPES } from '../../ast-node-types';
import type { BaseNode } from '../../base/BaseNode';
import type { Expression } from '../../unions/Expression';
import type { ValueOf } from '../../utils';
import type { AssignmentOperatorToText } from './AssignmentOperatorToText';

export * from './AssignmentOperatorToText';

export interface AssignmentExpression extends BaseNode {
type: AST_NODE_TYPES.AssignmentExpression;
operator:
| '-='
| '??='
| '**='
| '*='
| '/='
| '&&='
| '&='
| '%='
| '^='
| '+='
| '<<='
| '='
| '>>='
| '>>>='
| '|='
| '||=';
operator: ValueOf<AssignmentOperatorToText>;
left: Expression;
right: Expression;
}
@@ -1,6 +1,8 @@
import type { SyntaxKind } from 'typescript';

export interface PunctuatorTokenToText {
import type { AssignmentOperatorToText } from '../../expression/AssignmentExpression/AssignmentOperatorToText';

export interface PunctuatorTokenToText extends AssignmentOperatorToText {
[SyntaxKind.OpenBraceToken]: '{';
[SyntaxKind.CloseBraceToken]: '}';
[SyntaxKind.OpenParenToken]: '(';
Expand Down Expand Up @@ -46,20 +48,4 @@ export interface PunctuatorTokenToText {
[SyntaxKind.QuestionQuestionToken]: '??';
[SyntaxKind.BacktickToken]: '`';
[SyntaxKind.HashToken]: '#';
[SyntaxKind.EqualsToken]: '=';
[SyntaxKind.PlusEqualsToken]: '+=';
[SyntaxKind.MinusEqualsToken]: '-=';
[SyntaxKind.AsteriskEqualsToken]: '*=';
[SyntaxKind.AsteriskAsteriskEqualsToken]: '**=';
[SyntaxKind.SlashEqualsToken]: '/=';
[SyntaxKind.PercentEqualsToken]: '%=';
[SyntaxKind.LessThanLessThanEqualsToken]: '<<=';
[SyntaxKind.GreaterThanGreaterThanEqualsToken]: '>>=';
[SyntaxKind.GreaterThanGreaterThanGreaterThanEqualsToken]: '>>>=';
[SyntaxKind.AmpersandEqualsToken]: '&=';
[SyntaxKind.BarEqualsToken]: '|=';
[SyntaxKind.BarBarEqualsToken]: '||=';
[SyntaxKind.AmpersandAmpersandEqualsToken]: '&&=';
[SyntaxKind.QuestionQuestionEqualsToken]: '??=';
[SyntaxKind.CaretEqualsToken]: '^=';
}
3 changes: 1 addition & 2 deletions packages/ast-spec/src/token/PunctuatorToken/spec.ts
@@ -1,11 +1,10 @@
import type { AST_TOKEN_TYPES } from '../../ast-token-types';
import type { BaseToken } from '../../base/BaseToken';
import type { ValueOf } from '../../utils';
import type { PunctuatorTokenToText } from './PunctuatorTokenToText';

export * from './PunctuatorTokenToText';

type ValueOf<T> = T[keyof T];

export interface PunctuatorToken extends BaseToken {
type: AST_TOKEN_TYPES.Punctuator;
value: ValueOf<PunctuatorTokenToText>;
Expand Down
1 change: 1 addition & 0 deletions packages/ast-spec/src/utils.ts
@@ -0,0 +1 @@
export type ValueOf<T> = T[keyof T];
11 changes: 11 additions & 0 deletions packages/ast-spec/tests/AssignmentOperatorToText.test.ts
@@ -0,0 +1,11 @@
import type { AssignmentOperator } from 'typescript';

import { AssignmentOperatorToText } from '../src';

// @ts-expect-error: purposely unused
type _Test = {
readonly [T in AssignmentOperator]: AssignmentOperatorToText[T];
// If there are any AssignmentOperator members that don't have a corresponding
// AssignmentOperatorToText, then this line will error with "Type 'T' cannot
// be used to index type 'AssignmentOperatorToText'."
};

0 comments on commit 8cba48e

Please sign in to comment.