forked from IanVS/prettier-plugin-sort-imports
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support // prettier-ignore-start and // prettier-ignore--end comments
Part of trivago/prettier-plugin-sort-imports#112 Note: Due to a limitation of how the AST is generated from the import nodes, an empty lines is inserted before the range end comment: // prettier-ignore-start import "e"; import c from "c"; import { d } from "d"; // prettier-ignore-end import { f } from "f"; import { g } from "g";
- Loading branch information
1 parent
cee20f7
commit 838a48c
Showing
15 changed files
with
240 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { parse as babelParser } from '@babel/parser'; | ||
import { CommentBlock, CommentLine } from '@babel/types'; | ||
import { getRangeIgnoredLines } from "../get-range-ignored-lines"; | ||
|
||
function getComments(code: string): (CommentBlock | CommentLine)[] { | ||
return babelParser(code, { | ||
sourceType: 'module', | ||
}).comments ?? []; | ||
} | ||
|
||
test('it does not find ranges when there are no ignore ranged', () => { | ||
const comments = getComments(`import a from "a";`); | ||
expect(comments.length).toBe(0); | ||
expect(getRangeIgnoredLines(comments)).toEqual(new Set()); | ||
}); | ||
|
||
test('it finds a range delimited by start and end comments', () => { | ||
const comments = getComments(`import a from "a"; | ||
// prettier-ignore-start | ||
import b from "b"; | ||
import c from "c"; | ||
// prettier-ignore-end | ||
import d from "d";`); | ||
expect(comments.length).toBe(2); | ||
expect(getRangeIgnoredLines(comments)).toEqual(new Set([2, 3, 4, 5])); | ||
}); | ||
|
||
test('it includes the line on which a block comment is placed', () => { | ||
const comments = getComments(`import a from "a"; | ||
import b from "b"; /* prettier-ignore-start */ | ||
/* prettier-ignore-end */ import c from "c"; | ||
import d from "d";`); | ||
expect(comments.length).toBe(2); | ||
expect(getRangeIgnoredLines(comments)).toEqual(new Set([2, 3])); | ||
}); | ||
|
||
test('it considers only the first start and end comment', () => { | ||
const comments = getComments(`import a from "a"; | ||
// prettier-ignore-start | ||
import b from "b"; | ||
// prettier-ignore-start | ||
import c from "c"; | ||
// prettier-ignore-end | ||
import d from "d"; | ||
// prettier-ignore-end | ||
import e from "e";`); | ||
expect(comments.length).toBe(4); | ||
expect(getRangeIgnoredLines(comments)).toEqual(new Set([2, 3, 4, 5, 6])); | ||
}); | ||
|
||
test('it ignores unfinished start comments', () => { | ||
const comments = getComments(`import a from "a"; | ||
// prettier-ignore-start | ||
import b from "b"; | ||
// prettier-ignore-start | ||
import c from "c"; | ||
import d from "d"; | ||
import e from "e";`); | ||
expect(comments.length).toBe(2); | ||
expect(getRangeIgnoredLines(comments)).toEqual(new Set([])); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { Comment } from "@babel/types"; | ||
import { GetRangeIgnoredLines } from "../types"; | ||
|
||
type Range = readonly [number, number]; | ||
type Ranges = readonly Range[]; | ||
|
||
export function isRangeStartComment(comment: Comment): boolean { | ||
return comment.value.trim() === "prettier-ignore-start"; | ||
} | ||
|
||
export function isRangeEndComment(comment: Comment): boolean { | ||
return comment.value.trim() === "prettier-ignore-end"; | ||
} | ||
|
||
function isRangeUnfinished(range: Range | undefined): boolean { | ||
return range !== undefined && isNaN(range[1]); | ||
} | ||
|
||
function findIgnoredRanges(): [ | ||
(ranges: Ranges, comment: Comment) => Ranges, | ||
Ranges | ||
] { | ||
return [ | ||
(ranges, comment) => { | ||
const lastRangeUnfinished = isRangeUnfinished(ranges[ranges.length - 1]); | ||
if (isRangeStartComment(comment)) { | ||
return lastRangeUnfinished ? ranges : [...ranges, [comment.loc.start.line, NaN]] | ||
} | ||
if (isRangeEndComment(comment)) { | ||
const head = ranges.slice(0, ranges.length - 1); | ||
const tail = ranges[ranges.length - 1]; | ||
return lastRangeUnfinished ? [...head, [tail[0], comment.loc.end.line]] : ranges; | ||
} | ||
return ranges; | ||
}, | ||
[] | ||
]; | ||
} | ||
|
||
function numbersBetween([start, end]: Range): readonly number[] { | ||
const lines: number[] = []; | ||
for (let line = start; line <= end; line += 1) { | ||
lines.push(line); | ||
} | ||
return lines; | ||
} | ||
|
||
/** | ||
* Given a list of comments, checks for ranged ignore comments and returns a | ||
* set of all lines that should be ignored. The start of an ignored range is | ||
* indicated by a `prettier-ignore-start` comment, then end of an ignored range | ||
* by a `prettier-ignore-end` comment. | ||
* | ||
* Note that lines comments (`//`) should be used. This algorithm works with | ||
* block comments (`/* ... * /`) too, but will consider the entire line ignored, | ||
* even when the block comment is at the end of the line. This matches the | ||
* current (2.4.1) prettier behavior for `prettier-ignore` (as prettier does not | ||
* yet support ranged comments in JavaScript). | ||
* @param comments List of comments to process. | ||
* @return The set of all lines that fall inside a range delimited by range | ||
* ignore comments. The lines with the comments themselves are included. | ||
*/ | ||
export const getRangeIgnoredLines: GetRangeIgnoredLines = (comments) => { | ||
const lines = comments | ||
.reduce(...findIgnoredRanges()) | ||
.filter(range => !isRangeUnfinished(range)) | ||
.map(numbersBetween) | ||
.flat(); | ||
return new Set(lines); | ||
} | ||
|
Oops, something went wrong.