Skip to content

3.9 Regression: organizeImports prepends/appends "undefined" when there are 2+ named imports #38548

Closed
@mathieumg

Description

@mathieumg

TypeScript Version: 3.9.2

Search Terms: organizeImports, imports, undefined

Code

import {
  stat,
  statSync,
} from "fs";

export function fakeFn() {
  stat;
  statSync;
}

Run through ts.organizeImports.

Expected behavior:

import { stat, statSync } from "fs";

export function fakeFn() {
  stat;
  statSync;
}

Actual behavior:

import { undefinedstat, statSyncundefined } from "fs";

export function fakeFn() {
  stat;
  statSync;
}

Doesn't happen if there's only one import:

import { stat } from "fs";

export function fakeFn() {
  stat;
}

Only happens at the start and end of the block:

import {
  lstatSync,
  stat,
  statSync,
} from "fs";

export function fakeFn() {
  lstatSync;
  stat;
  statSync;
}

will yield:

import { undefinedlstatSync, stat, statSyncundefined } from "fs";

export function fakeFn() {
  lstatSync;
  stat;
  statSync;
}

Playground Link: https://www.typescriptlang.org/play/?ssl=1&ssc=1&pln=9&pc=1#code/JYWwDg9gTgLgBAbwFBzgZxgQxgGherGAZQE8A7AYzwF84AzKCEOAIjrRYG4kkBTAD0ix6AV0oxgEMvUwBrXgDEyACgCUifBmzdUW4uQrdqSIA

Don't think it can run the programmatic API.

https://ts-ast-viewer.com/#code/JYWwDg9gTgLgBAbwFBzgZxgQxgGherGAZQE8A7AYzwF84AzKCEOAIjrRYG4kkBTAD0ix6AV0oxgEMvUwBrXgDEyACgCUifBmzdUW4uQrdqSIA

Doesn't have TS 3.9 yet.

I setup a demo repo with both TS 3.8.7 and TS 3.9.2 here:

https://github.com/mathieumg/organize-imports/blob/v3.8.3/index.js yields:

TypeScript 3.8.3
Text Changes [
  {
    span: { start: 0, length: 55 },
    newText: 'import { lstatSync,stat,statSync } from "fs";\r\n'
  }
]
Failure? NO

and https://github.com/mathieumg/organize-imports/blob/v3.9.2/index.js yields:

TypeScript 3.9.2
Text Changes [
  {
    span: { start: 0, length: 55 },
    newText: 'import {undefinedlstatSync,\r\nstat,\r\nstatSyncundefined} from "fs";\r\n'
  }
]
Failure? YES

It's a simplified version of https://github.com/simonhaenisch/prettier-plugin-organize-imports/blob/f590b06c382aeeeeb3dadd9191c31aab9284d68c/index.js (which my use case emanates from), so it's not impossible they could be misusing the Language Service API. (but I wouldn't know) I imagine it could also be there was an undocumented breaking change there.

Possibly Related Issues: #38507

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions