@prettier/plugin-ruby
Advanced tools
Comparing version 0.9.0 to 0.9.1
@@ -17,2 +17,3 @@ { | ||
"object-curly-newline": "off", | ||
"prefer-spread": "off", | ||
"quotes": ["error", "double"] | ||
@@ -19,0 +20,0 @@ }, |
@@ -9,2 +9,7 @@ # Changelog | ||
## [0.9.1] - 2019-03-24 | ||
### Changed | ||
- Better support string quotes by favoring what the user chose if the string contains escape patterns. | ||
- Better support heredocs within method calls. | ||
## [0.9.0] - 2019-03-18 | ||
@@ -214,3 +219,4 @@ ### Added | ||
[Unreleased]: https://github.com/prettier/plugin-ruby/compare/v0.9.0...HEAD | ||
[Unreleased]: https://github.com/prettier/plugin-ruby/compare/v0.9.1...HEAD | ||
[0.9.1]: https://github.com/prettier/plugin-ruby/compare/v0.9.0...v0.9.1 | ||
[0.9.0]: https://github.com/prettier/plugin-ruby/compare/v0.8.0...v0.9.0 | ||
@@ -217,0 +223,0 @@ [0.8.0]: https://github.com/prettier/plugin-ruby/compare/v0.7.0...v0.8.0 |
{ | ||
"name": "@prettier/plugin-ruby", | ||
"version": "0.9.0", | ||
"version": "0.9.1", | ||
"description": "prettier plugin for the Ruby programming language", | ||
@@ -5,0 +5,0 @@ "main": "src/ruby.js", |
const { align, breakParent, concat, dedent, group, hardline, ifBreak, indent, join, line, literalline, markAsRoot, softline, trim } = require("prettier").doc.builders; | ||
const { removeLines } = require("prettier").doc.utils; | ||
const { concatBody, docLength, empty, first, literal, makeCall, makeList, prefix, printComments, skipAssignIndent } = require("./utils"); | ||
const { concatBody, empty, first, literal, makeArgs, makeCall, makeList, prefix, printComments, skipAssignIndent } = require("./utils"); | ||
@@ -14,3 +14,3 @@ const nodes = { | ||
}, | ||
arg_paren: (path, { addTrailingCommas }, print) => { | ||
arg_paren: (path, opts, print) => { | ||
if (path.getValue().body[0] === null) { | ||
@@ -20,10 +20,17 @@ return ""; | ||
const args = path.getValue().body[0]; | ||
const hasBlock = args.type === "args_add_block" && args.body[1]; | ||
const { addTrailingCommas } = opts; | ||
const { args, heredocs } = makeArgs(path, opts, print, 0); | ||
return group(concat([ | ||
const argsNode = path.getValue().body[0]; | ||
const hasBlock = argsNode.type === "args_add_block" && argsNode.body[1]; | ||
if (heredocs.length > 1) { | ||
return concat(["(", join(", ", args), ")"].concat(heredocs)); | ||
} | ||
const parenDoc = group(concat([ | ||
"(", | ||
indent(concat([ | ||
softline, | ||
join(concat([",", line]), path.call(print, "body", 0)), | ||
join(concat([",", line]), args), | ||
addTrailingCommas && !hasBlock ? ifBreak(",", "") : "" | ||
@@ -33,2 +40,8 @@ ])), | ||
])); | ||
if (heredocs.length === 1) { | ||
return group(concat([parenDoc].concat(heredocs))); | ||
} | ||
return parenDoc; | ||
}, | ||
@@ -221,31 +234,2 @@ args: makeList, | ||
}, | ||
command: (path, opts, print) => { | ||
const command = path.call(print, "body", 0); | ||
const args = join(concat([",", line]), path.call(print, "body", 1)); | ||
// Hate, hate, hate this but can't figure out how to fix it. | ||
return group(ifBreak( | ||
concat([command, " ", align(command.length + 1, args)]), | ||
concat([command, " ", args]) | ||
)); | ||
}, | ||
command_call: (path, opts, print) => { | ||
const parts = [ | ||
path.call(print, "body", 0), | ||
makeCall(path, opts, print), | ||
path.call(print, "body", 2) | ||
]; | ||
if (!path.getValue().body[3]) { | ||
return concat(parts); | ||
} | ||
parts.push(" "); | ||
const args = join(concat([",", line]), path.call(print, "body", 3)); | ||
return group(ifBreak( | ||
concat(parts.concat([align(docLength(concat(parts)), args)])), | ||
concat(parts.concat([args])) | ||
)); | ||
}, | ||
const_path_field: (path, opts, print) => join("::", path.map(print, "body")), | ||
@@ -625,2 +609,3 @@ const_path_ref: (path, opts, print) => join("::", path.map(print, "body")), | ||
require("./nodes/blocks"), | ||
require("./nodes/commands"), | ||
require("./nodes/conditionals"), | ||
@@ -627,0 +612,0 @@ require("./nodes/hooks"), |
@@ -5,8 +5,27 @@ const { concat, group, hardline, indent, join, literalline, softline } = require("prettier").doc.builders; | ||
const isSingleQuotable = stringPart => ( | ||
stringPart.type === "@tstring_content" | ||
&& !stringPart.body.includes("'") | ||
&& !escapePattern.test(stringPart.body) | ||
); | ||
// If there is some part of this string that matches an escape sequence or that | ||
// contains the interpolation pattern ("#{"), then we are locked into whichever | ||
// quote the user chose. (If they chose single quotes, then double quoting | ||
// would activate the escape sequence, and if they chose double quotes, then | ||
// single quotes would deactivate it.) | ||
const isQuoteLocked = string => string.body.some(part => ( | ||
part.type === "@tstring_content" && ( | ||
escapePattern.test(part.body) || part.body.includes("#{") | ||
) | ||
)); | ||
// A string is considered to be able to use single quotes if it contains only | ||
// plain string content and that content does not contain a single quote. | ||
const isSingleQuotable = string => string.body.every(part => ( | ||
part.type === "@tstring_content" && !part.body.includes("'") | ||
)); | ||
const getStringQuote = (string, preferSingleQuotes) => { | ||
if (isQuoteLocked(string)) { | ||
return string.quote; | ||
} | ||
return preferSingleQuotes && isSingleQuotable(string) ? "'" : "\""; | ||
}; | ||
const quotePattern = new RegExp("\\\\([\\s\\S])|(['\"])", "g"); | ||
@@ -90,11 +109,5 @@ | ||
// Determine the quote to use. If we prefer single quotes and there are no | ||
// embedded expressions and there aren't any single quotes in the string | ||
// already, we can safely switch to single quotes. | ||
let quote = "\""; | ||
if (preferSingleQuotes && string.body.every(isSingleQuotable)) { | ||
quote = "'"; | ||
} | ||
const quote = getStringQuote(string, preferSingleQuotes); | ||
const parts = []; | ||
const parts = []; | ||
string.body.forEach((part, index) => { | ||
@@ -101,0 +114,0 @@ if (part.type === "@tstring_content") { |
@@ -1,2 +0,2 @@ | ||
const { breakParent, concat, hardline, lineSuffix } = require("prettier").doc.builders; | ||
const { breakParent, concat, hardline, lineSuffix, literalline } = require("prettier").doc.builders; | ||
@@ -23,2 +23,36 @@ const concatBody = (path, opts, print) => concat(path.map(print, "body")); | ||
const makeArgs = (path, opts, print, argsIndex) => { | ||
let argNodes = path.getValue().body[argsIndex]; | ||
const argPattern = [print, "body", argsIndex, "body"]; | ||
if (argNodes.type === "args_add_block") { | ||
[argNodes] = argNodes.body; | ||
argPattern.push(0, "body"); | ||
} | ||
const args = path.call(print, "body", argsIndex); | ||
const heredocs = []; | ||
argNodes.body.forEach((argNode, index) => { | ||
let pattern; | ||
let heredoc; | ||
if (argNode.type === "heredoc") { | ||
pattern = [index, "body"]; | ||
heredoc = argNode; | ||
} else if (argNode.type === "string_literal" && argNode.body[0].type === "heredoc") { | ||
pattern = [index, "body", 0, "body"]; | ||
[heredoc] = argNode.body; | ||
} else { | ||
return; | ||
} | ||
const content = path.map.apply(path, argPattern.slice().concat(pattern)); | ||
heredocs.push(concat([literalline].concat(content).concat([heredoc.ending]))); | ||
args[index] = heredoc.beging; | ||
}); | ||
return { args, heredocs }; | ||
}; | ||
const makeCall = (path, opts, print) => { | ||
@@ -82,2 +116,3 @@ const operation = path.getValue().body[1]; | ||
literal, | ||
makeArgs, | ||
makeCall, | ||
@@ -84,0 +119,0 @@ makeList, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
132270
39
1397
169459