|
| 1 | +const eslintScope = require('eslint-scope'); |
| 2 | +const DocumentLines = require('../utils/document'); |
| 3 | +const { processGlimmerTemplate, buildGlimmerVisitorKeys } = require('./transforms'); |
| 4 | + |
| 5 | +/** |
| 6 | + * implements https://eslint.org/docs/latest/extend/custom-parsers |
| 7 | + * for Handlebars (.hbs) template files. |
| 8 | + * |
| 9 | + * The entire file is treated as a Glimmer template. |
| 10 | + * All locals not defined in the template are assumed to be defined |
| 11 | + * (no no-undef errors for template identifiers). |
| 12 | + */ |
| 13 | + |
| 14 | +/** |
| 15 | + * @type {import('eslint').ParserModule} |
| 16 | + */ |
| 17 | +module.exports = { |
| 18 | + meta: { |
| 19 | + name: 'ember-eslint-parser/hbs', |
| 20 | + version: '*', |
| 21 | + }, |
| 22 | + |
| 23 | + parseForESLint(code, options) { |
| 24 | + const filePath = (options && options.filePath) || '<hbs>'; |
| 25 | + const codeLines = new DocumentLines(code); |
| 26 | + |
| 27 | + let result; |
| 28 | + try { |
| 29 | + result = processGlimmerTemplate({ |
| 30 | + templateContent: code, |
| 31 | + codeLines, |
| 32 | + templateRange: [0, code.length], |
| 33 | + }); |
| 34 | + } catch (e) { |
| 35 | + // Transform glimmer parse error to ESLint-compatible error |
| 36 | + const loc = e.location || (e.hash && e.hash.loc); |
| 37 | + if (loc && loc.start) { |
| 38 | + const err = Object.assign(new SyntaxError(e.message), { |
| 39 | + lineNumber: loc.start.line, |
| 40 | + column: loc.start.column, |
| 41 | + index: codeLines.positionToOffset(loc.start), |
| 42 | + fileName: filePath, |
| 43 | + }); |
| 44 | + throw err; |
| 45 | + } |
| 46 | + throw e; |
| 47 | + } |
| 48 | + |
| 49 | + const { ast: templateNode, comments } = result; |
| 50 | + |
| 51 | + // Wrap in a synthetic Program node (required by ESLint) |
| 52 | + const program = { |
| 53 | + type: 'Program', |
| 54 | + body: [templateNode], |
| 55 | + tokens: templateNode.tokens, |
| 56 | + comments, |
| 57 | + range: [0, code.length], |
| 58 | + start: 0, |
| 59 | + end: code.length, |
| 60 | + loc: { |
| 61 | + start: { line: 1, column: 0 }, |
| 62 | + end: codeLines.offsetToPosition(code.length), |
| 63 | + }, |
| 64 | + }; |
| 65 | + |
| 66 | + // Build visitor keys: Program + all Glimmer node types |
| 67 | + const visitorKeys = { Program: ['body'], ...buildGlimmerVisitorKeys() }; |
| 68 | + |
| 69 | + // Create an empty scope manager. |
| 70 | + // For HBS, all locals are assumed to be defined at runtime, |
| 71 | + // so we don't track variable references (no no-undef errors). |
| 72 | + const scopeManager = eslintScope.analyze( |
| 73 | + { |
| 74 | + type: 'Program', |
| 75 | + body: [], |
| 76 | + range: [0, code.length], |
| 77 | + loc: program.loc, |
| 78 | + }, |
| 79 | + { range: true } |
| 80 | + ); |
| 81 | + |
| 82 | + return { |
| 83 | + ast: program, |
| 84 | + scopeManager, |
| 85 | + visitorKeys, |
| 86 | + services: {}, |
| 87 | + }; |
| 88 | + }, |
| 89 | +}; |
0 commit comments