Skip to content

Commit

Permalink
Print with new prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Mar 23, 2020
1 parent 56546b9 commit 4d1226a
Show file tree
Hide file tree
Showing 33 changed files with 82 additions and 92 deletions.
5 changes: 1 addition & 4 deletions bin/doc
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ const prettier = require("prettier");

const code = fs.existsSync(process.argv[2])
? fs.readFileSync(process.argv[2], "utf-8")
: process.argv
.slice(2)
.join(" ")
.replace(/\\n/g, "\n");
: process.argv.slice(2).join(" ").replace(/\\n/g, "\n");

const doc = prettier.__debug.printToDoc(code, {
parser: "ruby",
Expand Down
5 changes: 1 addition & 4 deletions bin/print
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ const prettier = require("prettier");

const code = fs.existsSync(process.argv[2])
? fs.readFileSync(process.argv[2], "utf-8")
: process.argv
.slice(2)
.join(" ")
.replace(/\\n/g, "\n");
: process.argv.slice(2).join(" ").replace(/\\n/g, "\n");

console.log(prettier.format(code, { parser: "ruby", plugins: ["."] }));
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,8 @@
"./test/js/setupTests.js"
],
"testRegex": ".test.js$"
},
"prettier": {
"trailingComma": "none"
}
}
6 changes: 3 additions & 3 deletions src/haml.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ const parse = require("./haml/parse");
const print = require("./haml/print");

const pragmaPattern = /^\s*-#\s*@(prettier|format)/;
const hasPragma = text => pragmaPattern.test(text);
const hasPragma = (text) => pragmaPattern.test(text);

// These functions are just placeholders until we can actually perform this
// properly. The functions are necessary otherwise the format with cursor
// functions break.
const locStart = _node => 0;
const locEnd = _node => 0;
const locStart = (_node) => 0;
const locEnd = (_node) => 0;

