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

refactor(analyze): add language field to rule metadata #2811

Conversation

dyc3
Copy link
Contributor

@dyc3 dyc3 commented May 10, 2024

Summary

This adds a new language field to RuleMetadata so we can emit better docs for end users.

closes #2801

For the sake of being comprehensive, this is the python script I used to do the codemod for all the existing rules:

Codemod
from pathlib import Path
import os
import sys

os.chdir(sys.path[0])

target_dir = Path("crates/biome_css_analyze/src/lint/")


def process_file(file_path: Path):
	with file_path.open("r") as f:
		lines = f.readlines()
	is_rule = False
	rule_name_line = None
	rule_source = None
	lang_likely = None
	for line_number, line in enumerate(lines):
		if "///" in line:
			if not lang_likely:
				if "```jsx" in line:
					lang_likely = "jsx"
				elif "```tsx" in line:
					lang_likely = "tsx"
				elif "```ts" in line:
					lang_likely = "ts"
				elif "```js" in line:
					lang_likely = "js"
				elif "```css" in line:
					lang_likely = "css"
			continue
		if "language: " in line:
			print("language already exists")
			return

		if "declare_rule! {" in line:
			is_rule = True
		if not rule_name_line and "name: " in line:
			rule_name_line = line_number
		if not rule_source and "sources: " in line:
			rule_source = line
		if rule_name_line is not None and rule_source is not None:
			break

	if not is_rule:
		print("not a rule")
		return

	lang_to_insert = None
	if not rule_source:
		rule_source = ""
	if "EslintReact" in rule_source:
		lang_to_insert = "jsx"
	elif "EslintJsxA11y" in rule_source:
		lang_to_insert = "jsx"
	elif "EslintTypeScript" in rule_source:
		lang_to_insert = "ts"
	elif "EslintJest" in rule_source:
		lang_to_insert = "js"
	elif "::Eslint(" in rule_source:
		lang_to_insert = "js"
	elif "Stylelint" in rule_source:
		lang_to_insert = "css"
	elif "type" in lines[rule_name_line]:
		lang_to_insert = "ts"
	else:
		lang_to_insert = lang_likely

	print("rule", lines[rule_name_line], lang_likely, lang_to_insert)
	if lang_to_insert is not None:
		lines.insert(rule_name_line + 1, f"        language: \"{lang_to_insert}\",\n")
		with file_path.open("w") as f:
			f.writelines(lines)

for file in target_dir.glob("**/*.rs"):
	process_file(file)

Test Plan

If it builds, it should work.

@github-actions github-actions bot added A-Linter Area: linter L-JavaScript Language: JavaScript and super languages L-CSS Language: CSS L-JSON Language: JSON and super languages labels May 10, 2024
@dyc3 dyc3 marked this pull request as ready for review May 10, 2024 14:27
Copy link
Member

@ematipico ematipico left a comment

Choose a reason for hiding this comment

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

This looks to me. @Conaclos, what do you think? I'd like your opinion

Copy link

codspeed-hq bot commented May 10, 2024

CodSpeed Performance Report

Merging #2811 will not alter performance

Comparing dyc3:05-10-refactor_analyze_add_language_field_to_rule_metadata (0230679) with main (7245146)

Summary

✅ 97 untouched benchmarks

@dyc3
Copy link
Contributor Author

dyc3 commented May 10, 2024

does the new field need to be added to codegen?

@dyc3 dyc3 force-pushed the 05-10-refactor_analyze_add_language_field_to_rule_metadata branch from 0230679 to 4aaf470 Compare May 11, 2024 10:55
@ematipico
Copy link
Member

does the new field need to be added to codegen?

Yeah, otherwise the macro will fail

@ematipico
Copy link
Member

We should update the contribution guide to reflect the new changes

@dyc3 dyc3 force-pushed the 05-10-refactor_analyze_add_language_field_to_rule_metadata branch from 4aaf470 to ff18ad5 Compare May 11, 2024 11:27
@github-actions github-actions bot added the A-Tooling Area: internal tools label May 11, 2024
@dyc3 dyc3 force-pushed the 05-10-refactor_analyze_add_language_field_to_rule_metadata branch from ff18ad5 to 8c6b501 Compare May 11, 2024 11:37
@dyc3
Copy link
Contributor Author

dyc3 commented May 11, 2024

I've updated the codegen and the contributing guide.

@ematipico ematipico merged commit 7a79dda into biomejs:main May 11, 2024
11 of 12 checks passed
@dyc3 dyc3 deleted the 05-10-refactor_analyze_add_language_field_to_rule_metadata branch May 11, 2024 13:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Linter Area: linter A-Tooling Area: internal tools L-CSS Language: CSS L-JavaScript Language: JavaScript and super languages L-JSON Language: JSON and super languages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a language field to RuleMetadata
2 participants