Skip to content

Commit 9917045

Browse files
authored
fix: compatibility with prettier 2.0 (#1359)
1 parent d6cb8b4 commit 9917045

19 files changed

Lines changed: 824 additions & 1023 deletions

File tree

build/rollup.config.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,28 @@ export default {
2020
name: "prettierPlugins.php",
2121
exports: "named",
2222
globals: {
23-
prettier: "prettier"
23+
prettier: "prettier",
2424
},
2525
paths: {
26-
prettier: "prettier/standalone"
27-
}
26+
prettier: "prettier/standalone",
27+
},
2828
},
2929
external: ["prettier"],
3030
plugins: [
3131
nodeResolve({
32-
preferBuiltins: true
32+
preferBuiltins: true,
3333
}),
3434
commonjs(),
3535
alias({
3636
entries: [
37-
{ find: "assert", replacement: resolve(BUILD_DIR, "shims/assert.js") }
38-
]
37+
{ find: "assert", replacement: resolve(BUILD_DIR, "shims/assert.js") },
38+
],
3939
}),
4040
inject({
41-
Buffer: resolve(BUILD_DIR, "shims/buffer.js")
41+
Buffer: resolve(BUILD_DIR, "shims/buffer.js"),
4242
}),
4343
replace({
44-
"process.arch": JSON.stringify("x32")
44+
"process.arch": JSON.stringify("x32"),
4545
}),
4646
json(),
4747
babel({
@@ -53,11 +53,11 @@ export default {
5353
require.resolve("@babel/preset-env"),
5454
{
5555
targets: { browsers: [">0.25%", "not ie 11", "not op_mini all"] },
56-
modules: false
57-
}
58-
]
59-
]
56+
modules: false,
57+
},
58+
],
59+
],
6060
}),
61-
terser()
62-
]
61+
terser(),
62+
],
6363
};

build/shims/assert.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
"use strict";
22
function assert() {}
3-
assert.strictEqual = function() {};
3+
assert.strictEqual = function () {};
44
module.exports = assert;

build/shims/buffer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22
const Buffer = {};
3-
Buffer.isBuffer = function() {
3+
Buffer.isBuffer = function () {
44
return false;
55
};
66
module.exports = Buffer;

jest.config.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module.exports = {
77
collectCoverageFrom: [
88
"<rootDir>/src/**/*.js",
99
"!<rootDir>/node_modules/",
10-
"!<rootDir>/tests_config/"
10+
"!<rootDir>/tests_config/",
1111
],
1212
projects: [
1313
{
@@ -17,8 +17,8 @@ module.exports = {
1717
snapshotSerializers: ["jest-snapshot-serializer-raw"],
1818
testEnvironment: "node",
1919
globals: {
20-
STANDALONE: false
21-
}
20+
STANDALONE: false,
21+
},
2222
},
2323
...(/^true$/i.test(process.env.RUN_STANDALONE_TESTS)
2424
? [
@@ -29,16 +29,19 @@ module.exports = {
2929
snapshotSerializers: ["jest-snapshot-serializer-raw"],
3030
testEnvironment: "jsdom",
3131
globals: {
32-
STANDALONE: true
33-
}
34-
}
32+
STANDALONE: true,
33+
},
34+
},
3535
]
3636
: []),
3737
{
3838
runner: "jest-runner-eslint",
3939
displayName: "lint",
4040
testMatch: ["<rootDir>/**/*.js"],
41-
testPathIgnorePatterns: ["<rootDir>/node_modules/", "<rootDir>/coverage/"]
42-
}
43-
]
41+
testPathIgnorePatterns: [
42+
"<rootDir>/node_modules/",
43+
"<rootDir>/coverage/",
44+
],
45+
},
46+
],
4447
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"jest": "25.2.3",
2828
"jest-runner-eslint": "0.7.6",
2929
"jest-snapshot-serializer-raw": "^1.1.0",
30-
"prettier": "^1.19.1",
30+
"prettier": "^2.0.0",
3131
"rollup": "^1.1.2",
3232
"rollup-plugin-alias": "^2.0.0",
3333
"rollup-plugin-babel": "^4.3.2",

