UNPKG

eslint-codemod-utils

Version:

A collection of AST helper functions for more complex ESLint rule fixes.

90 lines (89 loc) 4.49 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const __1 = require(".."); const espree = __importStar(require("@typescript-eslint/parser")); const ESPREE_OPTIONS = { ecmaVersion: 2015, sourceType: 'module', }; describe('tsAsExpression', () => { test('basic', () => { expect(String((0, __1.tsAsExpression)({ expression: (0, __1.literal)('hello'), typeAnnotation: (0, __1.tsAnyKeyword)({}), }))).eq(`'hello' as any`); }); test('parsed with as string keyword', () => { const { body } = espree.parse(`const x = 'hello' as string`, ESPREE_OPTIONS); expect((0, __1.node)(body[0]).toString()).eq(`const x = 'hello' as string`); }); test('parsed with as type', () => { const { body } = espree.parse(`const x = 'hello' as World`, ESPREE_OPTIONS); expect((0, __1.node)(body[0]).toString()).eq(`const x = 'hello' as World`); }); test('parsed with as type with type parameter', () => { const { body } = espree.parse(`"2" as React.Ref<HTMLDivElement>`, ESPREE_OPTIONS); expect((0, __1.node)(body[0]).toString()).eq(`"2" as React.Ref<HTMLDivElement>`); }); test('parsed with as type with type parameter', () => { const { body } = espree.parse(`"2" as React.Ref<HTMLDivElement, true>`, ESPREE_OPTIONS); expect((0, __1.node)(body[0]).toString()).eq(`"2" as React.Ref<HTMLDivElement, true>`); }); test('parsed non-null ts expression', () => { const { body } = espree.parse(`inputEl.current!.select()`, ESPREE_OPTIONS); expect((0, __1.node)(body[0]).toString()).eq(`inputEl.current!.select()`); }); test('parsed keyword assertions', () => { const { body } = espree.parse(`"10" as any as unknown as null as boolean`, ESPREE_OPTIONS); expect((0, __1.node)(body[0]).toString()).eq(`"10" as any as unknown as null as boolean`); }); test('parsed ts union & intersection types', () => { const { body } = espree.parse(`type X = 'hello' | 'thing' & 8`, ESPREE_OPTIONS); expect((0, __1.node)(body[0]).toString()).eq(`type X = 'hello' | 'thing' & 8`); }); test('parsed ts type query', () => { const { body } = espree.parse(`type X = 'hello'\ntype Y = typeof X`, ESPREE_OPTIONS); expect((0, __1.node)(body[0]).toString()).eq(`type X = 'hello'`); expect((0, __1.node)(body[1]).toString()).eq(`type Y = typeof X`); }); test('parsed ts type operator', () => { const { body } = espree.parse(`type X = 'hello'\ntype Y = keyof typeof X`, ESPREE_OPTIONS); expect((0, __1.node)(body[0]).toString()).eq(`type X = 'hello'`); expect((0, __1.node)(body[1]).toString()).eq(`type Y = keyof typeof X`); }); test('type alias declaration (keyword)', () => { const { body } = espree.parse(`type X = string`, ESPREE_OPTIONS); expect((0, __1.node)(body[0]).toString()).eq(`type X = string`); }); test('type alias declaration (literal)', () => { const { body } = espree.parse(`type X = 'hello'`, ESPREE_OPTIONS); expect((0, __1.node)(body[0]).toString()).eq(`type X = 'hello'`); }); test('parsed ts union & intersection types with generic', () => { const { body } = espree.parse(`type X<T> = 'hello' | 'thing' & 8`, ESPREE_OPTIONS); expect((0, __1.node)(body[0]).toString()).eq(`type X<T> = 'hello' | 'thing' & 8`); }); });