8000 Run prettier on the remainder of the Repo by robert-j-webb · Pull Request #1 · robert-j-webb/prism · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Run prettier on the remainder of the Repo #1

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

Open
wants to merge 3 commits into
base: rob/v2-prettier
Choose a base branch
from

Conversation

robert-j-webb
Copy link
Owner

No description provided.

@robert-j-webb robert-j-webb changed the title Rob/v2 run prettier Run prettier on the remainder of the Repo Mar 17, 2025
Copy link
github-actions bot commented Mar 17, 2025

No JS Changes

Generated by 🚫 dangerJS against bbadd78

@robert-j-webb
Copy link
Owner Author

So this prettier run mangles scriptsa/build.ts. I added it to .prettierignore to avoid any issues

Original

const inlineRegexSourcePlugin: Plugin = {
	name: 'inline-regex-source',
	renderChunk(code) {
		const str = new MagicString(code);
		str.replace(
			/\/((?:[^\n\r[\\\/]|\\.|\[(?:[^\n\r\\\]]|\\.)*\])+)\/\s*\.\s*source\b/g,
			(m, source: string) => {
				// escape backslashes
				source = source.replace(/\\(.)|\[(?:\\s\\S|\\S\\s)\]/g, (m, g1: string) => {
					if (g1) {
						// characters like /\n/ can just be kept as "\n" instead of being escaped to "\\n"
						if (/[nrt0/]/.test(g1)) {
							return m;
						}
						if ('\\' === g1) {
							return '\\\\\\\\'; // escape using 4 backslashes
						}
						return '\\\\' + g1;
					} else {
						return '[^]';
					}
				});
				// escape single quotes
				source = source.replace(/'/g, "\\'");
				// wrap source in single quotes
				return "'" + source + "'";
			}
		);
		return toRenderedChunk(str);
	},
};

Mangled by prettier:

const inlineRegexSourcePlugin: Plugin = {
	name: 'inline-regex-source',
	renderChunk (code) {
		const str = new MagicString(code);
		str.replace(
			/\/((?:[^\n\r[\\\/]|\\.|\[(?:[^\n\r\\\]]|\\.)*\])+)\/\s*\.\s*source\b/g,
			(m, source: string) => {
				// escape backslashes
				source = source.replace(/\\(.)|\[(?:\\s\\S|\\S\\s)\]/g, (m, g1: string) => {
					if (g1) {
						// characters like /\n/ can just be kept as "\n" instead of being escaped to "\\n"
						if (/[nrt0/]/.test(g1)) {
							return m;
						}
						if ('\\' === g1) {
							return '\\\\\\\\'; // escape using 4 backslashes
						}
						return '\\\\' + g1;
					}
					else {
						return '[^]';
					}
					if ('\\' === g1) {
						return '\\\\\\\\'; // escape using 4 backslashes
					}
					return '\\\\' + g1;
				} else {
					return '[^]';
				}
			});
			// escape single quotes
			source = source.replace(/'/g, "\\'");
			// wrap source in single quotes
			return "'" + source + "'";
		});
		return toRenderedChunk(str);
	},
};

@DmitrySharabin
Copy link

So this prettier run mangles scriptsa/build.ts. I added it to .prettierignore to avoid any issues

How about adding prettier-ignore and not excluding the file entirely? It seems to solve the issue and correctly adds a space after renderChunk.

// prettier-ignore
const inlineRegexSourcePlugin: Plugin = {

Could you please check if it works not “on my machine” only? 🙂

@robert-j-webb
Copy link
Owner Author

So this prettier run mangles scriptsa/build.ts. I added it to .prettierignore to avoid any issues

How about adding prettier-ignore and not excluding the file entirely? It seems to solve the issue and correctly adds a space after renderChunk.

// prettier-ignore
const inlineRegexSourcePlugin: Plugin = {

Could you please check if it works not “on my machine” only? 🙂

I did one better and applied it just to the lambda that gets mangled : )

@DmitrySharabin
Copy link

I tried another approach that might be slightly better. I noticed that none of the plugins mangle the files by themselves. It only happens when we expect them to be applied sequentially. I already knew plugin order matters, and the one we use here plays well in our other project. But it seems we should swap the prettier-plugin-space-before-function-paren and prettier-plugin-brace-style here.

{
	"plugins": [
		"prettier-plugin-space-before-function-paren",
		"prettier-plugin-brace-style",
		"prettier-plugin-merge"
	],
	...
}

Could you please check it?

@DmitrySharabin
Copy link

Actually, it is weird that we have to do that. It's more like we are working around a bug in our plugin (which adds a space before the function paren). We need to fix the plugin first. I’ll work on it.

@DmitrySharabin
Copy link
DmitrySharabin commented Mar 19, 2025

Actually, it is weird that we have to do that. It's more like we are working around a bug in our plugin (which adds a space before the function paren). We need to fix the plugin first. I’ll work on it.

It turned out it's not an issue with the prettier-plugin-space-before-function-paren plugin (phew 😅). It's the prettier-plugin-merge that screws things up (in some cases). The workaround I proposed seems still valid. However, in cases where previously we had mangled blocks of code, else might not be on a separate line.

I have another issue to file concerning the merge plugin. It might be when that issue is fixed, we won't need any workarounds. 🤞

UPDATE: I hope to update the prettier-plugin-space-before-function-paren plugin soon, so it covers all the cases Prism codebase needs. I'll let you know when it's published.

UPDATE 2: Another option would be adding "parser": "babel-ts" to .prettierrc (and don't swap the plugins), but it's not ideal either—the same problem with not handled else.

@DmitrySharabin
Copy link

I'll let you know when it's published

It's published: https://www.npmjs.com/package/prettier-plugin-space-before-function-paren

@robert-j-webb
Copy link
Owner Author

I'll let you know when it's published

It's published: https://www.npmjs.com/package/prettier-plugin-space-before-function-paren

Unfortunately still seeing the same issue. I did check package-json.lock to make sure I had 0.0.7

@robert-j-webb
Copy link
Owner Author

Correct that changing plugin order does fix it though!

@robert-j-webb robert-j-webb force-pushed the rob/v2-run-prettier branch 2 times, most recently from fb0b389 to 84f1b0d Compare March 19, 2025 21:59
@DmitrySharabin
Copy link

I'll let you know when it's published

It's published: https://www.npmjs.com/package/prettier-plugin-space-before-function-paren

Unfortunately still seeing the same issue. I did check package-json.lock to make sure I had 0.0.7

Yeah, it is more for other issues that weren't previously addressed. I filed an issue for the one that causes files to be mangled. I hope they can give me a hint of why we might face it at all.

8000
}
]
]
"presets": [

Choose a reason for hiding this comment

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

I think we could actually skip babel and minification entirely in v2. If folks need broader browser support than what we provide or to minify, they can do their own transformations.

@DmitrySharabin
Copy link

I'll let you know when it's published

It's published: https://www.npmjs.com/package/prettier-plugin-space-before-function-paren

Unfortunately still seeing the same issue. I did check package-json.lock to make sure I had 0.0.7

Yeah, it is more for other issues that weren't previously addressed. I filed an issue for the one that causes files to be mangled. I hope they can give me a hint of why we might face it at all.

Okay, the issue with the prettier-plugin-merge is fixed in the recent version, and the file doesn't seem to be mangled anymore (and we probably don't need either fully ignore it or none of its parts). 🥳

