|
| 1 | +import { Project } from 'glint-monorepo-test-utils'; |
| 2 | +import { describe, beforeEach, afterEach, test, expect } from 'vitest'; |
| 3 | +import { stripIndent } from 'common-tags'; |
| 4 | +import * as ts from 'typescript'; |
| 5 | + |
| 6 | +describe('Language Server: Organize Imports', () => { |
| 7 | + let project!: Project; |
| 8 | + |
| 9 | + beforeEach(async () => { |
| 10 | + project = await Project.create(); |
| 11 | + }); |
| 12 | + |
| 13 | + afterEach(async () => { |
| 14 | + await project.destroy(); |
| 15 | + }); |
| 16 | + |
| 17 | + test('no imports', () => { |
| 18 | + project.write({ |
| 19 | + 'index.ts': stripIndent` |
| 20 | +
|
| 21 | + export default class Application extends Component { |
| 22 | + static template = hbs\` |
| 23 | + Hello, world! |
| 24 | + \`; |
| 25 | + } |
| 26 | + `, |
| 27 | + }); |
| 28 | + |
| 29 | + let server = project.startLanguageServer(); |
| 30 | + let formatting = ts.getDefaultFormatCodeSettings(); |
| 31 | + let preferences = {}; |
| 32 | + let edits = server.organizeImports(project.fileURI('index.ts'), formatting, preferences); |
| 33 | + |
| 34 | + expect(edits).toEqual([]); |
| 35 | + }); |
| 36 | + |
| 37 | + test('ts: handles sorting imports', () => { |
| 38 | + project.write({ |
| 39 | + 'index.ts': stripIndent` |
| 40 | + import './App.css'; |
| 41 | + import EmberComponent from './ember-component'; |
| 42 | + import Component from '@ember/component'; |
| 43 | +
|
| 44 | + interface WrapperComponentSignature { |
| 45 | + Blocks: { |
| 46 | + default: [ |
| 47 | + { |
| 48 | + InnerComponent: WithBoundArgs<typeof EmberComponent, 'required'>; |
| 49 | + MaybeComponent?: ComponentLike<{ Args: { key: string } }>; |
| 50 | + } |
| 51 | + ]; |
| 52 | + }; |
| 53 | + } |
| 54 | +
|
| 55 | + // Second Import Block |
| 56 | + import logo from './logo.svg'; |
| 57 | + import { ComponentLike, WithBoundArgs } from '@glint/template'; |
| 58 | +
|
| 59 | + export default class WrapperComponent extends Component<WrapperComponentSignature> { |
| 60 | + logo = logo |
| 61 | + } |
| 62 | + `, |
| 63 | + }); |
| 64 | + |
| 65 | + let server = project.startLanguageServer(); |
| 66 | + |
| 67 | + let formatting = ts.getDefaultFormatCodeSettings(); |
| 68 | + let preferences = {}; |
| 69 | + let edits = server.organizeImports(project.fileURI('index.ts'), formatting, preferences); |
| 70 | + |
| 71 | + expect(edits).toEqual([ |
| 72 | + { |
| 73 | + newText: |
| 74 | + "import Component from '@ember/component';\nimport './App.css';\nimport EmberComponent from './ember-component';\n", |
| 75 | + range: { |
| 76 | + start: { character: 0, line: 0 }, |
| 77 | + end: { character: 0, line: 1 }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + { |
| 81 | + newText: '', |
| 82 | + range: { |
| 83 | + start: { character: 0, line: 1 }, |
| 84 | + end: { character: 0, line: 2 }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + { |
| 88 | + newText: '', |
| 89 | + range: { |
| 90 | + start: { character: 0, line: 2 }, |
| 91 | + end: { character: 0, line: 3 }, |
| 92 | + }, |
| 93 | + }, |
| 94 | + { |
| 95 | + newText: |
| 96 | + "import { ComponentLike, WithBoundArgs } from '@glint/template';\nimport logo from './logo.svg';\n", |
| 97 | + range: { |
| 98 | + start: { character: 0, line: 16 }, |
| 99 | + end: { character: 0, line: 17 }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + { |
| 103 | + newText: '', |
| 104 | + range: { |
| 105 | + start: { character: 0, line: 17 }, |
| 106 | + end: { character: 0, line: 18 }, |
| 107 | + }, |
| 108 | + }, |
| 109 | + ]); |
| 110 | + }); |
| 111 | + |
| 112 | + test('gts: handles sorting imports', () => { |
| 113 | + project.setGlintConfig({ environment: 'ember-template-imports' }); |
| 114 | + project.write({ |
| 115 | + 'index.gts': stripIndent` |
| 116 | + import Component from '@glimmer/component'; |
| 117 | + import { hash } from '@ember/helper'; |
| 118 | +
|
| 119 | + class List<T> extends Component { |
| 120 | + <template> |
| 121 | + <MaybeComponent /> |
| 122 | + <ol> |
| 123 | + {{#each-in (hash a=1 b='hi') as |key value|}} |
| 124 | + <li>{{key}}: {{value}}</li> |
| 125 | + {{/each-in}} |
| 126 | + </ol> |
| 127 | + </template> |
| 128 | + } |
| 129 | +
|
| 130 | + // Second Import Block |
| 131 | + import { ComponentLike, ModifierLike, HelperLike } from '@glint/template'; |
| 132 | + import { TOC } from '@ember/component/template-only'; |
| 133 | +
|
| 134 | + const MaybeComponent: undefined as TOC<{ Args: { arg: string } }> | undefined; |
| 135 | + declare const CanvasThing: ComponentLike<{ Args: { str: string }; Element: HTMLCanvasElement }>; |
| 136 | + `, |
| 137 | + }); |
| 138 | + |
| 139 | + let server = project.startLanguageServer(); |
| 140 | + |
| 141 | + let formatting = ts.getDefaultFormatCodeSettings(); |
| 142 | + let preferences = {}; |
| 143 | + let edits = server.organizeImports(project.fileURI('index.gts'), formatting, preferences); |
| 144 | + |
| 145 | + expect(edits).toEqual([ |
| 146 | + { |
| 147 | + newText: |
| 148 | + "import { hash } from '@ember/helper';\nimport Component from '@glimmer/component';\n", |
| 149 | + range: { |
| 150 | + start: { character: 0, line: 0 }, |
| 151 | + end: { character: 0, line: 1 }, |
| 152 | + }, |
| 153 | + }, |
| 154 | + { |
| 155 | + newText: '', |
| 156 | + range: { |
| 157 | + start: { character: 0, line: 1 }, |
| 158 | + end: { character: 0, line: 2 }, |
| 159 | + }, |
| 160 | + }, |
| 161 | + { |
| 162 | + newText: |
| 163 | + "import { TOC } from '@ember/component/template-only';\nimport { ComponentLike, HelperLike, ModifierLike } from '@glint/template';\n", |
| 164 | + range: { |
| 165 | + start: { character: 0, line: 15 }, |
| 166 | + end: { character: 0, line: 16 }, |
| 167 | + }, |
| 168 | + }, |
| 169 | + { |
| 170 | + newText: '', |
| 171 | + range: { |
| 172 | + start: { character: 0, line: 16 }, |
| 173 | + end: { character: 0, line: 17 }, |
| 174 | + }, |
| 175 | + }, |
| 176 | + ]); |
| 177 | + }); |
| 178 | +}); |
0 commit comments