|
| 1 | +# ast-grep rules for migrating .oxlintrc configuration to vite.config.ts |
| 2 | +# |
| 3 | +# These rules merge oxlint configuration from .oxlintrc into the defineConfig |
| 4 | +# call in vite.config.ts, following the vite-plus unified configuration approach. |
| 5 | +# |
| 6 | +# Usage: |
| 7 | +# The migration script should: |
| 8 | +# 1. Read .oxlintrc JSON content |
| 9 | +# 2. Convert it to TypeScript object literal format |
| 10 | +# 3. Replace __OXLINT_CONFIG__ placeholder with the actual config |
| 11 | +# 4. Run: sg scan -r oxlint-config.yaml vite.config.ts -U |
| 12 | +# |
| 13 | +# Example workflow: |
| 14 | +# |
| 15 | +# # Step 1: Create rule file with actual oxlint config |
| 16 | +# cat > /tmp/migrate-oxlint.yaml << 'EOF' |
| 17 | +# --- |
| 18 | +# id: merge-oxlint-config |
| 19 | +# language: TypeScript |
| 20 | +# rule: |
| 21 | +# pattern: | |
| 22 | +# defineConfig({ |
| 23 | +# $$$CONFIG |
| 24 | +# }) |
| 25 | +# fix: |- |
| 26 | +# defineConfig({ |
| 27 | +# $$$CONFIG |
| 28 | +# lint: { |
| 29 | +# rules: { |
| 30 | +# 'no-unused-vars': 'error', |
| 31 | +# 'no-console': 'warn', |
| 32 | +# }, |
| 33 | +# ignorePatterns: ['dist', 'node_modules'], |
| 34 | +# }, |
| 35 | +# }) |
| 36 | +# EOF |
| 37 | +# |
| 38 | +# # Step 2: Run ast-grep |
| 39 | +# sg scan -r /tmp/migrate-oxlint.yaml vite.config.ts -U |
| 40 | +# |
| 41 | +# # Step 3: Remove old config file |
| 42 | +# rm .oxlintrc |
| 43 | +# |
| 44 | +# For programmatic usage, use the vite_migration Rust crate which provides |
| 45 | +# proper JSON to TypeScript conversion. |
| 46 | + |
| 47 | +# Rule 1: Add lint config to defineConfig with existing properties |
| 48 | +# Matches: |
| 49 | +# defineConfig({ |
| 50 | +# plugins: [...], |
| 51 | +# server: {...}, |
| 52 | +# }) |
| 53 | +# Result: |
| 54 | +# defineConfig({ |
| 55 | +# plugins: [...], |
| 56 | +# server: {...}, |
| 57 | +# lint: {...}, |
| 58 | +# }) |
| 59 | +--- |
| 60 | +id: add-lint-config-to-defineconfig |
| 61 | +language: TypeScript |
| 62 | +severity: info |
| 63 | +message: 'Add lint configuration to vite.config.ts' |
| 64 | +note: 'Migrating oxlint configuration from .oxlintrc to vite.config.ts' |
| 65 | +rule: |
| 66 | + pattern: | |
| 67 | + defineConfig({ |
| 68 | + $$$EXISTING_CONFIG |
| 69 | + }) |
| 70 | +fix: |- |
| 71 | + defineConfig({ |
| 72 | + $$$EXISTING_CONFIG |
| 73 | + // lint configuration (merged from .oxlintrc) |
| 74 | + lint: __OXLINT_CONFIG__, |
| 75 | + }) |
| 76 | +files: |
| 77 | + - '**/vite.config.ts' |
| 78 | + - '**/vite.config.mts' |
| 79 | + - '**/vite.config.js' |
| 80 | + - '**/vite.config.mjs' |
| 81 | +ignores: |
| 82 | + - '**/node_modules/**' |
| 83 | + - '**/dist/**' |
| 84 | + |
| 85 | +# Rule 2: Detect defineConfig with function callback (warning only) |
| 86 | +# This pattern requires manual migration |
| 87 | +# Matches: defineConfig((env) => ({ ... })) |
| 88 | +--- |
| 89 | +id: detect-function-defineconfig |
| 90 | +language: TypeScript |
| 91 | +severity: warning |
| 92 | +message: 'defineConfig uses a function callback - manual migration required' |
| 93 | +note: | |
| 94 | + The defineConfig uses a function callback pattern which cannot be |
| 95 | + automatically migrated. Please manually add the lint configuration: |
| 96 | +
|
| 97 | + export default defineConfig((env) => ({ |
| 98 | + ...existingConfig, |
| 99 | + lint: { |
| 100 | + rules: { |
| 101 | + // your rules from .oxlintrc |
| 102 | + }, |
| 103 | + }, |
| 104 | + })); |
| 105 | +rule: |
| 106 | + # Using explicit arrow function pattern to match callback style |
| 107 | + pattern: defineConfig(($PARAMS) => $BODY) |
| 108 | +files: |
| 109 | + - '**/vite.config.ts' |
| 110 | + - '**/vite.config.mts' |
| 111 | + - '**/vite.config.js' |
| 112 | + - '**/vite.config.mjs' |
| 113 | +ignores: |
| 114 | + - '**/node_modules/**' |
| 115 | + - '**/dist/**' |
0 commit comments