_default:
  just --list -u

alias f := format
alias t := test
alias r := ready
alias l := lint
alias qt := test-quick

set windows-powershell := true

# Installs the tools needed to develop
install-tools:
	cargo install cargo-binstall
	cargo binstall cargo-insta@1.48.0 wasm-opt cargo-deny
	cargo binstall wasm-bindgen-cli --version 0.2.117
	pnpm install

# Upgrades the tools needed to develop
upgrade-tools:
	cargo install cargo-binstall --force
	cargo binstall cargo-insta@1.48.0 wasm-opt cargo-deny --force
	cargo binstall wasm-bindgen-cli --version 0.2.117 --force

# Generate all files across crates and tools. You rarely want to use it locally.
gen-all:
  cargo run -p xtask_codegen -- all
  just gen-configuration
  just gen-migrate
  just gen-bindings
  just format

# Generates TypeScript types and JSON schema of the configuration
gen-bindings:
  just gen-schema
  just gen-types

# Generates TypeScript types
gen-types:
  cargo run -p xtask_codegen --features schema -- bindings

# Generates the JSON Schema of the configuration
gen-schema:
  cargo run -p xtask_codegen --features schema -- schema

# Generates code generated files for the linter
gen-analyzer:
  just gen-rules
  just gen-configuration
  just gen-migrate
  just gen-bindings
  just lint-rules
  just format

# Generate and updates the files needed inside the *_analyze crates
gen-rules:
  cargo run -p xtask_codegen -- analyzer

# Generates Baseline data for CSS features from web-features
gen-css-baseline:
  cargo run -p xtask_codegen --features xtask_codegen/external_data -- css-baseline

# Generates CSS keywords from @webref/css
gen-css-keywords:
  cargo run -p xtask_codegen --features xtask_codegen/external_data -- css-keywords

# Generates module-replacements data from e18e
gen-module-replacements:
  cargo run -p xtask_codegen --features xtask_codegen/external_data -- module-replacements

gen-configuration:
  cargo run -p xtask_codegen --features configuration -- configuration

# Generates code for eslint and stylelint migration
gen-migrate:
  cargo run -p xtask_codegen --features configuration -- migrate-eslint
  cargo run -p xtask_codegen --features configuration -- migrate-stylelint

# Exports every lint rule and assist action as JSON, with the upstream rules they implement and the ones Biome deliberately doesn't support. Prints to stdout unless given `--out <path>`.
export-rule-metadata *args='':
  cargo run -p xtask_codegen --features rules_metadata -- rules-metadata {{args}}

# Generates the initial files for all formatter crates
gen-formatter *args='':
  cargo run -p xtask_codegen -- formatter {{args}}

gen-global-types:
  cargo run -p xtask_codegen --features global_types -- global-types

# Generates the Tailwind CSS preset for utility class sorting
[working-directory: 'packages/tailwindcss-config-analyzer']
gen-tw:
  pnpm execute:v4

# Build WASM for bundler target (development)
build-wasm-bundler-dev:
  cargo build --lib --target wasm32-unknown-unknown -p biome_wasm
  wasm-bindgen target/wasm32-unknown-unknown/debug/biome_wasm.wasm \
    --out-dir packages/@biomejs/wasm-bundler \
    --target bundler \
    --typescript

# Build WASM for bundler target (release)
build-wasm-bundler *args='':
  cargo build --lib --target wasm32-unknown-unknown --release -p biome_wasm {{args}}
  wasm-bindgen target/wasm32-unknown-unknown/release/biome_wasm.wasm \
    --out-dir packages/@biomejs/wasm-bundler \
    --no-demangle \
    --target bundler \
    --typescript
  wasm-opt packages/@biomejs/wasm-bundler/biome_wasm_bg.wasm \
    -o packages/@biomejs/wasm-bundler/biome_wasm_bg.wasm \
    -Os \
    -g

# Build WASM for Node.js target (development)
build-wasm-node-dev:
  cargo build --lib --target wasm32-unknown-unknown -p biome_wasm --features unstable
  wasm-bindgen target/wasm32-unknown-unknown/debug/biome_wasm.wasm \
    --out-dir packages/@biomejs/wasm-nodejs \
    --target nodejs \
    --typescript

# Build WASM for Node.js target (release)
build-wasm-node *args='':
  cargo build --lib --target wasm32-unknown-unknown --release -p biome_wasm {{args}}
  wasm-bindgen target/wasm32-unknown-unknown/release/biome_wasm.wasm \
    --out-dir packages/@biomejs/wasm-nodejs \
    --no-demangle \
    --target nodejs \
    --typescript
  wasm-opt packages/@biomejs/wasm-nodejs/biome_wasm_bg.wasm \
    -o packages/@biomejs/wasm-nodejs/biome_wasm_bg.wasm \
    -Os \
    -g

# Build WASM for web target (development)
build-wasm-web-dev:
  cargo build --lib --target wasm32-unknown-unknown -p biome_wasm
  wasm-bindgen target/wasm32-unknown-unknown/debug/biome_wasm.wasm \
    --out-dir packages/@biomejs/wasm-web \
    --target web \
    --typescript

