Skip to content

Commit

Permalink
Throwing if the prettier version is not >=2.3.0 (#552)
Browse files Browse the repository at this point in the history
* Throwing if the prettier version is not >=2.3.0

* grammar
  • Loading branch information
Janther authored Jul 9, 2021
1 parent e02a5af commit e5e7e52
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/printer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
const prettier = require('prettier');
const semver = require('semver');
const nodes = require('./nodes');
const { hasNodeIgnoreComment } = require('./prettier-comments/common/util');
const ignoreComments = require('./comments/ignore');

let checked = false;

function prettierVersionCheck() {
if (checked) return;
if (!semver.satisfies(prettier.version, '>=2.3.0')) {
throw new Error(
'The version of prettier in your node-modules does not satisfy the required ">=2.3.0" constraint. Please update the version of Prettier.'
);
}
checked = true;
}

function genericPrint(path, options, print) {
prettierVersionCheck();

const node = path.getValue();
if (node === null) {
return '';
Expand Down
2 changes: 1 addition & 1 deletion tests/config/format-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ function runSpec(fixtures, parsers, options) {

describe(title, () => {
const formatOptions = {
plugins: [path.dirname(path.join(__dirname, ".."))],
plugins: [path.join(__dirname, "../..")],
printWidth: 80,
...options,
filepath: filename,
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/prettier-version.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const path = require('path');

jest.mock('prettier', () => {
// Require the original module to not be mocked...
const originalModule = jest.requireActual('prettier');

return {
...originalModule,
version: '2.2.1'
};
});

const prettier = require('prettier');

test('should throw if the installed version of prettier is less than v2.3.0', () => {
const data = 'contract CheckPrettierVersion {}';

const options = {
plugins: [path.join(__dirname, '../..')],
parser: 'solidity-parse'
};

expect(() => {
prettier.format(data, options);
}).toThrow('>=2.3.0');
});

0 comments on commit e5e7e52

Please sign in to comment.