-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support
sh
(uses prettier-plugin-sh
).
- Loading branch information
Showing
7 changed files
with
207 additions
and
0 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,7 @@ | ||
export const embeddedLanguage = "embeddedSh"; | ||
|
||
declare module "../types.js" { | ||
interface EmbeddedLanguagesHolder { | ||
[embeddedLanguage]: void; | ||
} | ||
} |
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,99 @@ | ||
import type { Options } from "prettier"; | ||
import { builders, utils } from "prettier/doc"; | ||
import type { Embedder } from "../../types.js"; | ||
import { preparePlaceholder, printTemplateExpressions } from "../utils.js"; | ||
import { embeddedLanguage } from "./embedded-language.js"; | ||
|
||
const { hardline, group, line, softline, indent } = builders; | ||
const { mapDoc } = utils; | ||
|
||
export const embedder: Embedder<Options> = async ( | ||
textToDoc, | ||
print, | ||
path, | ||
options, | ||
{ identifier, embeddedOverrideOptions }, | ||
) => { | ||
options = { | ||
...options, | ||
...embeddedOverrideOptions, | ||
}; | ||
|
||
const { node } = path; | ||
|
||
const { createPlaceholder, placeholderRegex } = preparePlaceholder(); | ||
|
||
const text = node.quasis | ||
.map((quasi, index, { length }) => | ||
index === length - 1 | ||
? quasi.value.cooked | ||
: quasi.value.cooked + createPlaceholder(index), | ||
) | ||
.join(""); | ||
|
||
const leadingWhitespaces = text.match(/^\s+/)?.[0] ?? ""; | ||
const trailingWhitespaces = text.match(/\s+$/)?.[0] ?? ""; | ||
|
||
const expressionDocs = printTemplateExpressions(path, print); | ||
|
||
const doc = await textToDoc(text, { | ||
...options, | ||
parser: "sh", | ||
}); | ||
|
||
const contentDoc = mapDoc(doc, (doc) => { | ||
if (typeof doc !== "string") { | ||
return doc; | ||
} | ||
const parts = []; | ||
const components = doc.split(placeholderRegex); | ||
for (let i = 0; i < components.length; i++) { | ||
let component = components[i]; | ||
if (i % 2 == 0) { | ||
if (!component) { | ||
continue; | ||
} | ||
component = component.replaceAll(/([\\`]|\${)/g, "\\$1"); | ||
component | ||
.split(/(\n)/) | ||
.forEach((c) => (c === "\n" ? parts.push(hardline) : parts.push(c))); | ||
} else { | ||
const placeholderIndex = Number(component); | ||
parts.push(expressionDocs[placeholderIndex]); | ||
} | ||
} | ||
return parts; | ||
}); | ||
|
||
if (options.preserveEmbeddedExteriorWhitespaces?.includes(identifier)) { | ||
// TODO: should we label the doc with { hug: false } ? | ||
// https://github.com/prettier/prettier/blob/5cfb76ee50cf286cab267cf3cb7a26e749c995f7/src/language-js/embed/html.js#L88 | ||
return group([ | ||
"`", | ||
leadingWhitespaces, | ||
options.noEmbeddedMultiLineIndentation?.includes(identifier) | ||
? [group(contentDoc)] | ||
: indent([group(contentDoc)]), | ||
trailingWhitespaces, | ||
"`", | ||
]); | ||
} | ||
|
||
const leadingLineBreak = leadingWhitespaces.length ? line : softline; | ||
const trailingLineBreak = trailingWhitespaces.length ? line : softline; | ||
|
||
return group([ | ||
"`", | ||
options.noEmbeddedMultiLineIndentation?.includes(identifier) | ||
? [leadingLineBreak, group(contentDoc)] | ||
: indent([leadingLineBreak, group(contentDoc)]), | ||
trailingLineBreak, | ||
"`", | ||
]); | ||
}; | ||
|
||
declare module "../types.js" { | ||
interface EmbeddedEmbedders { | ||
[embeddedLanguage]: typeof embedder; | ||
} | ||
} |
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,3 @@ | ||
export * from "./embedded-language.js"; | ||
export * from "./embedder.js"; | ||
export * from "./options.js"; |
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,46 @@ | ||
import type { CoreCategoryType, SupportOptions } from "prettier"; | ||
import type { ShParserOptions } from "prettier-plugin-sh"; | ||
import { | ||
makeIdentifiersOptionName, | ||
type AutocompleteStringList, | ||
type StringListToInterfaceKey, | ||
} from "../utils.js"; | ||
import { embeddedLanguage } from "./embedded-language.js"; | ||
|
||
/** References: | ||
* - https://github.com/github-linguist/linguist/blob/7ca3799b8b5f1acde1dd7a8dfb7ae849d3dfb4cd/lib/linguist/languages.yml#L6445C8-L6445C8 | ||
*/ | ||
const DEFAULT_IDENTIFIERS = ["sh"] as const; | ||
type Identifiers = AutocompleteStringList<typeof DEFAULT_IDENTIFIERS>; | ||
type DefaultIdentifiersHolder = StringListToInterfaceKey< | ||
typeof DEFAULT_IDENTIFIERS | ||
>; | ||
|
||
const EMBEDDED_LANGUAGE_IDENTIFIERS = | ||
makeIdentifiersOptionName(embeddedLanguage); | ||
|
||
export interface PrettierPluginDepsOptions extends Partial<ShParserOptions> {} | ||
|
||
export const options = { | ||
[EMBEDDED_LANGUAGE_IDENTIFIERS]: { | ||
category: "Global", | ||
type: "string", | ||
array: true, | ||
default: [{ value: [...DEFAULT_IDENTIFIERS] }], | ||
description: "Specify embedded Shell language identifiers.", | ||
}, | ||
} satisfies SupportOptions & Record<string, { category: CoreCategoryType }>; | ||
|
||
type Options = typeof options; | ||
|
||
declare module "../types.js" { | ||
interface EmbeddedOptions extends Options {} | ||
interface EmbeddedDefaultIdentifiersHolder extends DefaultIdentifiersHolder {} | ||
interface PrettierPluginEmbedOptions { | ||
[EMBEDDED_LANGUAGE_IDENTIFIERS]?: Identifiers; | ||
} | ||
} | ||
|
||
declare module "prettier" { | ||
export interface Options extends PrettierPluginDepsOptions {} | ||
} |