# Build WASM for web target (release)
build-wasm-web *args='':
  cargo build --lib --target wasm32-unknown-unknown --release -p biome_wasm {{args}}
  wasm-bindgen target/wasm32-unknown-unknown/release/biome_wasm.wasm \
    --out-dir packages/@biomejs/wasm-web \
    --no-demangle \
    --target web \
    --typescript
  wasm-opt packages/@biomejs/wasm-web/biome_wasm_bg.wasm \
    -o packages/@biomejs/wasm-web/biome_wasm_bg.wasm \
    -Os \
    -g

# Generates the code of the grammars available in Biome
gen-grammar *args='':
  cargo run -p xtask_codegen -- grammar {{args}}

# Generates the linter documentation and Rust documentation
documentation:
  RUSTDOCFLAGS='-D warnings' cargo documentation

# Creates a new js lint rule with the given name. Name has to be camel case.
new-js-lintrule rulename:
  cargo run -p xtask_codegen -- new-lintrule --kind=js --category=lint --name={{rulename}}
  just gen-analyzer

# Creates a new js assist rule with the given name. Name has to be camel case.
new-js-assistrule rulename:
  cargo run -p xtask_codegen -- new-lintrule --kind=js --category=assist --name={{rulename}}
  just gen-analyzer

# Creates a new json lint rule with the given name. Name has to be camel case.
new-json-lintrule rulename:
  cargo run -p xtask_codegen -- new-lintrule --kind=json --category=lint --name={{rulename}}
  just gen-analyzer

# Creates a new json assist rule with the given name. Name has to be camel case.
new-json-assistrule rulename:
  cargo run -p xtask_codegen -- new-lintrule --kind=json --category=assist --name={{rulename}}
  just gen-analyzer

# Creates a new css lint rule with the given name. Name has to be camel case.
new-css-lintrule rulename:
  cargo run -p xtask_codegen -- new-lintrule --kind=css --category=lint --name={{rulename}}
  just gen-analyzer

# Creates a new css assist rule with the given name. Name has to be camel case.
new-css-assistrule rulename:
  cargo run -p xtask_codegen -- new-lintrule --kind=css --category=assist --name={{rulename}}
  just gen-analyzer

# Creates a new graphql lint rule with the given name. Name has to be camel case.
new-graphql-lintrule rulename:
  cargo run -p xtask_codegen -- new-lintrule --kind=graphql --category=lint --name={{rulename}}
  just gen-analyzer

# Creates a new graphql assist rule with the given name. Name has to be camel case.
new-graphql-assistrule rulename:
  cargo run -p xtask_codegen -- new-lintrule --kind=graphql --category=assist --name={{rulename}}
  just gen-analyzer

# Creates a new markdown lint rule with the given name. Name has to be camel case.
new-markdown-lintrule rulename:
  cargo run -p xtask_codegen -- new-lintrule --kind=markdown --category=lint --name={{rulename}}
  just gen-analyzer

# Creates a new markdown assist rule with the given name. Name has to be camel case.
new-markdown-assistrule rulename:
  cargo run -p xtask_codegen -- new-lintrule --kind=markdown --category=assist --name={{rulename}}
  just gen-analyzer

# Creates a new html lint rule with the given name. Name has to be camel case.
new-html-lintrule rulename:
  cargo run -p xtask_codegen -- new-lintrule --kind=html --category=lint --name={{rulename}}
  just gen-analyzer

# Creates a new html assist rule with the given name. Name has to be camel case.
new-html-assistrule rulename:
  cargo run -p xtask_codegen -- new-lintrule --kind=html --category=assist --name={{rulename}}
  just gen-analyzer

# Creates a new html lint rule with the given name, but targets vue. Name has to be camel case.
new-html-vue-lintrule rulename:
  cargo run -p xtask_codegen -- new-lintrule --kind=html-vue --category=lint --name={{rulename}}
  just gen-analyzer

# Creates a new yaml lint rule with the given name. Name has to be camel case.
new-yaml-lintrule rulename:
  cargo run -p xtask_codegen -- new-lintrule --kind=yaml --category=lint --name={{rulename}}
  just gen-analyzer

# Creates a new yaml assist rule with the given name. Name has to be camel case.
new-yaml-assistrule rulename:
  cargo run -p xtask_codegen -- new-lintrule --kind=yaml --category=assist --name={{rulename}}
  just gen-analyzer

# Promotes a rule from the nursery group to a new group
move-rule rulename group:
  cargo run -p xtask_codegen -- move-rule --group={{group}} --name={{rulename}}
  cargo run -p xtask_codegen -- analyzer

# Promotes nursery lint rules described by a JSON manifest
promote-rules manifest:
  cargo run -p xtask_codegen -- promote-rules --manifest={{manifest}}

# Format Rust files and TOML files
format:
	cargo format
	pnpm format

[unix]
_touch file:
  touch {{file}}

[windows]
_touch file:
  powershell -Command "(Get-Item {{file}}).LastWriteTime = Get-Date"

