|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Regression coverage for `{{! eslint-disable-* }}` directives inside |
| 5 | + * `<template>` blocks in .gjs/.gts files. |
| 6 | + * |
| 7 | + * ESLint's inline-config scanner reads `Program.comments`. For .gjs/.gts the |
| 8 | + * template is parsed by ember-eslint-parser (via ember-estree). Upstream |
| 9 | + * ember-estree 0.4.3 (NullVoxPopuli/ember-estree#31) kept Glimmer comment |
| 10 | + * nodes inside the template body and stopped mirroring them into |
| 11 | + * `Program.comments`, which silently dropped template-level directives. |
| 12 | + * |
| 13 | + * The current lockfile pins [email protected] so these tests pass today. |
| 14 | + * They exist to trip loudly if a future bump re-introduces the regression |
| 15 | + * without a corresponding fix in ember-eslint-parser / ember-estree. |
| 16 | + * The pure-hbs parser path has always behaved correctly and is exercised |
| 17 | + * here as a positive control. |
| 18 | + */ |
| 19 | + |
| 20 | +const { Linter } = require('eslint'); |
| 21 | +const rule = require('../../lib/rules/template-no-unnecessary-concat'); |
| 22 | + |
| 23 | +const RULE_ID = 'ember/template-no-unnecessary-concat'; |
| 24 | + |
| 25 | +function makeLinter(parserName) { |
| 26 | + const linter = new Linter(); |
| 27 | + linter.defineParser('ember-eslint-parser', require('ember-eslint-parser')); |
| 28 | + linter.defineParser('ember-eslint-parser/hbs', require('ember-eslint-parser/hbs')); |
| 29 | + linter.defineRule(RULE_ID, rule); |
| 30 | + return { |
| 31 | + verify(code, filename) { |
| 32 | + return linter.verify( |
| 33 | + code, |
| 34 | + { |
| 35 | + parser: parserName, |
| 36 | + parserOptions: { ecmaVersion: 2022, sourceType: 'module' }, |
| 37 | + rules: { [RULE_ID]: 'error' }, |
| 38 | + }, |
| 39 | + { filename } |
| 40 | + ); |
| 41 | + }, |
| 42 | + }; |
| 43 | +} |
| 44 | + |
| 45 | +describe('eslint-disable directives inside <template> — .gts (gjs-gts-parser)', () => { |
| 46 | + const linter = makeLinter('ember-eslint-parser'); |
| 47 | + |
| 48 | + it('rule fires without a directive (sanity check)', () => { |
| 49 | + const code = ['<template>', ' <li aria-current="{{foo}}"></li>', '</template>'].join('\n'); |
| 50 | + const messages = linter.verify(code, 'test.gts'); |
| 51 | + const ruleMsgs = messages.filter((m) => m.ruleId === RULE_ID); |
| 52 | + expect(ruleMsgs).toHaveLength(1); |
| 53 | + }); |
| 54 | + |
| 55 | + it('{{! eslint-disable-next-line ember/rule }} suppresses the rule on the next line', () => { |
| 56 | + const code = [ |
| 57 | + '<template>', |
| 58 | + ' <li', |
| 59 | + ' {{! eslint-disable-next-line ember/template-no-unnecessary-concat }}', |
| 60 | + ' aria-current="{{foo}}"', |
| 61 | + ' ></li>', |
| 62 | + '</template>', |
| 63 | + ].join('\n'); |
| 64 | + const messages = linter.verify(code, 'test.gts'); |
| 65 | + const ruleMsgs = messages.filter((m) => m.ruleId === RULE_ID); |
| 66 | + expect(ruleMsgs).toEqual([]); |
| 67 | + }); |
| 68 | + |
| 69 | + it('long-form {{!-- eslint-disable-next-line ember/rule --}} suppresses the rule', () => { |
| 70 | + const code = [ |
| 71 | + '<template>', |
| 72 | + ' <li', |
| 73 | + ' {{!-- eslint-disable-next-line ember/template-no-unnecessary-concat --}}', |
| 74 | + ' aria-current="{{foo}}"', |
| 75 | + ' ></li>', |
| 76 | + '</template>', |
| 77 | + ].join('\n'); |
| 78 | + const messages = linter.verify(code, 'test.gts'); |
| 79 | + const ruleMsgs = messages.filter((m) => m.ruleId === RULE_ID); |
| 80 | + expect(ruleMsgs).toEqual([]); |
| 81 | + }); |
| 82 | +}); |
| 83 | + |
| 84 | +describe('eslint-disable directives inside <template> — .hbs (hbs-parser) [positive control]', () => { |
| 85 | + const linter = makeLinter('ember-eslint-parser/hbs'); |
| 86 | + |
| 87 | + it('rule fires without a directive (sanity check)', () => { |
| 88 | + const code = '<li aria-current="{{foo}}"></li>\n'; |
| 89 | + const messages = linter.verify(code, 'test.hbs'); |
| 90 | + const ruleMsgs = messages.filter((m) => m.ruleId === RULE_ID); |
| 91 | + expect(ruleMsgs).toHaveLength(1); |
| 92 | + }); |
| 93 | + |
| 94 | + it('{{! eslint-disable-next-line ember/rule }} suppresses the rule on the next line', () => { |
| 95 | + const code = [ |
| 96 | + '<li', |
| 97 | + ' {{! eslint-disable-next-line ember/template-no-unnecessary-concat }}', |
| 98 | + ' aria-current="{{foo}}"', |
| 99 | + '></li>', |
| 100 | + ].join('\n'); |
| 101 | + const messages = linter.verify(code, 'test.hbs'); |
| 102 | + const ruleMsgs = messages.filter((m) => m.ruleId === RULE_ID); |
| 103 | + expect(ruleMsgs).toEqual([]); |
| 104 | + }); |
| 105 | +}); |
0 commit comments