|
| 1 | +/** |
| 2 | + * Regression tests for the JS-fallback parse path. |
| 3 | + * |
| 4 | + * The JS path runs when @typescript-eslint/parser isn't available (or when |
| 5 | + * `useBabel: true` is set). Historically this path produced an AST that |
| 6 | + * ESLint refused — `oxc-parser` only emits `start`/`end` byte offsets, so |
| 7 | + * `Program.loc` and `Program.range` were missing and `Program.tokens` was |
| 8 | + * empty. Lint of any .gjs file blew up with `TypeError: AST is missing |
| 9 | + * location information.`, and rules that walk tokens (e.g. no-dupe-args) |
| 10 | + * crashed even after that. |
| 11 | + * |
| 12 | + * These tests force the JS path via `useBabel: true` and pin the contract |
| 13 | + * at two layers: the AST shape the parser returns, and a real Linter pass |
| 14 | + * driving an ESLint rule that walks tokens. Both fail under oxc. |
| 15 | + * |
| 16 | + * Switch back to oxc once https://github.com/oxc-project/oxc/issues/10307 |
| 17 | + * lands native loc support. |
| 18 | + */ |
| 19 | + |
| 20 | +import { describe, expect, it } from 'vitest'; |
| 21 | +import { Linter } from 'eslint'; |
| 22 | +import { parseForESLint } from '../src/parser/gjs-gts-parser.js'; |
| 23 | + |
| 24 | +describe('JS path (useBabel) — AST shape ESLint requires', () => { |
| 25 | + const code = [ |
| 26 | + "import currentYear from './helper.js';", |
| 27 | + '', |
| 28 | + '<template>', |
| 29 | + ' <p>{{(currentYear)}}, hi</p>', |
| 30 | + '</template>', |
| 31 | + ].join('\n'); |
| 32 | + |
| 33 | + const result = parseForESLint(code, { |
| 34 | + filePath: 'fixture.gjs', |
| 35 | + useBabel: true, |
| 36 | + }); |
| 37 | + |
| 38 | + it('Program node carries loc, range, tokens, and comments', () => { |
| 39 | + const ast = result.ast; |
| 40 | + expect(ast.type).toBe('Program'); |
| 41 | + expect(ast.loc).toBeDefined(); |
| 42 | + expect(ast.loc.start).toMatchObject({ line: expect.any(Number), column: expect.any(Number) }); |
| 43 | + expect(ast.loc.end).toMatchObject({ line: expect.any(Number), column: expect.any(Number) }); |
| 44 | + expect(ast.range).toEqual([expect.any(Number), expect.any(Number)]); |
| 45 | + expect(Array.isArray(ast.tokens)).toBe(true); |
| 46 | + expect(ast.tokens.length).toBeGreaterThan(0); |
| 47 | + expect(Array.isArray(ast.comments)).toBe(true); |
| 48 | + }); |
| 49 | + |
| 50 | + it('inner JS nodes carry loc and range (rules read these everywhere)', () => { |
| 51 | + const importDecl = result.ast.body[0]; |
| 52 | + expect(importDecl.type).toBe('ImportDeclaration'); |
| 53 | + expect(importDecl.loc).toBeDefined(); |
| 54 | + expect(importDecl.range).toEqual([expect.any(Number), expect.any(Number)]); |
| 55 | + }); |
| 56 | + |
| 57 | + it('produces a scope manager that resolves the import', () => { |
| 58 | + expect(result.scopeManager).toBeDefined(); |
| 59 | + const moduleScope = result.scopeManager.scopes.find((s) => s.type === 'module'); |
| 60 | + expect(moduleScope).toBeDefined(); |
| 61 | + expect(moduleScope.set.has('currentYear')).toBe(true); |
| 62 | + }); |
| 63 | +}); |
| 64 | + |
| 65 | +describe('JS path (useBabel) — end-to-end Linter pass', () => { |
| 66 | + function makeLinter() { |
| 67 | + const linter = new Linter(); |
| 68 | + linter.defineParser('ember-eslint-parser', { |
| 69 | + parseForESLint: (code, options) => parseForESLint(code, { ...options, useBabel: true }), |
| 70 | + }); |
| 71 | + return linter; |
| 72 | + } |
| 73 | + |
| 74 | + it('lints a .gjs file without throwing on missing loc/tokens', () => { |
| 75 | + const linter = makeLinter(); |
| 76 | + const code = [ |
| 77 | + "import currentYear from './helper.js';", |
| 78 | + '', |
| 79 | + '<template>', |
| 80 | + ' <p>{{(currentYear)}}, hi</p>', |
| 81 | + '</template>', |
| 82 | + ].join('\n'); |
| 83 | + |
| 84 | + // `no-dupe-args` is a core rule that calls |
| 85 | + // `astUtils.getOpeningParenOfParams(...).loc.start`, which reaches |
| 86 | + // through the token stream. Under the oxc path this throws because |
| 87 | + // `program.tokens` is empty. Enabling the rule keeps that surface |
| 88 | + // covered even if a future change quietly restores `loc` but still |
| 89 | + // ships an empty token array. |
| 90 | + const messages = linter.verify( |
| 91 | + code, |
| 92 | + { |
| 93 | + parser: 'ember-eslint-parser', |
| 94 | + parserOptions: { ecmaVersion: 2022, sourceType: 'module' }, |
| 95 | + rules: { 'no-dupe-args': 'error' }, |
| 96 | + }, |
| 97 | + { filename: 'fixture.gjs' } |
| 98 | + ); |
| 99 | + |
| 100 | + // The fatal "AST is missing location information" / "Cannot read |
| 101 | + // properties of null (reading 'loc')" errors land here as |
| 102 | + // `fatal: true` messages — assert there are none. |
| 103 | + const fatal = messages.filter((m) => m.fatal); |
| 104 | + expect(fatal).toEqual([]); |
| 105 | + }); |
| 106 | + |
| 107 | + it('marks template-only references as used (scope wiring is intact)', () => { |
| 108 | + const linter = makeLinter(); |
| 109 | + const code = [ |
| 110 | + "import currentYear from './helper.js';", |
| 111 | + '', |
| 112 | + '<template>', |
| 113 | + ' <p>{{(currentYear)}}, hi</p>', |
| 114 | + '</template>', |
| 115 | + ].join('\n'); |
| 116 | + |
| 117 | + const messages = linter.verify( |
| 118 | + code, |
| 119 | + { |
| 120 | + parser: 'ember-eslint-parser', |
| 121 | + parserOptions: { ecmaVersion: 2022, sourceType: 'module' }, |
| 122 | + rules: { 'no-unused-vars': 'error' }, |
| 123 | + }, |
| 124 | + { filename: 'fixture.gjs' } |
| 125 | + ); |
| 126 | + |
| 127 | + const unused = messages.filter((m) => m.ruleId === 'no-unused-vars'); |
| 128 | + expect(unused).toEqual([]); |
| 129 | + }); |
| 130 | +}); |
0 commit comments