Speaking of the plugin order, we still need our plugin to come after the prettier-plugin-brace-style (in that case, all the changes made by both plugins will be preserved):

"plugins": [
	"prettier-plugin-brace-style",
	"prettier-plugin-space-before-function-paren",
	"prettier-plugin-merge"
]

@robert-j-webb
8000
Copy link
Owner Author

I'll let you know when it's published

It's published: https://www.npmjs.com/package/prettier-plugin-space-before-function-paren

Unfortunately still seeing the same issue. I did check package-json.lock to make sure I had 0.0.7

Yeah, it is more for other issues that weren't previously addressed. I filed an issue for the one that causes files to be mangled. I hope they can give me a hint of why we might face it at all.

Okay, the issue with the prettier-plugin-merge is fixed in the recent version, and the file doesn't seem to be mangled anymore (and we probably don't need either fully ignore it or none of its parts). 🥳

Speaking of the plugin order, we still need our plugin to come after the prettier-plugin-brace-style (in that case, all the changes made by both plugins will be preserved):

"plugins": [
	"prettier-plugin-brace-style",
	"prettier-plugin-space-before-function-paren",
	"prettier-plugin-merge"
]

Ok, updated to new version, and update order. Everything is looking groovy : )

@robert-j-webb robert-j-webb force-pushed the rob/v2-prettier branch 3 times, most recently from 0071f98 to f390aec Compare March 26, 2025 22:20
…3876)

Note that prettier sync is being imported in order to enable us to format test code during tests. It should not be used anywhere else

Updated eslint rules to work with prettier, including removing redundant rules.
@robert-j-webb robert-j-webb force-pushed the rob/v2-run-prettier branch 2 times, most recently from a95fa3a to 6637d4d Compare March 26, 2025 22:34
@robert-j-webb robert-j-webb force-pushed the rob/v2-run-prettier branch 2 times, most recently from 2aed9fc to f5a8a46 Compare March 27, 2025 15:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
48D1 None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0