module.exports = {
embed,
Expand Down
4 changes: 2 additions & 2 deletions src/haml/embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const parsers = {
scss: "scss"
};

const replaceNewlines = doc =>
mapDoc(doc, currentDoc =>
const replaceNewlines = (doc) =>
mapDoc(doc, (currentDoc) =>
typeof currentDoc === "string" && currentDoc.includes("\n")
? concat(
currentDoc
Expand Down
12 changes: 7 additions & 5 deletions src/haml/nodes/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ const getDynamicAttributes = (header, attributes) => {
const pairs = attributes
.slice(1, -2)
.split(",")
.map(pair => pair.slice(1).split('" => '));
.map((pair) => pair.slice(1).split('" => '));
const parts = [concat([pairs[0][0], "=", pairs[0][1]])];

pairs.slice(1).forEach(pair => {
pairs.slice(1).forEach((pair) => {
parts.push(line, concat([pair[0], "=", pair[1]]));
});

Expand Down Expand Up @@ -49,13 +49,13 @@ const getHashLabel = (key, value, opts) => {

const getStaticAttributes = (header, attributes, opts) => {
const keys = Object.keys(attributes).filter(
name => !["class", "id"].includes(name)
(name) => !["class", "id"].includes(name)
);

const getKeyValuePair = opts.preferHashLabels ? getHashLabel : getHashRocket;
const parts = [getKeyValuePair(keys[0], attributes[keys[0]], opts)];

keys.slice(1).forEach(key => {
keys.slice(1).forEach((key) => {
parts.push(",", line, getKeyValuePair(key, attributes[key], opts));
});

Expand Down Expand Up @@ -84,7 +84,9 @@ const getHeader = (value, opts) => {
);
}

if (Object.keys(attributes).some(name => name !== "class" && name !== "id")) {
if (
Object.keys(attributes).some((name) => name !== "class" && name !== "id")
) {
parts.push(getStaticAttributes(parts.join("").length, attributes, opts));
}

Expand Down
4 changes: 2 additions & 2 deletions src/nodes/alias.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { concat, join } = require("../prettier");

const usingSymbols = path => {
const [left, right] = path.getValue().body.map(node => node.body[0].type);
const usingSymbols = (path) => {
const [left, right] = path.getValue().body.map((node) => node.body[0].type);
return left === "symbol" && right === "symbol";
};

Expand Down
2 changes: 1 addition & 1 deletion src/nodes/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ module.exports = {
// method_add_block node, and if they are that that node has a block
// associated with it. If it does, we're going to attempt to transform it
// into the to_proc shorthand and add it to the list of arguments.
[1, 2, 3].find(parent => {
[1, 2, 3].find((parent) => {
const parentNode = path.getParentNode(parent);
blockNode =
parentNode &&
Expand Down
12 changes: 6 additions & 6 deletions src/nodes/arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ const {
softline
} = require("../prettier");

const isStringArray = args =>
const isStringArray = (args) =>
args.body.every(
arg =>
(arg) =>
arg.type === "string_literal" &&
arg.body[0].body.length === 1 &&
arg.body[0].body[0].type === "@tstring_content" &&
!arg.body[0].body[0].body.includes(" ")
);

const isSymbolArray = args =>
args.body.every(arg => arg.type === "symbol_literal");
const isSymbolArray = (args) =>
args.body.every((arg) => arg.type === "symbol_literal");

const makeArray = start => (path, opts, print) =>
const makeArray = (start) => (path, opts, print) =>
[start].concat(path.map(print, "body"));

const getSpecialArrayParts = (path, print, args) =>
Expand All @@ -44,7 +44,7 @@ const printAref = (path, opts, print) =>
])
);

const printSpecialArray = parts =>
const printSpecialArray = (parts) =>
group(
concat([
parts[0],
Expand Down
4 changes: 2 additions & 2 deletions src/nodes/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const {
} = require("../prettier");
const { empty, hasAncestor } = require("../utils");

const printBlock = braces => (path, opts, print) => {
const printBlock = (braces) => (path, opts, print) => {
const [variables, statements] = path.getValue().body;
const stmts =
statements.type === "stmts" ? statements.body : statements.body[0].body;
Expand Down Expand Up @@ -38,7 +38,7 @@ const printBlock = braces => (path, opts, print) => {
// comment.
if (
stmts.length > 1 &&
stmts.filter(stmt => stmt.type !== "@comment").length === 1
stmts.filter((stmt) => stmt.type !== "@comment").length === 1
) {
return concat([breakParent, doBlock]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/case.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ module.exports = {

// It's possible in a when to just have empty void statements, in which case
// we would skip adding the body.
if (!stmts.parts.every(part => !part)) {
if (!stmts.parts.every((part) => !part)) {
parts.push(indent(concat([hardline, stmts])));
}

Expand Down
4 changes: 2 additions & 2 deletions src/nodes/commands.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { align, concat, group, ifBreak, join, line } = require("../prettier");
const { docLength, makeArgs, makeCall } = require("../utils");

const hasDef = node =>
const hasDef = (node) =>
node.body[1].type === "args_add_block" &&
node.body[1].body[0].type === "args" &&
node.body[1].body[0].body[0] &&
Expand All @@ -20,7 +20,7 @@ const hasDef = node =>
//
// In this case the arguments are aligned to the left side as opposed to being
// aligned with the `receive` call.
const skipArgsAlign = path =>
const skipArgsAlign = (path) =>
["to", "not_to"].includes(path.getValue().body[2].body);

module.exports = {
Expand Down
13 changes: 7 additions & 6 deletions src/nodes/conditionals.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const printWithAddition = (keyword, path, print, { breaking = false } = {}) =>
// For the unary `not` operator, we need to explicitly add parentheses to it in
// order for it to be valid from within a ternary. Otherwise if the clause of
// the ternary isn't a unary `not`, we can just pass it along.
const printTernaryClause = clause => {
const printTernaryClause = (clause) => {
if (clause.type === "concat") {
const [part] = clause.parts;

Expand Down Expand Up @@ -78,7 +78,7 @@ const printTernary = (path, _opts, print) => {
// Prints an `if_mod` or `unless_mod` node. Because it was previously in the
// modifier form, we're guaranteed to not have an additional node, so we can
// just work with the predicate and the body.
const printSingle = keyword => (path, { inlineConditionals }, print) => {
const printSingle = (keyword) => (path, { inlineConditionals }, print) => {
const multiline = concat([
`${keyword} `,
align(keyword.length + 1, path.call(print, "body", 0)),
Expand All @@ -88,7 +88,8 @@ const printSingle = keyword => (path, { inlineConditionals }, print) => {

const [_predicate, stmts] = path.getValue().body;
const hasComments =
stmts.type === "stmts" && stmts.body.some(stmt => stmt.type === "@comment");
stmts.type === "stmts" &&
stmts.body.some((stmt) => stmt.type === "@comment");

if (!inlineConditionals || hasComments) {
return multiline;
Expand Down Expand Up @@ -152,7 +153,7 @@ const noTernary = [
// Certain expressions cannot be reduced to a ternary without adding parens
// around them. In this case we say they cannot be ternaried and default instead
// to breaking them into multiple lines.
const canTernaryStmts = stmts => {
const canTernaryStmts = (stmts) => {
if (stmts.body.length !== 1) {
return false;
}
Expand All @@ -175,7 +176,7 @@ const canTernaryStmts = stmts => {
// is of the "else" type. Both the body of the main node and the body of the
// additional node must have only one statement, and that statement list must
// pass the `canTernaryStmts` check.
const canTernary = path => {
const canTernary = (path) => {
const [_pred, stmts, addition] = path.getValue().body;

return (
Expand All @@ -186,7 +187,7 @@ const canTernary = path => {
};

// A normalized print function for both `if` and `unless` nodes.
const printConditional = keyword => (path, { inlineConditionals }, print) => {
const printConditional = (keyword) => (path, { inlineConditionals }, print) => {
if (canTernary(path)) {
let ternaryParts = [path.call(print, "body", 0), " ? "].concat(
printTernaryClauses(
Expand Down
4 changes: 2 additions & 2 deletions src/nodes/hashes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const { prefix, skipAssignIndent } = require("../utils");
const nodeDive = (node, steps) => {
let current = node;

steps.forEach(step => {
steps.forEach((step) => {
current = current[step];
});

Expand All @@ -29,7 +29,7 @@ const nodeDive = (node, steps) => {
//
// This function represents that check, as it determines if it can convert the
// symbol node into a hash label.
const isValidHashLabel = symbolLiteral => {
const isValidHashLabel = (symbolLiteral) => {
const label = symbolLiteral.body[0].body[0].body;
return label.match(/^[_A-Za-z]/) && !label.endsWith("=");
};
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/hooks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { concat, group, indent, line } = require("../prettier");

const printHook = name => (path, opts, print) =>
const printHook = (name) => (path, opts, print) =>
group(
concat([
`${name} {`,
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/lambdas.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = {
paramsConcat = path.call(print, "body", 0, "body", 0);
}

const noParams = params.body.every(type => !type);
const noParams = params.body.every((type) => !type);
const inlineLambda = concat([
"->",
noParams ? "" : concat(["(", paramsConcat, ")"]),
Expand Down
4 changes: 2 additions & 2 deletions src/nodes/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const {
} = require("../prettier");
const { first, literal } = require("../utils");

const printMethod = offset => (path, opts, print) => {
const printMethod = (offset) => (path, opts, print) => {
const [_name, params, body] = path.getValue().body.slice(offset);
const declaration = ["def "];

Expand All @@ -21,7 +21,7 @@ const printMethod = offset => (path, opts, print) => {

// In case there are no parens but there are arguments
const parens =
params.type === "params" && params.body.some(paramType => paramType);
params.type === "params" && params.body.some((paramType) => paramType);

declaration.push(
path.call(print, "body", offset),
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/params.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { concat, group, join, line } = require("../prettier");
const { literal } = require("../utils");

const printGenericRestParam = symbol => (path, opts, print) =>
const printGenericRestParam = (symbol) => (path, opts, print) =>
path.getValue().body[0]
? concat([symbol, path.call(print, "body", 0)])
: symbol;
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/regexp.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = {
const [contents, ending] = path.map(print, "body");

const useBraces = contents.some(
content => typeof content === "string" && content.includes("/")
(content) => typeof content === "string" && content.includes("/")
);
const parts = [useBraces ? "%r{" : "/"]
.concat(contents)
Expand Down
10 changes: 5 additions & 5 deletions src/nodes/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ const { concatBody, empty, makeList, prefix, surround } = require("../utils");
// 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 =>
const isQuoteLocked = (string) =>
string.body.some(
part =>
(part) =>
part.type === "@tstring_content" &&
(part.body.includes("#{") || 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 =>
const isSingleQuotable = (string) =>
string.body.every(
part => part.type === "@tstring_content" && !part.body.includes("'")
(part) => part.type === "@tstring_content" && !part.body.includes("'")
);

const quotePattern = new RegExp("\\\\([\\s\\S])|(['\"])", "g");
Expand Down Expand Up @@ -61,7 +61,7 @@ const quotePairs = {
"<": ">"
};

const getClosingQuote = quote => {
const getClosingQuote = (quote) => {
if (!quote.startsWith("%")) {
return quote;
}
Expand Down
6 changes: 3 additions & 3 deletions src/ruby.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ const print = require("./print");
const haml = require("./haml");

const pragmaPattern = /#\s*@(prettier|format)/;
const hasPragma = text => pragmaPattern.test(text);
const hasPragma = (text) => pragmaPattern.test(text);

const locStart = node => node.char_start;
const locEnd = node => node.char_end;
const locStart = (node) => node.char_start;
const locEnd = (node) => node.char_end;

/*
* metadata mostly pulled from linguist and rubocop:
Expand Down
4 changes: 2 additions & 2 deletions src/toProc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const isCall = node => ["::", "."].includes(node) || node.type === "@period";
const isCall = (node) => ["::", "."].includes(node) || node.type === "@period";

// If you have a simple block that only calls a method on the single required
// parameter that is passed to it, then you can replace that block with the
Expand All @@ -11,7 +11,7 @@ const isCall = node => ["::", "."].includes(node) || node.type === "@period";
// [1, 2, 3].map(&:to_s)
//
// This works with `do` blocks as well.
const toProc = node => {
const toProc = (node) => {
if (!node) {
return null;
}
Expand Down
Loading

0 comments on commit 4d1226a

Please sign in to comment.