src/clean.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ function clean(node, newObj) {
2323
"end",
2424
"tokens",
2525
"errors",
26-
"extra"
27-
].forEach(name => {
26+
"extra",
27+
].forEach((name) => {
2828
delete newObj[name];
2929
});
3030

@@ -77,7 +77,7 @@ function clean(node, newObj) {
7777
if (node.body && node.body.kind !== "block") {
7878
newObj.body = {
7979
kind: "block",
80-
children: [newObj.body]
80+
children: [newObj.body],
8181
};
8282
} else {
8383
newObj.body = newObj.body ? newObj.body : null;
@@ -86,7 +86,7 @@ function clean(node, newObj) {
8686
if (node.alternate && node.alternate.kind !== "block") {
8787
newObj.alternate = {
8888
kind: "block",
89-
children: [newObj.alternate]
89+
children: [newObj.alternate],
9090
};
9191
} else {
9292
newObj.alternate = newObj.alternate ? newObj.alternate : null;

src/comments.js

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@ const {
44
addLeadingComment,
55
addDanglingComment,
66
addTrailingComment,
7-
getNextNonSpaceNonCommentCharacterIndex,
8-
isNextLineEmpty,
97
skipNewline,
10-
isPreviousLineEmpty,
118
hasNewline,
12-
hasNewlineInRange
9+
hasNewlineInRange,
1310
} = require("prettier").util;
1411
const {
1512
concat,
@@ -18,9 +15,14 @@ const {
1815
hardline,
1916
cursor,
2017
lineSuffix,
21-
breakParent
18+
breakParent,
2219
} = require("prettier").doc.builders;
23-
const { isLookupNode } = require("./util");
20+
const {
21+
getNextNonSpaceNonCommentCharacterIndex,
22+
isNextLineEmpty,
23+
isPreviousLineEmpty,
24+
isLookupNode,
25+
} = require("./util");
2426

2527
/*
2628
Comment functions are meant to inspect various edge cases using given comment nodes,
@@ -461,7 +463,7 @@ function handleClassComments(enclosingNode, followingNode, comment) {
461463
}
462464
} else {
463465
if (
464-
enclosingNode.extends.some(extendsNode => {
466+
enclosingNode.extends.some((extendsNode) => {
465467
if (followingNode && followingNode === extendsNode) {
466468
addDanglingComment(followingNode, comment);
467469
return true;
@@ -477,7 +479,7 @@ function handleClassComments(enclosingNode, followingNode, comment) {
477479
// them as dangling comments and handle them in the printer
478480
if (followingNode && enclosingNode.implements) {
479481
if (
480-
enclosingNode.implements.some(implementsNode => {
482+
enclosingNode.implements.some((implementsNode) => {
481483
if (followingNode && followingNode === implementsNode) {
482484
addDanglingComment(followingNode, comment);
483485
return true;
@@ -830,7 +832,7 @@ function printDanglingComments(path, options, sameIndent, filter) {
830832
return "";
831833
}
832834

833-
path.each(commentPath => {
835+
path.each((commentPath) => {
834836
const comment = commentPath.getValue();
835837
if (
836838
comment &&
@@ -853,18 +855,18 @@ function printDanglingComments(path, options, sameIndent, filter) {
853855
}
854856

855857
function hasLeadingComment(node) {
856-
return node.comments && node.comments.some(comment => comment.leading);
858+
return node.comments && node.comments.some((comment) => comment.leading);
857859
}
858860

859861
function hasTrailingComment(node) {
860-
return node.comments && node.comments.some(comment => comment.trailing);
862+
return node.comments && node.comments.some((comment) => comment.trailing);
861863
}
862864

863865
function hasLeadingOwnLineComment(text, node, options) {
864866
return (
865867
node.comments &&
866868
node.comments.some(
867-
comment => comment.leading && hasNewline(text, options.locEnd(comment))
869+
(comment) => comment.leading && hasNewline(text, options.locEnd(comment))
868870
)
869871
);
870872
}
@@ -897,10 +899,10 @@ function getCommentChildNodes(node) {
897899
return [];
898900
}
899901

900-
const getChildNodes = node =>
902+
const getChildNodes = (node) =>
901903
Object.keys(node)
902904
.filter(
903-
n =>
905+
(n) =>
904906
n !== "kind" &&
905907
n !== "loc" &&
906908
n !== "errors" &&
@@ -911,7 +913,7 @@ function getCommentChildNodes(node) {
911913
n !== "precedingNode" &&
912914
n !== "followingNode"
913915
)
914-
.map(n => node[n]);
916+
.map((n) => node[n]);
915917

916918
return getChildNodes(node);
917919
}
@@ -948,7 +950,9 @@ function printLeadingComment(commentPath, print, options) {
948950
if (isBlock) {
949951
return concat([
950952
contents,
951-
hasNewline(options.originalText, options.locEnd(comment)) ? hardline : " "
953+
hasNewline(options.originalText, options.locEnd(comment))
954+
? hardline
955+
: " ",
952956
]);
953957
}
954958

@@ -966,7 +970,7 @@ function printTrailingComment(commentPath, print, options) {
966970

967971
if (
968972
hasNewline(options.originalText, options.locStart(comment), {
969-
backwards: true
973+
backwards: true,
970974
})
971975
) {
972976
// This allows comments at the end of nested structures:
@@ -997,7 +1001,7 @@ function printTrailingComment(commentPath, print, options) {
9971001

9981002
return concat([
9991003
lineSuffix(concat([" ", contents])),
1000-
!isBlock ? breakParent : ""
1004+
!isBlock ? breakParent : "",
10011005
]);
10021006
}
10031007

@@ -1013,7 +1017,7 @@ function printAllComments(path, print, options, needsSemi) {
10131017
const leadingParts = [];
10141018
const trailingParts = [needsSemi ? ";" : "", printed];
10151019

1016-
path.each(commentPath => {
1020+
path.each((commentPath) => {
10171021
const comment = commentPath.getValue();
10181022
const { leading, trailing } = comment;
10191023

@@ -1052,5 +1056,5 @@ module.exports = {
10521056
hasTrailingComment,
10531057
hasLeadingOwnLineComment,
10541058
printComments,
1055-
printAllComments
1059+
printAllComments,
10561060
};

src/index.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,18 @@ const languages = [
3434
createLanguage(require("linguist-languages/data/PHP"), {
3535
override: {
3636
parsers: ["php"],
37-
vscodeLanguageIds: ["php"]
38-
}
37+
vscodeLanguageIds: ["php"],
38+
},
3939
}),
4040
createLanguage(require("linguist-languages/data/HTML+PHP"), {
4141
override: {
4242
parsers: ["php"],
43-
vscodeLanguageIds: ["php"]
44-
}
45-
})
43+
vscodeLanguageIds: ["php"],
44+
},
45+
}),
4646
];
4747

48-
const loc = prop => node => {
48+
const loc = (prop) => (node) => {
4949
return node.loc && node.loc[prop] && node.loc[prop].offset;
5050
};
5151

@@ -55,8 +55,8 @@ const parsers = {
5555
astFormat: "php",
5656
locStart: loc("start"),
5757
locEnd: loc("end"),
58-
hasPragma
59-
}
58+
hasPragma,
59+
},
6060
};
6161

6262
const printers = {
@@ -70,7 +70,7 @@ const printers = {
7070
handleComments: {
7171
ownLine: comments.handleOwnLineComment,
7272
endOfLine: comments.handleEndOfLineComment,
73-
remaining: comments.handleRemainingComment
73+
remaining: comments.handleRemainingComment,
7474
},
7575
willPrintOwnComments(path) {
7676
const node = path.getValue();
@@ -92,7 +92,7 @@ const printers = {
9292
if (
9393
lines
9494
.slice(1, lines.length - 1)
95-
.every(line => line.trim()[0] === "*")
95+
.every((line) => line.trim()[0] === "*")
9696
) {
9797
return join(
9898
hardline,
@@ -117,7 +117,7 @@ const printers = {
117117
},
118118
hasPrettierIgnore(path) {
119119
const node = path.getNode();
120-
const isSimpleIgnore = comment =>
120+
const isSimpleIgnore = (comment) =>
121121
comment.value.includes("prettier-ignore") &&
122122
!comment.value.includes("prettier-ignore-start") &&
123123
!comment.value.includes("prettier-ignore-end");
@@ -127,8 +127,8 @@ const printers = {
127127
node.comments.length > 0 &&
128128
node.comments.some(isSimpleIgnore)
129129
);
130-
}
131-
}
130+
},
131+
},
132132
};
133133

134134
module.exports = {
@@ -137,6 +137,6 @@ module.exports = {
137137
parsers,
138138
options,
139139
defaultOptions: {
140-
tabWidth: 4
141-
}
140+
tabWidth: 4,
141+
},
142142
};

src/needs-parens.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function needsParens(path) {
2727
"include",
2828
"print",
2929
"return",
30-
"echo"
30+
"echo",
3131
].includes(parent.kind)
3232
) {
3333
return false;

0 commit comments

Comments
 (0)