|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import { describe, it, expect, beforeEach } from 'vitest'; |
| 5 | +import { RuleTester } from 'eslint'; |
| 6 | +import type { JSRuleDefinition } from 'eslint'; |
| 7 | +import htmlParser from '@html-eslint/parser'; |
| 8 | +import noDeprecatedAttributeValue, { DEPRECATED_ATTRIBUTE_VALUES } from './no-deprecated-global-attribute-value.js'; |
| 9 | +import noDeprecatedGlobalAttributeValue from './no-deprecated-global-attribute-value.js'; |
| 10 | + |
| 11 | +const rule = noDeprecatedGlobalAttributeValue as unknown as JSRuleDefinition; |
| 12 | + |
| 13 | +const MIGRATE_PROMPT_DEPRECATIONS: { attribute: string; before: string; after: string }[] = [ |
| 14 | + { attribute: 'nve-text', before: 'eyebrow', after: 'label sm' }, |
| 15 | + { attribute: 'nve-layout', before: 'grow', after: 'full' } |
| 16 | +]; |
| 17 | + |
| 18 | +describe('noDeprecatedGlobalAttributeValue', () => { |
| 19 | + let tester: RuleTester; |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + tester = new RuleTester({ |
| 23 | + languageOptions: { |
| 24 | + parser: htmlParser, |
| 25 | + parserOptions: { |
| 26 | + frontmatter: true |
| 27 | + } |
| 28 | + } |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should define rule metadata', () => { |
| 33 | + expect(noDeprecatedAttributeValue.meta).toBeDefined(); |
| 34 | + expect(noDeprecatedAttributeValue.meta.type).toBe('problem'); |
| 35 | + expect(noDeprecatedAttributeValue.meta.fixable).toBe('code'); |
| 36 | + expect(noDeprecatedAttributeValue.meta.hasSuggestions).toBe(true); |
| 37 | + expect(noDeprecatedAttributeValue.meta.docs.recommended).toBe(true); |
| 38 | + expect(noDeprecatedAttributeValue.meta.docs.url).toContain('/docs/lint/'); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should cover every deprecation documented in the migrate prompt', () => { |
| 42 | + MIGRATE_PROMPT_DEPRECATIONS.forEach(({ attribute, before, after }) => { |
| 43 | + expect(DEPRECATED_ATTRIBUTE_VALUES[attribute]?.[before]).toBe(after); |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should allow non-deprecated values', () => { |
| 48 | + tester.run('non-deprecated values', rule, { |
| 49 | + valid: [ |
| 50 | + '<div nve-text="body"></div>', |
| 51 | + '<div nve-text="label sm"></div>', |
| 52 | + '<div nve-layout="row"></div>', |
| 53 | + '<div nve-layout="full"></div>', |
| 54 | + '<div></div>' |
| 55 | + ], |
| 56 | + invalid: [] |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should ignore template binding expressions', () => { |
| 61 | + tester.run('template binding expressions', rule, { |
| 62 | + valid: [ |
| 63 | + '<div nve-text="${value}"></div>', |
| 64 | + '<div nve-text="{{ value }}"></div>', |
| 65 | + '<div nve-layout="{value}"></div>' |
| 66 | + ], |
| 67 | + invalid: [] |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should report and auto-fix every migrate-prompt deprecation', () => { |
| 72 | + tester.run('migrate prompt deprecations', rule, { |
| 73 | + valid: [], |
| 74 | + invalid: MIGRATE_PROMPT_DEPRECATIONS.map(({ attribute, before, after }) => ({ |
| 75 | + code: `<div ${attribute}="${before}"></div>`, |
| 76 | + output: `<div ${attribute}="${after}"></div>`, |
| 77 | + errors: [ |
| 78 | + { |
| 79 | + messageId: 'unexpected-deprecated-global-attribute-value', |
| 80 | + data: { attribute, value: before, alternative: after }, |
| 81 | + suggestions: [ |
| 82 | + { |
| 83 | + messageId: 'suggest-replace-deprecated-global-attribute-value', |
| 84 | + data: { value: before, alternative: after }, |
| 85 | + output: `<div ${attribute}="${after}"></div>` |
| 86 | + } |
| 87 | + ] |
| 88 | + } |
| 89 | + ] |
| 90 | + })) |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should rewrite deprecated tokens within a multi-token value', () => { |
| 95 | + tester.run('multi-token replacement', rule, { |
| 96 | + valid: [], |
| 97 | + invalid: [ |
| 98 | + { |
| 99 | + code: '<div nve-layout="grow column"></div>', |
| 100 | + output: '<div nve-layout="full column"></div>', |
| 101 | + errors: [ |
| 102 | + { |
| 103 | + messageId: 'unexpected-deprecated-global-attribute-value', |
| 104 | + data: { attribute: 'nve-layout', value: 'grow column', alternative: 'full column' }, |
| 105 | + suggestions: [ |
| 106 | + { |
| 107 | + messageId: 'suggest-replace-deprecated-global-attribute-value', |
| 108 | + data: { value: 'grow column', alternative: 'full column' }, |
| 109 | + output: '<div nve-layout="full column"></div>' |
| 110 | + } |
| 111 | + ] |
| 112 | + } |
| 113 | + ] |
| 114 | + } |
| 115 | + ] |
| 116 | + }); |
| 117 | + }); |
| 118 | +}); |
0 commit comments