From 39930fab1a2ebeab960d1c0c4bc1afbdd151a73c Mon Sep 17 00:00:00 2001 From: Patrick Pircher Date: Tue, 1 Aug 2023 17:27:45 +0200 Subject: [PATCH] fix location for block params --- lib/parsers/gjs-parser.js | 40 +++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/lib/parsers/gjs-parser.js b/lib/parsers/gjs-parser.js index 5f813f8b0e..f8d3903cee 100644 --- a/lib/parsers/gjs-parser.js +++ b/lib/parsers/gjs-parser.js @@ -159,6 +159,33 @@ function preprocessGlimmerTemplates(info, code) { n.loc.start = codeLines.offsetToPosition(tpl.templateRange[0]); n.loc.end = codeLines.offsetToPosition(tpl.templateRange[1]); } + if ('blockParams' in node) { + node.params = []; + let part = code.slice(...node.range); + let start = node.range[0]; + let idx = part.indexOf('|'); + start += idx; + part = part.slice(idx, -1); + idx = part.indexOf('|'); + part = part.slice(idx, -1); + + for (const param in node.blockParams) { + const regex = new RegExp(`\\w${param}\\w`); + const match = part.match(regex); + const range = [start + match.index, 0]; + range[1] = range[0] + param.length; + node.params.push({ + type: 'BlockParam', + name: param, + range, + parent: node, + loc: { + start: codeLines.offsetToPosition(range[0]), + end: codeLines.offsetToPosition(range[1]) + } + }); + } + } n.type = `Glimmer__${n.type}`; allNodeTypes.add(n.type); } @@ -168,7 +195,6 @@ function preprocessGlimmerTemplates(info, code) { for (const [k, v] of Object.entries(glimmerVisitorKeys)) { templateVisitorKeys[`Glimmer__${k}`] = [...v]; } - templateVisitorKeys['Glimmer__PathExpression']?.push('identifier', 'member'); return { templateVisitorKeys, templateInfos, @@ -209,15 +235,9 @@ function convertAst(result, preprocessedResult, visitorKeys) { const scope = result.isTypescript ? new TypescriptScope.BlockScope(result.scopeManager, upperScope, node) : new Scope(result.scopeManager, 'block', upperScope, node); - for (const [i, b] of node.blockParams.entries()) { - const v = new Variable(b, scope); - const nameNode = { - type: 'BlockParam', - loc: node.loc, - parent: node, - name: b, - }; - v.identifiers.push(nameNode); + for (const [i, b] of node.params.entries()) { + const v = new Variable(b.name, scope); + v.identifiers.push(b); v.defs.push(new Definition('Parameter', nameNode, node, node, i, 'Block Param')); scope.variables.push(v); scope.set.set(b, v);