UNPKG

3.65 kBJavaScriptView Raw
1"use strict";
2
3const parse = require("./parser");
4const print = require("./printer");
5const clean = require("./clean");
6const options = require("./options");
7const comments = require("./comments");
8const { join, hardline } = require("prettier").doc.builders;
9const { hasPragma, insertPragma } = require("./pragma");
10
11// TODO: remove after resolve https://github.com/prettier/prettier/pull/5854
12function 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
33const 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
48const loc = (prop) => (node) => {
49 return node.loc && node.loc[prop] && node.loc[prop].offset;
50};
51
52const parsers = {
53 php: {
54 parse,
55 astFormat: "php",
56 locStart: loc("start"),
57 locEnd: loc("end"),
58 hasPragma,
59 },
60};
61
62const 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 // for now, don't touch single line block comments
86 if (!comment.value.includes("\n")) {
87 return comment.value;
88 }
89
90 const lines = comment.value.split(/\r?\n/g);
91 // if this is a block comment, handle indentation
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 // otherwise we can't be sure about indentation, so just print as is
108 return comment.value;
109 }
110 case "commentline": {
111 return comment.value.trimRight();
112 }
113 /* istanbul ignore next */
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
134module.exports = {
135 languages,
136 printers,
137 parsers,
138 options,
139 defaultOptions: {
140 tabWidth: 4,
141 },
142};