1 | "use strict";
|
2 |
|
3 | const parse = require("./parser");
|
4 | const print = require("./printer");
|
5 | const clean = require("./clean");
|
6 | const options = require("./options");
|
7 | const comments = require("./comments");
|
8 | const { join, hardline } = require("prettier").doc.builders;
|
9 | const { hasPragma, insertPragma } = require("./pragma");
|
10 |
|
11 |
|
12 | function createLanguage(linguistData, { extend, override }) {
|
13 | const language = {};
|
14 |
|
15 | for (const key in linguistData) {
|
16 | const newKey = key === "languageId" ? "linguistLanguageId" : key;
|
17 | language[newKey] = linguistData[key];
|
18 | }
|
19 |
|
20 | if (extend) {
|
21 | for (const key in extend) {
|
22 | language[key] = (language[key] || []).concat(extend[key]);
|
23 | }
|
24 | }
|
25 |
|
26 | for (const key in override) {
|
27 | language[key] = override[key];
|
28 | }
|
29 |
|
30 | return language;
|
31 | }
|
32 |
|
33 | const languages = [
|
34 | createLanguage(require("linguist-languages/data/PHP"), {
|
35 | override: {
|
36 | parsers: ["php"],
|
37 | vscodeLanguageIds: ["php"],
|
38 | },
|
39 | }),
|
40 | createLanguage(require("linguist-languages/data/HTML+PHP"), {
|
41 | override: {
|
42 | parsers: ["php"],
|
43 | vscodeLanguageIds: ["php"],
|
44 | },
|
45 | }),
|
46 | ];
|
47 |
|
48 | const loc = (prop) => (node) => {
|
49 | return node.loc && node.loc[prop] && node.loc[prop].offset;
|
50 | };
|
51 |
|
52 | const parsers = {
|
53 | php: {
|
54 | parse,
|
55 | astFormat: "php",
|
56 | locStart: loc("start"),
|
57 | locEnd: loc("end"),
|
58 | hasPragma,
|
59 | },
|
60 | };
|
61 |
|
62 | const printers = {
|
63 | php: {
|
64 | print,
|
65 | insertPragma,
|
66 | massageAstNode: clean,
|
67 | getCommentChildNodes: comments.getCommentChildNodes,
|
68 | canAttachComment: comments.canAttachComment,
|
69 | isBlockComment: comments.isBlockComment,
|
70 | handleComments: {
|
71 | ownLine: comments.handleOwnLineComment,
|
72 | endOfLine: comments.handleEndOfLineComment,
|
73 | remaining: comments.handleRemainingComment,
|
74 | },
|
75 | willPrintOwnComments(path) {
|
76 | const node = path.getValue();
|
77 |
|
78 | return node && node.kind === "noop";
|
79 | },
|
80 | printComment(commentPath) {
|
81 | const comment = commentPath.getValue();
|
82 |
|
83 | switch (comment.kind) {
|
84 | case "commentblock": {
|
85 |
|
86 | if (!comment.value.includes("\n")) {
|
87 | return comment.value;
|
88 | }
|
89 |
|
90 | const lines = comment.value.split(/\r?\n/g);
|
91 |
|
92 | if (
|
93 | lines
|
94 | .slice(1, lines.length - 1)
|
95 | .every((line) => line.trim()[0] === "*")
|
96 | ) {
|
97 | return join(
|
98 | hardline,
|
99 | lines.map(
|
100 | (line, index) =>
|
101 | (index > 0 ? " " : "") +
|
102 | (index < lines.length - 1 ? line.trim() : line.trimLeft())
|
103 | )
|
104 | );
|
105 | }
|
106 |
|
107 |
|
108 | return comment.value;
|
109 | }
|
110 | case "commentline": {
|
111 | return comment.value.trimRight();
|
112 | }
|
113 |
|
114 | default:
|
115 | throw new Error(`Not a comment: ${JSON.stringify(comment)}`);
|
116 | }
|
117 | },
|
118 | hasPrettierIgnore(path) {
|
119 | const node = path.getNode();
|
120 | const isSimpleIgnore = (comment) =>
|
121 | comment.value.includes("prettier-ignore") &&
|
122 | !comment.value.includes("prettier-ignore-start") &&
|
123 | !comment.value.includes("prettier-ignore-end");
|
124 | return (
|
125 | node &&
|
126 | node.comments &&
|
127 | node.comments.length > 0 &&
|
128 | node.comments.some(isSimpleIgnore)
|
129 | );
|
130 | },
|
131 | },
|
132 | };
|
133 |
|
134 | module.exports = {
|
135 | languages,
|
136 | printers,
|
137 | parsers,
|
138 | options,
|
139 | defaultOptions: {
|
140 | tabWidth: 4,
|
141 | },
|
142 | };
|