# Run tests of all crates
test:
	cargo test --no-fail-fast

# Run tests for the crate passed as argument e.g. just test-crate biome_cli
test-crate name:
	cargo test -p {{name}} --no-fail-fast

# Run doc tests
test-doc:
	cargo test --doc

# Run CommonMark conformance tests for the markdown parser
test-markdown-conformance:
	cargo run -p xtask_coverage -- --suites=markdown/commonmark

# Generate differential fuzz corpus for the markdown parser using commonmark.js
# Requires `pnpm install` from the repo root (commonmark is a root devDependency).
fuzz-markdown-generate count="1000" seed="42":
	node crates/biome_markdown_parser/tests/fuzz_generate_corpus.cjs \
		--count={{count}} --seed={{seed}} \
		--output=crates/biome_markdown_parser/tests/fuzz_corpus/corpus.jsonl

# Run differential fuzzer comparing Biome markdown output against commonmark.js
# Runs the checked-in seed corpus plus any generated corpus.jsonl
fuzz-markdown-differential:
	#!/usr/bin/env bash
	set -euo pipefail
	CORPUS="$(pwd)/crates/biome_markdown_parser/tests/fuzz_corpus/corpus.jsonl"
	if [ -f "$CORPUS" ]; then
		FUZZ_CORPUS="$CORPUS" cargo test -p biome_markdown_parser --test fuzz_differential -- --ignored --nocapture
	else
		cargo test -p biome_markdown_parser --test fuzz_differential -- --ignored --nocapture
	fi

# Update the CommonMark spec.json to a specific version
update-commonmark-spec version:
	./scripts/update-commonmark-spec.sh {{version}}

# Tests a lint rule. The name of the rule needs to be camel case
test-lintrule rulename:
  just test-js-lintrule {{rulename}}
  just test-json-lintrule {{rulename}}
  just test-css-lintrule {{rulename}}
  just test-graphql-lintrule {{rulename}}
  just test-html-lintrule {{rulename}}
  just test-markdown-lintrule {{rulename}}
  just test-yaml-lintrule {{rulename}}

# Test a js lint rule. The name of the rule needs to be camel case
test-js-lintrule rulename:
  just _touch crates/biome_js_analyze/tests/spec_tests.rs
  cargo test -p biome_js_analyze -- {{snakecase(rulename)}} --show-output

# Test a json lint rule. The name of the rule needs to be camel case
test-json-lintrule rulename:
  just _touch crates/biome_json_analyze/tests/spec_tests.rs
  cargo test -p biome_json_analyze -- {{snakecase(rulename)}} --show-output

# Test a css lint rule. The name of the rule needs to be camel case
test-css-lintrule rulename:
  just _touch crates/biome_css_analyze/tests/spec_tests.rs
  cargo test -p biome_css_analyze -- {{snakecase(rulename)}} --show-output

# Test a graphql lint rule. The name of the rule needs to be camel case
test-graphql-lintrule rulename:
  just _touch crates/biome_graphql_analyze/tests/spec_tests.rs
  cargo test -p biome_graphql_analyze -- {{snakecase(rulename)}} --show-output

# Test a html lint rule. The name of the rule needs to be camel case
test-html-lintrule rulename:
  just _touch crates/biome_html_analyze/tests/spec_tests.rs
  cargo test -p biome_html_analyze -- {{snakecase(rulename)}} --show-output

# Test a markdown lint rule. The name of the rule needs to be camel case
test-markdown-lintrule rulename:
  just _touch crates/biome_markdown_analyze/tests/spec_tests.rs
  cargo test -p biome_markdown_analyze -- {{snakecase(rulename)}} --show-output

# Test a yaml lint rule. The name of the rule needs to be camel case
test-yaml-lintrule rulename:
  just _touch crates/biome_yaml_analyze/tests/spec_tests.rs
  cargo test -p biome_yaml_analyze -- {{snakecase(rulename)}} --show-output

# Tests a lint rule. The name of the rule needs to be camel case
test-transformation name:
  just _touch crates/biome_js_transform/tests/spec_tests.rs
  cargo test -p biome_js_transform -- {{snakecase(name)}} --show-output

# Run the quick_test for the given package.
test-quick package:
  cargo test -p {{package}} --test quick_test -- quick_test --nocapture --ignored

# Alias for `cargo lint`, it runs clippy on the whole codebase
lint:
  cargo lint

# Checks if the docs of the lint rules follow Biome's requirements
lint-rules:
  cargo run -p rules_check

# When you finished coding, run this command to run the same commands in the CI.
ready:
  git diff --exit-code --quiet
  just gen-all
  just documentation
  #just format # format is already run in `just gen-all`
  just lint
  just test
  just test-doc
  git diff --exit-code --quiet

# Creates a new changeset for the final changelog
new-changeset:
  pnpm changeset

# Creates a new changeset without interaction
new-changeset-empty:
  pnpm changeset --empty

# Create new crate
new-crate name:
  cargo new crates/{{name}} --lib
