-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Proposal to avoid separating function definitions without an implemen…
…tation (#941) * Proposal for avoiding separation between function definitions without an implementation * compatibility with Prettier 2 * refactor backward compatibility utils * moving isPrettier2 to the backwards compatibility module * reorganising the logic for clarity and to avoid having to check for extra lines added by mistake at the end * to avoid conflicts with #912 we changed isPrettier2 from a function to a constant checked at the loading of the module * Adding documentation * only check for leading comments
- Loading branch information
Showing
10 changed files
with
157 additions
and
184 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { util } from 'prettier'; | ||
import { prettierVersionSatisfies } from './util.js'; | ||
|
||
export const isPrettier2 = prettierVersionSatisfies('^2.3.0'); | ||
|
||
// The functions in this file will never be 100% covered in a single run | ||
// since it depends on the version of Prettier being used. | ||
// Mocking the behaviour will introduce a lot of maintenance in the tests. | ||
|
||
export function isNextLineEmpty(text, startIndex) { | ||
return isPrettier2 | ||
? util.isNextLineEmptyAfterIndex(text, startIndex) | ||
: util.isNextLineEmpty(text, startIndex); // V3 deprecated `isNextLineEmptyAfterIndex` | ||
} | ||
|
||
export function getNextNonSpaceNonCommentCharacterIndex(text, node, locEnd) { | ||
return isPrettier2 | ||
? util.getNextNonSpaceNonCommentCharacterIndex(text, node, locEnd) | ||
: util.getNextNonSpaceNonCommentCharacterIndex(text, locEnd(node)); // V3 signature changed | ||
} | ||
|
||
export function getNextNonSpaceNonCommentCharacter(text, node, locEnd) { | ||
return isPrettier2 | ||
? text.charAt( | ||
util.getNextNonSpaceNonCommentCharacterIndex(text, node, locEnd) | ||
) | ||
: util.getNextNonSpaceNonCommentCharacter(text, locEnd(node)); // V3 exposes this function directly | ||
} | ||
|
||
export function isFirst(path, _, index) { | ||
return isPrettier2 ? index === 0 : path.isFirst; | ||
} | ||
|
||
export function isLast(path, key, index) { | ||
return isPrettier2 | ||
? index === path.getParentNode()[key].length - 1 | ||
: path.isLast; | ||
} | ||
|
||
export function previous(path, key, index) { | ||
return isPrettier2 ? path.getParentNode()[key][index - 1] : path.previous; | ||
} | ||
|
||
export function next(path, key, index) { | ||
return isPrettier2 ? path.getParentNode()[key][index + 1] : path.next; | ||
} |
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
Oops, something went wrong.