Skip to content

Commit

Permalink
fix: skip when formatting ranges (#112)
Browse files Browse the repository at this point in the history
Co-authored-by: simonhaenisch <simonhaenisch@users.noreply.github.com>
  • Loading branch information
MattLish and simonhaenisch committed Nov 9, 2023
1 parent 9a10490 commit 5ebe7d1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.idea
9 changes: 9 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ const organizeImports = (code, options) => {
return code;
}

const isRange =
Boolean(options.originalText) ||
options.rangeStart !== 0 ||
(options.rangeEnd !== Infinity && options.rangeEnd !== code.length);

if (isRange) {
return code; // processing a range doesn't make sense
}

try {
return organize(code, options);
} catch (error) {
Expand Down
10 changes: 10 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ test(
{ transformer: (res) => res.split('\n')[1] },
);

test('skips when formatting a range', async (t) => {
const code = 'import { foo } from "./bar";';

const formattedCode1 = await prettify(code, { rangeEnd: 10 });
const formattedCode2 = await prettify(code, { rangeStart: 10 });

t.is(formattedCode1, code);
t.is(formattedCode2, code);
});

test('does not remove unused imports with `organizeImportsSkipDestructiveCodeActions` enabled', async (t) => {
const code = `import { foo } from "./bar";
`;
Expand Down

0 comments on commit 5ebe7d1

Please sign in to comment.