Skip to content

Commit

Permalink
to avoid conflicts with #912 we changed isPrettier2 from a function t…
Browse files Browse the repository at this point in the history
…o a constant checked at the loading of the module
  • Loading branch information
Janther committed Oct 31, 2023
1 parent 95b903e commit cdbea85
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
16 changes: 8 additions & 8 deletions src/common/backward-compatibility.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
import { util } from 'prettier';
import { prettierVersionSatisfies } from './util.js';

export const isPrettier2 = () => prettierVersionSatisfies('^2.3.0');
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()
return isPrettier2
? util.isNextLineEmptyAfterIndex(text, startIndex)
: util.isNextLineEmpty(text, startIndex); // V3 deprecated `isNextLineEmptyAfterIndex`
}

export function getNextNonSpaceNonCommentCharacterIndex(text, node, locEnd) {
return isPrettier2()
return isPrettier2
? util.getNextNonSpaceNonCommentCharacterIndex(text, node, locEnd)
: util.getNextNonSpaceNonCommentCharacterIndex(text, locEnd(node)); // V3 signature changed
}

export function getNextNonSpaceNonCommentCharacter(text, node, locEnd) {
return isPrettier2()
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;
return isPrettier2 ? index === 0 : path.isFirst;
}

export function isLast(path, key, index) {
return isPrettier2()
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;
return isPrettier2 ? path.getParentNode()[key][index - 1] : path.previous;
}

export function next(path, key, index) {
return isPrettier2() ? path.getParentNode()[key][index + 1] : path.next;
return isPrettier2 ? path.getParentNode()[key][index + 1] : path.next;
}
2 changes: 1 addition & 1 deletion src/common/printer-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const printComments = (node, path, options, filter = () => true) => {
// since it depends on the version of Prettier being used.
// Mocking the behaviour will introduce a lot of maintenance in the tests.
/* c8 ignore start */
return isPrettier2()
return isPrettier2
? document.parts // Prettier V2
: document; // Prettier V3
/* c8 ignore stop */
Expand Down

0 comments on commit cdbea85

Please sign in to comment.