Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enhance embeddedOverrides option #33

Merged
merged 9 commits into from
Nov 22, 2023
Merged
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
3 changes: 2 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"tabWidth": 2,
"useTabs": false
"useTabs": false,
"plugins": ["prettier-plugin-organize-imports"]
}
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<div align="center">

![prettier-plugin-embed-light](./public/prettier-plugin-embed-wide-light.svg#gh-light-mode-only)
![prettier-plugin-embed-dark](./public/prettier-plugin-embed-wide-dark.svg#gh-dark-mode-only)
![prettier-plugin-embed-light](./asset/prettier-plugin-embed-wide-light.svg#gh-light-mode-only)
![prettier-plugin-embed-dark](./asset/prettier-plugin-embed-wide-dark.svg#gh-dark-mode-only)

# Prettier Plugin Embed

[![npm](https://img.shields.io/npm/v/prettier-plugin-embed)](https://www.npmjs.com/package/prettier-plugin-embed/v/latest) [![npm bundle size (scoped)](https://img.shields.io/bundlephobia/minzip/prettier-plugin-embed)](https://www.npmjs.com/package/prettier-plugin-embed/v/latest) [![jsDelivr hits (npm scoped)](https://img.shields.io/jsdelivr/npm/hm/prettier-plugin-embed?color=%23ff5627)](https://cdn.jsdelivr.net/npm/prettier-plugin-embed@latest/)
[![npm version](https://badgen.net/npm/v/prettier-plugin-embed?cache=300)](https://www.npmjs.com/package/prettier-plugin-embed/v/latest) [![npm downloads](https://badgen.net/npm/dm/prettier-plugin-embed?cache=300)](https://www.npmjs.com/package/prettier-plugin-embed/v/latest) [![npm license](https://badgen.net/npm/license/prettier-plugin-embed?cache=300)](https://www.npmjs.com/package/prettier-plugin-embed/v/latest)

[![github last commit](https://badgen.net/github/last-commit/Sec-ant/prettier-plugin-embed?cache=300)](https://github.com/Sec-ant/prettier-plugin-embed) [![bundlephobia minzipped](https://badgen.net/bundlephobia/minzip/prettier-plugin-embed?cache=300)](https://bundlephobia.com/package/prettier-plugin-embed@latest) [![](https://data.jsdelivr.com/v1/package/npm/prettier-plugin-embed/badge?style=rounded)](https://www.jsdelivr.com/package/npm/prettier-plugin-embed)

A Configurable [Prettier](https://prettier.io/) [Plugin](https://prettier.io/docs/en/plugins.html) to Format [Embedded Languages](https://prettier.io/docs/en/options.html#embedded-language-formatting) in `js`/`ts` Files.

Expand Down Expand Up @@ -215,9 +217,13 @@ Formatting embedded XML code requires [`@prettier/plugin-xml`](https://github.co

#### `embeddedOverrides`

This option is provided for users to override certain options based on identifiers. Due to the lack of support for using objects in prettier plugin options (https://github.com/prettier/prettier/issues/14671), it accepts a **stringified** json string or an **absolute** json file path as its value.
This option is provided for users to override certain options based on identifiers. Due to the lack of support for using objects in prettier plugin options (https://github.com/prettier/prettier/issues/14671), it accepts a **stringified** json string, or a file path with an extension of `.json` or `.js` or `.cjs` or `.mjs` as its value. If no extension is provided, it will be treated as a `.json` file. For relative paths, it will automatically figure out the prettier config location and use that as the base path.

The resolved value should be an array of objects. Each object in the array must have 2 fields: `identifiers` and `options`. The `options` are considerred overrides that will be applied to the global `options` of prettier for those `idenfitiers` only. It's like the [`overrides`](https://prettier.io/docs/en/configuration.html#configuration-overrides) of `prettier`, but it is identifier-based instead of file-based.

In a json file, the root is the array of objects. In a javascript file, the array of objects should be a default export, or a named export with the name `embeddedOverrides`.

The value should be an array of objects. Each object must have 2 fields: `identifiers` and `options`. The `options` are considerred overrides that will be applied to the global `options` of prettier for those `idenfitiers` only. It's like the [`overrides`](https://prettier.io/docs/en/configuration.html#configuration-overrides) of `prettier`, but it is identifier-based instead of file-based. An example is:
An example `.json` file is:

```json
[
Expand Down
54 changes: 46 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "prettier-plugin-embed",
"description": "A configurable Prettier plugin to format embedded languages in js/ts files.",
"private": false,
"version": "0.3.0-rc.0",
"version": "0.3.0-rc.1",
"type": "module",
"files": [
"./dist"
Expand Down Expand Up @@ -75,10 +75,11 @@
"fast-glob": "^3.3.2",
"npm-check-updates": "^16.14.11",
"playwright": "^1.40.0",
"prettier-plugin-organize-imports": "^3.2.4",
"semantic-release": "^22.0.8",
"tsx": "^4.2.0",
"typescript": "^5.3.2",
"vite": "^5.0.0",
"vite": "^5.0.2",
"vitest": "^0.34.6"
},
"peerDependencies": {
Expand All @@ -92,6 +93,10 @@
"prettier-plugin-sql": ">=0.15.0 <1"
},
"dependencies": {
"micro-memoize": "^4.1.2"
"micro-memoize": "^4.1.2",
"package-up": "^5.0.0"
},
"optionalDependencies": {
"tsx": "^4.2.0"
}
}
2 changes: 1 addition & 1 deletion scripts/add-glob-import-types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fastGlob from "fast-glob";
import { appendFile, stat } from "node:fs/promises";
import { fileURLToPath } from "node:url";
import fastGlob from "fast-glob";

const registerTypeDeclarationFilePath = fileURLToPath(
new URL("../dist/embedded/register.d.ts", import.meta.url),
Expand Down
4 changes: 2 additions & 2 deletions scripts/process-readme.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { readFile, writeFile, copyFile } from "node:fs/promises";
import { resolve, dirname } from "node:path";
import { copyFile, readFile, writeFile } from "node:fs/promises";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";

const readmeFilePath = fileURLToPath(new URL("../README.md", import.meta.url));
Expand Down
2 changes: 1 addition & 1 deletion scripts/restore-readme.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { copyFile, unlink } from "node:fs/promises";
import { resolve, dirname } from "node:path";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";

const readmeFilePath = fileURLToPath(new URL("../README.md", import.meta.url));
Expand Down
9 changes: 4 additions & 5 deletions src/embedded/css/embedder.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { type Options } from "prettier";
import type { Options } from "prettier";
import { builders } from "prettier/doc";
import type { Embedder } from "../../types.js";
import {
printTemplateExpressions,
preparePlaceholder,
printTemplateExpressions,
simpleRehydrateDoc,
parseEmbeddedOverrideOptions,
} from "../utils.js";
import { embeddedLanguage } from "./embedded-language.js";

Expand All @@ -16,11 +15,11 @@ export const embedder: Embedder<Options> = async (
print,
path,
options,
identifier,
{ identifier, embeddedOverrideOptions },
) => {
options = {
...options,
...parseEmbeddedOverrideOptions(options.embeddedOverrides, identifier),
...embeddedOverrideOptions,
};

const { node } = path;
Expand Down
4 changes: 2 additions & 2 deletions src/embedded/css/options.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { CoreCategoryType, SupportOptions } from "prettier";
import { embeddedLanguage } from "./embedded-language.js";
import {
makeIdentifiersOptionName,
type AutocompleteStringList,
type StringListToInterfaceKey,
makeIdentifiersOptionName,
} from "../utils.js";
import { embeddedLanguage } from "./embedded-language.js";

/** References:
* - https://github.com/microsoft/vscode/blob/de0121cf0e05d1673903551b6dbb2871556bfae9/extensions/css/package.json#L22
Expand Down
9 changes: 4 additions & 5 deletions src/embedded/es/embedder.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { type Options } from "prettier";
import type { Options } from "prettier";
import { builders } from "prettier/doc";
import type { Embedder } from "../../types.js";
import {
printTemplateExpressions,
preparePlaceholder,
printTemplateExpressions,
simpleRehydrateDoc,
parseEmbeddedOverrideOptions,
} from "../utils.js";
import { embeddedLanguage } from "./embedded-language.js";

Expand All @@ -16,11 +15,11 @@ export const embedder: Embedder<Options> = async (
print,
path,
options,
identifier,
{ identifier, embeddedOverrideOptions },
) => {
options = {
...options,
...parseEmbeddedOverrideOptions(options.embeddedOverrides, identifier),
...embeddedOverrideOptions,
};

const { node } = path;
Expand Down
6 changes: 3 additions & 3 deletions src/embedded/es/options.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { CoreCategoryType, SupportOptions } from "prettier";
import { embeddedLanguage } from "./embedded-language.js";
import {
type AutocompleteStringList,
type StringListToInterfaceKey,
makeIdentifiersOptionName,
makeParserOptionName,
type AutocompleteStringList,
type StringListToInterfaceKey,
} from "../utils.js";
import { embeddedLanguage } from "./embedded-language.js";

/** References:
* - https://github.com/microsoft/vscode/blob/de0121cf0e05d1673903551b6dbb2871556bfae9/extensions/javascript/package.json#L37
Expand Down
9 changes: 4 additions & 5 deletions src/embedded/glsl/embedder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import type { Options } from "prettier";
import { builders } from "prettier/doc";
import type { Embedder } from "../../types.js";
import {
printTemplateExpressions,
throwIfPluginIsNotFound,
preparePlaceholder,
printTemplateExpressions,
simpleRehydrateDoc,
parseEmbeddedOverrideOptions,
throwIfPluginIsNotFound,
} from "../utils.js";
import { embeddedLanguage } from "./embedded-language.js";

Expand All @@ -17,13 +16,13 @@ export const embedder: Embedder<Options> = async (
print,
path,
options,
identifier,
{ identifier, embeddedOverrideOptions },
) => {
throwIfPluginIsNotFound("prettier-plugin-glsl", options, identifier);

options = {
...options,
...parseEmbeddedOverrideOptions(options.embeddedOverrides, identifier),
...embeddedOverrideOptions,
};

const { node } = path;
Expand Down
4 changes: 2 additions & 2 deletions src/embedded/glsl/options.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { CoreCategoryType, SupportOptions } from "prettier";
import { embeddedLanguage } from "./embedded-language.js";
import {
makeIdentifiersOptionName,
type AutocompleteStringList,
type StringListToInterfaceKey,
makeIdentifiersOptionName,
} from "../utils.js";
import { embeddedLanguage } from "./embedded-language.js";

/** References:
* - https://github.com/NaridaL/glsl-language-toolkit/blob/a7bd08d3f31355b335ae002b6a44c2999998b952/packages/prettier-plugin-glsl/src/prettier-plugin.ts#L88
Expand Down
12 changes: 4 additions & 8 deletions src/embedded/html/embedder.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { type Options } from "prettier";
import type { Options } from "prettier";
import { builders, utils } from "prettier/doc";
import type { Embedder } from "../../types.js";
import {
printTemplateExpressions,
preparePlaceholder,
parseEmbeddedOverrideOptions,
} from "../utils.js";
import { preparePlaceholder, printTemplateExpressions } from "../utils.js";
import { embeddedLanguage } from "./embedded-language.js";

const { line, group, indent, softline } = builders;
Expand All @@ -16,11 +12,11 @@ export const embedder: Embedder<Options> = async (
print,
path,
options,
identifier,
{ identifier, embeddedOverrideOptions },
) => {
options = {
...options,
...parseEmbeddedOverrideOptions(options.embeddedOverrides, identifier),
...embeddedOverrideOptions,
};

const { node } = path;
Expand Down
4 changes: 2 additions & 2 deletions src/embedded/html/options.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { CoreCategoryType, SupportOptions } from "prettier";
import { embeddedLanguage } from "./embedded-language.js";
import {
makeIdentifiersOptionName,
type AutocompleteStringList,
type StringListToInterfaceKey,
makeIdentifiersOptionName,
} from "../utils.js";
import { embeddedLanguage } from "./embedded-language.js";

/** References
* - https://github.com/microsoft/vscode/blob/de0121cf0e05d1673903551b6dbb2871556bfae9/extensions/html/package.json#L18
Expand Down
6 changes: 3 additions & 3 deletions src/embedded/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export * from "./register.js";
export type {
EmbeddedLanguage,
EmbeddedParsers,
EmbeddedDefaultIdentifier,
EmbeddedEmbedders,
EmbeddedLanguage,
EmbeddedOptions,
EmbeddedDefaultIdentifier,
EmbeddedParsers,
PrettierPluginEmbedOptions,
} from "./types.js";
export {
Expand Down
Loading