Skip to content
This repository was archived by the owner on Feb 12, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 85 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use strict";

const utilShared = require("prettier").util;
const util = require("./_util-from-prettier");
const parse = require("./parser");
const print = require("./printer");
Expand All @@ -20,10 +21,79 @@ const languages = [
const parsers = {
ruby: {
parse,
astFormat: "ruby"
astFormat: "ruby",
locStart: locStart,
locEnd: locEnd
}
};

function locStart(node) {
// This function is copied from the code that used to live in the main prettier repo.

// Handle nodes with decorators. They should start at the first decorator
if (
node.declaration &&
node.declaration.decorators &&
node.declaration.decorators.length > 0
) {
return locStart(node.declaration.decorators[0]);
}
if (node.decorators && node.decorators.length > 0) {
return locStart(node.decorators[0]);
}

if (node.__location) {
return node.__location.startOffset;
}
if (node.range) {
return node.range[0];
}
if (typeof node.start === "number") {
return node.start;
}
if (node.source) {
return (
utilShared.lineColumnToIndex(node.source.start, node.source.input.css) - 1
);
}
if (node.loc) {
return node.loc.start;
}

return 0;
}

function locEnd(node) {
// This function is copied from the code that used to live in the main prettier repo.

const endNode = node.nodes && utilShared.getLast(node.nodes);
if (endNode && node.source && !node.source.end) {
node = endNode;
}

let loc;
if (node.range) {
loc = node.range[1];
} else if (typeof node.end === "number") {
loc = node.end;
} else if (node.source) {
loc = utilShared.lineColumnToIndex(node.source.end, node.source.input.css);
}

if (node.__location) {
return node.__location.endOffset;
}
if (node.typeAnnotation) {
return Math.max(loc, locEnd(node.typeAnnotation));
}

if (node.loc && !loc) {
return node.loc.end;
}

return loc || 0;
}

function canAttachComment(node) {
return node.ast_type && node.ast_type !== "comment";
}
Expand All @@ -32,8 +102,20 @@ function printComment(commentPath) {
const comment = commentPath.getValue();

switch (comment.ast_type) {
case "comment":
return comment.value;
case "comment": {
const hasNewLine = comment.value.lastIndexOf("\n");
let value = comment.value;
const hasSpaceAfterHash = value.charAt(1) === " ";
if (hasNewLine) {
value = value.slice(0, hasNewLine);
}

if (!hasSpaceAfterHash) {
value = "# " + value.slice(1, value.length);
}

return value;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about removing the leading # of the comment text, then stripping leading & trailing whitespace so there’s consistent spacing?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea 👍

}
default:
throw new Error("Not a comment: " + JSON.stringify(comment));
}
Expand Down
32 changes: 20 additions & 12 deletions src/printer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ function genericPrint(path, options, print) {
}

case "@ident": {
return concat([n.value, n.hardline ? hardline : ""]);
return path.call(print, "value");
}

case "var_field": {
Expand Down Expand Up @@ -525,7 +525,7 @@ function genericPrint(path, options, print) {
: path.call(print, "args");
let finalBody = group(concat(["(", body, ")"]));
const name = n.name && path.call(print, "name");
const value = name && name.parts[0];
const value = name;
if (keywords.hasOwnProperty(value)) {
finalBody = group(concat([" ", body]));
}
Expand Down Expand Up @@ -602,7 +602,7 @@ function genericPrint(path, options, print) {
}

case "symbol": {
return concat([":", path.call(print, "symbol")]);
return group(concat([":", path.call(print, "symbol")]));
}

case "dyna_symbol": {
Expand Down Expand Up @@ -651,7 +651,8 @@ function genericPrint(path, options, print) {
" ",
path.call(print, "from"),
" ",
path.call(print, "to")
path.call(print, "to"),
n.hardline ? hardline : ""
]);
}

Expand Down Expand Up @@ -713,13 +714,13 @@ function genericPrint(path, options, print) {
const targetParts = [];
targetParts.push(path.call(print, "target"), line, "=");
let value = group(path.call(print, "value"));
if (n.value.ast_type !== "hash") {
value = indent(group(concat([line, value])));
} else {
if (n.value.ast_type === "hash" || n.value.ast_type === "array") {
targetParts.push(line);
} else {
value = indent(group(concat([line, value])));
}
const target = group(concat(targetParts));
return concat([target, value]);
return group(concat([target, value, n.hardline ? hardline : ""]));
}

case "massign": {
Expand Down Expand Up @@ -1014,8 +1015,8 @@ function genericPrint(path, options, print) {
}

case "array": {
if (n.body === null) {
return "[]";
if (n.body === null || (n.body && n.body.length === 0)) {
return group(concat(["[", "]"]));
}
let parts = [];
const type = n.array_type;
Expand All @@ -1024,8 +1025,15 @@ function genericPrint(path, options, print) {
parts = group(
concat([
"[",
join(concat([", ", softline]), path.map(print, "body")),
"]"
indent(
group(
concat([
softline,
join(concat([",", line]), path.map(print, "body"))
])
)
),
concat([softline, dedent("]")])
])
);
break;
Expand Down
6 changes: 5 additions & 1 deletion tests/array/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@

exports[`empty_array.rb 1`] = `
empty_array = []

empty_array
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
empty_array = []

empty_array

`;

exports[`number_array.rb 1`] = `
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]

numbers
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]

numbers

`;
Expand Down
1 change: 1 addition & 0 deletions tests/array/empty_array.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
empty_array = []

empty_array
3 changes: 2 additions & 1 deletion tests/array/number_array.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]

numbers
20 changes: 20 additions & 0 deletions tests/comments/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`comments.rb 1`] = `
alias $foo $bar #comment

alias store []= #comment 2

alias :foo :bar #comment test test test test

alias foo bar
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
alias $foo $bar # comment

alias store []= # comment 2

alias :foo :bar # comment test test test test

alias foo bar

`;
7 changes: 7 additions & 0 deletions tests/comments/comments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
alias $foo $bar #comment

alias store []= #comment 2

alias :foo :bar #comment test test test test

alias foo bar
1 change: 1 addition & 0 deletions tests/comments/jsfmt.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
run_spec(__dirname, ["ruby"]);
Loading