|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { handleExtendsScope, resolveRelativeExtendsPaths } from './extends.js'; |
| 3 | +import { OxlintConfig } from './types.js'; |
| 4 | + |
| 5 | +describe('handleExtendsScope', () => { |
| 6 | + it('should handle empty extends configs', () => { |
| 7 | + const extendsConfigs: OxlintConfig[] = []; |
| 8 | + const config: OxlintConfig = { |
| 9 | + plugins: ['react'], |
| 10 | + categories: { correctness: 'warn' }, |
| 11 | + rules: { eqeqeq: 'error' }, |
| 12 | + }; |
| 13 | + handleExtendsScope(extendsConfigs, config); |
| 14 | + expect(config).toEqual(config); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should merge extends configs', () => { |
| 18 | + const extendsConfigs: OxlintConfig[] = [ |
| 19 | + { |
| 20 | + plugins: ['react', 'unicorn'], |
| 21 | + categories: { correctness: 'error' }, |
| 22 | + rules: { rule1: 'error' }, |
| 23 | + }, |
| 24 | + ]; |
| 25 | + const config: OxlintConfig = { |
| 26 | + categories: { correctness: 'warn' }, |
| 27 | + rules: { rule3: 'warn' }, |
| 28 | + }; |
| 29 | + handleExtendsScope(extendsConfigs, config); |
| 30 | + expect(config).toEqual({ |
| 31 | + plugins: ['react', 'unicorn'], |
| 32 | + categories: config.categories, |
| 33 | + rules: { rule1: 'error', rule3: 'warn' }, |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should merge extends and de duplicate plugins and rules', () => { |
| 38 | + const extendsConfigs: OxlintConfig[] = [ |
| 39 | + { |
| 40 | + plugins: ['react', 'typescript'], |
| 41 | + categories: { correctness: 'error' }, |
| 42 | + rules: { rule1: 'error', rule2: 'error' }, |
| 43 | + }, |
| 44 | + ]; |
| 45 | + const config: OxlintConfig = { |
| 46 | + plugins: ['react', 'unicorn'], |
| 47 | + categories: { correctness: 'warn' }, |
| 48 | + rules: { rule1: 'warn' }, |
| 49 | + }; |
| 50 | + handleExtendsScope(extendsConfigs, config); |
| 51 | + expect(config).toEqual({ |
| 52 | + plugins: ['react', 'typescript', 'unicorn'], |
| 53 | + categories: config.categories, |
| 54 | + rules: { rule1: 'warn', rule2: 'error' }, |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should merge multiple extends configs', () => { |
| 59 | + const extendsConfigs: OxlintConfig[] = [ |
| 60 | + { |
| 61 | + plugins: ['react', 'unicorn'], |
| 62 | + categories: { correctness: 'error' }, |
| 63 | + rules: { rule1: 'error', rule2: 'error' }, |
| 64 | + }, |
| 65 | + { |
| 66 | + plugins: ['typescript'], |
| 67 | + overrides: [{ files: ['*.ts'], rules: { rule3: 'error' } }], |
| 68 | + }, |
| 69 | + ]; |
| 70 | + const config: OxlintConfig = { |
| 71 | + plugins: ['react', 'vitest'], |
| 72 | + categories: { correctness: 'warn' }, |
| 73 | + rules: { rule1: 'warn' }, |
| 74 | + }; |
| 75 | + handleExtendsScope(extendsConfigs, config); |
| 76 | + expect(config).toEqual({ |
| 77 | + plugins: ['typescript', 'react', 'unicorn', 'vitest'], |
| 78 | + categories: config.categories, |
| 79 | + rules: { rule1: 'warn', rule2: 'error' }, |
| 80 | + overrides: [{ files: ['*.ts'], rules: { rule3: 'error' } }], |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should merge multiple extends configs with multiple overrides', () => { |
| 85 | + const extendsConfigs: OxlintConfig[] = [ |
| 86 | + { |
| 87 | + plugins: ['react', 'unicorn'], |
| 88 | + categories: { correctness: 'error' }, |
| 89 | + rules: { rule1: 'error', rule2: 'error' }, |
| 90 | + }, |
| 91 | + { |
| 92 | + plugins: ['typescript'], |
| 93 | + overrides: [{ files: ['*.ts'], rules: { rule3: 'error' } }], |
| 94 | + }, |
| 95 | + ]; |
| 96 | + const config: OxlintConfig = { |
| 97 | + plugins: ['react', 'vitest'], |
| 98 | + categories: { correctness: 'warn' }, |
| 99 | + rules: { rule1: 'warn' }, |
| 100 | + overrides: [{ files: ['*.spec.ts'], rules: { rule4: 'error' } }], |
| 101 | + }; |
| 102 | + handleExtendsScope(extendsConfigs, config); |
| 103 | + expect(config).toEqual({ |
| 104 | + plugins: ['typescript', 'react', 'unicorn', 'vitest'], |
| 105 | + categories: config.categories, |
| 106 | + rules: { rule1: 'warn', rule2: 'error' }, |
| 107 | + overrides: [ |
| 108 | + { files: ['*.ts'], rules: { rule3: 'error' } }, |
| 109 | + { files: ['*.spec.ts'], rules: { rule4: 'error' } }, |
| 110 | + ], |
| 111 | + }); |
| 112 | + }); |
| 113 | +}); |
| 114 | + |
| 115 | +describe('resolveRelativeExtendsPaths', () => { |
| 116 | + it('should resolve relative paths', () => { |
| 117 | + const config: OxlintConfig = { |
| 118 | + extends: [ |
| 119 | + './extends1.json', |
| 120 | + './folder/extends2.json', |
| 121 | + '../parent/extends3.json', |
| 122 | + ], |
| 123 | + __misc: { |
| 124 | + filePath: '/root/of/the/file/test-config.json', |
| 125 | + }, |
| 126 | + }; |
| 127 | + resolveRelativeExtendsPaths(config); |
| 128 | + |
| 129 | + expect(config.extends).toEqual([ |
| 130 | + '/root/of/the/file/extends1.json', |
| 131 | + '/root/of/the/file/folder/extends2.json', |
| 132 | + '/root/of/the/parent/extends3.json', |
| 133 | + ]); |
| 134 | + }); |
| 135 | +}); |
0 commit comments