This repository has been archived by the owner on Jun 24, 2022. It is now read-only.
forked from prettier/plugin-ruby
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
40 changed files
with
1,312 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ | |
/yarn-error.log | ||
/test.rb | ||
/test.rbs | ||
/test.haml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,10 +7,12 @@ | |
|
||
/.eslintcache | ||
/.*ignore | ||
/bin/port | ||
/docs/logo.png | ||
/*.lock | ||
/LICENSE | ||
/test.rb | ||
/test.rbs | ||
/test.haml | ||
|
||
*.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/usr/bin/env bash | ||
|
||
LOW_BOUND=49152 | ||
RANGE=16384 | ||
|
||
while true; do | ||
CANDIDATE=$[$LOW_BOUND + ($RANDOM % $RANGE)] | ||
(echo "" >/dev/tcp/127.0.0.1/${CANDIDATE}) >/dev/null 2>&1 | ||
|
||
if [ $? -ne 0 ]; then | ||
echo $CANDIDATE | ||
break | ||
fi | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
const { | ||
concat, | ||
hardline, | ||
indent, | ||
literalline, | ||
markAsRoot, | ||
mapDoc, | ||
stripTrailingHardline | ||
} = require("../prettier"); | ||
|
||
// Get the name of the parser that is represented by the given element node, | ||
// return null if a matching parser cannot be found | ||
function getParser(name, opts) { | ||
let parser = name; | ||
|
||
// We don't want to deal with some weird recursive parser situation, so we | ||
// need to explicitly call out the HAML parser here and just return null | ||
if (parser === "haml") { | ||
return null; | ||
} | ||
|
||
// In HAML the name of the JS filter is :javascript, whereas in prettier the | ||
// name of the JS parser is babel. Here we explicitly handle that conversion. | ||
if (parser === "javascript") { | ||
parser = "babel"; | ||
} | ||
|
||
// If there is a plugin that has a parser that matches the name of this | ||
// element, then we're going to assume that's correct for embedding and go | ||
// ahead and switch to that parser | ||
if ( | ||
opts.plugins.some( | ||
(plugin) => | ||
plugin.parsers && | ||
Object.prototype.hasOwnProperty.call(plugin.parsers, parser) | ||
) | ||
) { | ||
return parser; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
// This function is in here because it handles embedded parser values. I don't | ||
// have a test that exercises it because I'm not sure for which parser it is | ||
// necessary, but since it's in prettier core I'm keeping it here. | ||
/* istanbul ignore next */ | ||
function replaceNewlines(doc) { | ||
return mapDoc(doc, (currentDoc) => | ||
typeof currentDoc === "string" && currentDoc.includes("\n") | ||
? concat( | ||
currentDoc | ||
.split(/(\n)/g) | ||
.map((v, i) => (i % 2 === 0 ? v : literalline)) | ||
) | ||
: currentDoc | ||
); | ||
} | ||
|
||
function embed(path, _print, textToDoc, opts) { | ||
const node = path.getValue(); | ||
if (node.type !== "filter") { | ||
return null; | ||
} | ||
|
||
const parser = getParser(node.value.name, opts); | ||
if (!parser) { | ||
return null; | ||
} | ||
|
||
return markAsRoot( | ||
concat([ | ||
":", | ||
node.value.name, | ||
indent( | ||
concat([ | ||
hardline, | ||
replaceNewlines( | ||
stripTrailingHardline(textToDoc(node.value.text, { parser })) | ||
) | ||
]) | ||
) | ||
]) | ||
); | ||
} | ||
|
||
module.exports = embed; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const { concat, group, hardline, indent, join } = require("../../prettier"); | ||
|
||
// https://haml.info/docs/yardoc/file.REFERENCE.html#html-comments- | ||
function comment(path, _opts, print) { | ||
const { children, value } = path.getValue(); | ||
const parts = ["/"]; | ||
|
||
if (value.revealed) { | ||
parts.push("!"); | ||
} | ||
|
||
if (value.conditional) { | ||
parts.push(value.conditional); | ||
} else if (value.text) { | ||
parts.push(" ", value.text); | ||
} | ||
|
||
if (children.length > 0) { | ||
parts.push( | ||
indent(concat([hardline, join(hardline, path.map(print, "children"))])) | ||
); | ||
} | ||
|
||
return group(concat(parts)); | ||
} | ||
|
||
module.exports = comment; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const { join } = require("../../prettier"); | ||
|
||
const types = { | ||
basic: "Basic", | ||
frameset: "Frameset", | ||
mobile: "Mobile", | ||
rdfa: "RDFa", | ||
strict: "Strict", | ||
xml: "XML" | ||
}; | ||
|
||
const versions = ["1.1", "5"]; | ||
|
||
// https://haml.info/docs/yardoc/file.REFERENCE.html#doctype- | ||
function doctype(path, _opts, _print) { | ||
const { value } = path.getValue(); | ||
const parts = ["!!!"]; | ||
|
||
if (value.type in types) { | ||
parts.push(types[value.type]); | ||
} else if (versions.includes(value.version)) { | ||
parts.push(value.version); | ||
} else { | ||
parts.push(value.type); | ||
} | ||
|
||
if (value.encoding) { | ||
parts.push(value.encoding); | ||
} | ||
|
||
return join(" ", parts); | ||
} | ||
|
||
module.exports = doctype; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const { concat, group, hardline, indent, join } = require("../../prettier"); | ||
|
||
// https://haml.info/docs/yardoc/file.REFERENCE.html#filters | ||
function filter(path, _opts, _print) { | ||
const { value } = path.getValue(); | ||
|
||
return group( | ||
concat([ | ||
":", | ||
value.name, | ||
indent(concat([hardline, join(hardline, value.text.trim().split("\n"))])) | ||
]) | ||
); | ||
} | ||
|
||
module.exports = filter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const { concat, hardline, indent, join } = require("../../prettier"); | ||
|
||
// https://haml.info/docs/yardoc/file.REFERENCE.html#haml-comments-- | ||
function hamlComment(path, opts, _print) { | ||
const node = path.getValue(); | ||
const parts = ["-#"]; | ||
|
||
if (node.value.text) { | ||
if (opts.originalText.split("\n")[node.line - 1].trim() === "-#") { | ||
const lines = node.value.text.trim().split("\n"); | ||
|
||
parts.push(indent(concat([hardline, join(hardline, lines)]))); | ||
} else { | ||
parts.push(" ", node.value.text.trim()); | ||
} | ||
} | ||
|
||
return concat(parts); | ||
} | ||
|
||
module.exports = hamlComment; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// https://haml.info/docs/yardoc/file.REFERENCE.html#plain-text | ||
function plain(path, _opts, _print) { | ||
return path.getValue().value.text; | ||
} | ||
|
||
module.exports = plain; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const { concat, hardline, join } = require("../../prettier"); | ||
|
||
// The root node in the AST | ||
function root(path, _opts, print) { | ||
return concat([join(hardline, path.map(print, "children")), hardline]); | ||
} | ||
|
||
module.exports = root; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const { concat, group, hardline, indent, join } = require("../../prettier"); | ||
|
||
// https://haml.info/docs/yardoc/file.REFERENCE.html#inserting_ruby | ||
function script(path, opts, print) { | ||
const { children, value } = path.getValue(); | ||
const parts = []; | ||
|
||
if (value.escape_html) { | ||
parts.unshift("&"); | ||
} | ||
|
||
if (value.preserve) { | ||
parts.push("~"); | ||
} else if (!value.interpolate) { | ||
parts.push("="); | ||
} | ||
|
||
if (value.escape_html && !value.preserve && value.interpolate) { | ||
parts.push(" ", value.text.trim().slice(1, -1)); | ||
} else { | ||
parts.push(" ", value.text.trim()); | ||
} | ||
|
||
if (children.length > 0) { | ||
parts.push( | ||
indent(concat([hardline, join(hardline, path.map(print, "children"))])) | ||
); | ||
} | ||
|
||
return group(concat(parts)); | ||
} | ||
|
||
module.exports = script; |
Oops, something went wrong.