Skip to content

Commit

Permalink
fix: πŸ› singleQuote option does not work on format API (prettier#2303)
Browse files Browse the repository at this point in the history
* fix: πŸ› singleQuote option does not work on format API

* test: πŸ’ add test for singleQuote option from api call

* refactor: πŸ’‘ add comment

* style: πŸ’„ apply formatter
  • Loading branch information
shufo authored and czosel committed Feb 3, 2024
1 parent 410e292 commit 2dbe364
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 198 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
node-version: [16.x, 18.x, 20.x]
node-version: [18.x, 20.x, 21.x]

env:
ENABLE_COVERAGE: true
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
],
"dependencies": {
"linguist-languages": "^7.27.0",
"mem": "^9.0.2",
"memoize": "^10.0.0",
"php-parser": "^3.1.5"
},
"devDependencies": {
Expand All @@ -34,13 +34,13 @@
"@rollup/plugin-node-resolve": "^15.2.1",
"@rollup/plugin-replace": "^5.0.5",
"@rollup/plugin-terser": "^0.4.3",
"c8": "^8.0.1",
"c8": "^9.1.0",
"cross-env": "^7.0.2",
"eslint": "8.56.0",
"eslint-config-prettier": "9.1.0",
"eslint-plugin-import": "2.29.1",
"eslint-plugin-jest": "27.6.0",
"eslint-plugin-prettier": "5.0.1",
"eslint-plugin-jest": "27.6.3",
"eslint-plugin-prettier": "5.1.3",
"eslint-plugin-prettier-doc": "^1.1.0",
"jest": "29.7.0",
"jest-environment-jsdom": "29.7.0",
Expand Down
7 changes: 7 additions & 0 deletions src/options.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,11 @@ export default {
{ value: "1tbs", description: "Use 1tbs brace style." },
],
},
singleQuote: {
since: "0.0.0",
category: CATEGORY_PHP,
type: "boolean",
default: false,
description: "Use single quotes instead of double quotes.",
},
};
2 changes: 1 addition & 1 deletion src/pragma.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import memoize from "mem";
import memoize from "memoize";
import parse from "./parser.mjs";

const reHasPragma = /@prettier|@format/;
Expand Down
16 changes: 8 additions & 8 deletions src/printer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1018,8 +1018,8 @@ function printLines(path, options, print, childrenAttribute = "children") {
"?>",
]
: isProgramLikeNode(node) && isFirstNode && node.kind !== "namespace"
? ""
: [beforeCloseTagInlineNode, "?>"];
? ""
: [beforeCloseTagInlineNode, "?>"];

//FIXME getNode is used to get ancestors, but it seems this means to get next sibling?
const nextV = path.getNode(index + 1);
Expand All @@ -1034,8 +1034,8 @@ function printLines(path, options, print, childrenAttribute = "children") {
hardline,
]
: isProgramLikeNode(node) && isLastNode
? ""
: [openTag, " "];
? ""
: [openTag, " "];

printed = [beforeInline, printed, afterInline];
}
Expand Down Expand Up @@ -1091,8 +1091,8 @@ function printLines(path, options, print, childrenAttribute = "children") {
: "",
]
: node.comments
? hardline
: "";
? hardline
: "";

parts.push(lineSuffix([beforeCloseTag, "?>"]));
}
Expand Down Expand Up @@ -1711,8 +1711,8 @@ function printNode(path, options, print) {
hardline,
]
: hasDanglingComments(node)
? [line, printDanglingComments(path, options, true), line]
: "",
? [line, printDanglingComments(path, options, true), line]
: "",
"}",
]
: "",
Expand Down
21 changes: 21 additions & 0 deletions tests/single-quote-api/jsfmt.spec.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import prettier from "prettier/standalone";
import * as prettierPluginPhp from "../../src/index.mjs";

// https://github.com/prettier/plugin-php/issues/2302
test(`singleQuote option on format api`, async () => {
const input = `<?php echo link_to_route("frontend.users.user.show", $users["name"], $users['_id']); ?>`;
const result = await prettier.format(input, {
plugins: [prettierPluginPhp],
singleQuote: true,
parser: "php",
});

const expected = `<?php echo link_to_route(
'frontend.users.user.show',
$users['name'],
$users['_id']
); ?>
`;

expect(result).toEqual(expected);
});
4 changes: 2 additions & 2 deletions tests_config/run_spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ function printOptions(options) {
return value === Infinity
? "Infinity"
: Array.isArray(value)
? `[${value.map((v) => JSON.stringify(v)).join(", ")}]`
: JSON.stringify(value);
? `[${value.map((v) => JSON.stringify(v)).join(", ")}]`
: JSON.stringify(value);
}
}

Expand Down
Loading

0 comments on commit 2dbe364

Please sign in to comment.