diff --git a/addon/helpers/lookup-helper.js b/addon/helpers/lookup-helper.js index 2bdb35c..1c130dd 100644 --- a/addon/helpers/lookup-helper.js +++ b/addon/helpers/lookup-helper.js @@ -3,6 +3,10 @@ import Helper from '@ember/component/helper'; export default Helper.extend({ compute([context, path]) { - return getOwner(context||this).lookup(`helper:${path}`); + const h = getOwner(context||this).lookup(`helper:${path}`); + if (!h) { + throw new Error('could not find helper: ' + path); + } + return h; } }); diff --git a/addon/helpers/lookup-modifier.js b/addon/helpers/lookup-modifier.js index c9d4de9..8b3cc92 100644 --- a/addon/helpers/lookup-modifier.js +++ b/addon/helpers/lookup-modifier.js @@ -3,7 +3,11 @@ import Helper from '@ember/component/helper'; export default Helper.extend({ compute([context, path]) { - return getOwner(context||this).factoryFor(`modifier:${path}`)?.class; + const m = getOwner(context||this).factoryFor(`modifier:${path}`)?.class; + if (!m) { + throw new Error('could not find modifier: ' + path); + } + return m; } }); diff --git a/glint/glint-hbs-imports-env.js b/glint/glint-hbs-imports-env.js index 2b6dad9..8d21885 100644 --- a/glint/glint-hbs-imports-env.js +++ b/glint/glint-hbs-imports-env.js @@ -5,6 +5,8 @@ let relativePath = null; let currentTemplate = null; const cwd = process.cwd(); +const util = require('@glint/core/lib/language-server/util'); + function hbsImportsRewriteModule(ts, { script, template }, environment) { if (template?.filename) { relativePath = path.relative(cwd, template.filename); @@ -46,6 +48,19 @@ const hbsImportPreprocess = function(template) { if ([Object.keys(imported.info.components), Object.keys(imported.info.modifiers), Object.keys(imported.info.helpers)].flat().length === 0) { delete transformArgs?.globals; } + + const all = [ + ...Object.values(imported.info.components), + ...Object.values(imported.info.helpers), + ...Object.values(imported.info.modifiers) + ]; + + all.forEach((c) => { + const node = c.imp.node; + node.params[2].loc.start.offset = util.positionToOffset(template, { line: node.params[2].loc.start.line-1, character: node.params[2].loc.start.column }); + node.params[2].loc.end.offset = util.positionToOffset(template, { line: node.params[2].loc.end.line-1, character: node.params[2].loc.end.column }); + }) + const preamble = transformArgs.preamble; Object.entries(imported.info.components).forEach(([tag, i]) => { if (i.imp.shouldLookInFile) { @@ -127,7 +142,6 @@ templateToTypescript.templateToTypescript = patchedTemplateToTypescript; const transformManager = Object.entries(require.cache).find(([k, v]) => k.includes('@glint\\core\\lib\\common\\transform-manager'))?.[1].exports; const rewriteDiagnostics = transformManager.default.prototype.rewriteDiagnostics; -const util = require('@glint/core/lib/language-server/util'); const patchedRewriteDiagnostics = function (diagnostics, fileName) { console.error('patchedRewriteDiagnostics'); const diags = rewriteDiagnostics.call(this, diagnostics, fileName); @@ -144,8 +158,8 @@ const patchedRewriteDiagnostics = function (diagnostics, fileName) { Object.values(cache[rel].info.helpers).find(x => x.imp.importPath === importPath) || Object.values(cache[rel].info.modifiers).find(x => x.imp.importPath === importPath); if (c) { - d.start = util.positionToOffset(d.file.text, { line: c.imp.node.params[2].loc.start.line-1, character: c.imp.node.params[2].loc.start.column }); - const end = util.positionToOffset(d.file.text, { line: c.imp.node.params[2].loc.end.line-1, character: c.imp.node.params[2].loc.end.column }) + d.start = c.imp.node.params[2].loc.start.offset; + const end = c.imp.node.params[2].loc.end.offset; d.length = end - d.start; } } @@ -153,3 +167,30 @@ const patchedRewriteDiagnostics = function (diagnostics, fileName) { return diags; } transformManager.default.prototype.rewriteDiagnostics = patchedRewriteDiagnostics; + +const getTransformedOffset = transformManager.default.prototype.getTransformedOffset; +const patchedGetTransformedOffset = function (originalFileName, originalOffset) { + const res = getTransformedOffset.call(this, originalFileName, originalOffset); + const rel = path.relative(cwd, originalFileName).replace(/\\/g, '/'); + const contents = this.readTransformedFile(originalFileName); + const transformedContents = this.readTransformedFile(res.transformedFileName); + + const all = [ + ...Object.values(cache[rel].info.components), + ...Object.values(cache[rel].info.helpers), + ...Object.values(cache[rel].info.modifiers) + ]; + console.error('patchedGetTransformedOffset', originalOffset, all.map(x => [x.imp.node.params[2].loc.start.offset, x.imp.node.params[2].loc.end.offset])); + const c = all.find(x => x.imp.node.params[2].loc.start.offset <= originalOffset && x.imp.node.params[2].loc.end.offset >= originalOffset); + if (c) { + console.error('patchedGetTransformedOffset', res, transformedContents); + const offsetInImportPath = originalOffset - c.imp.node.params[2].loc.start.offset; + const importPath = c.imp.importPath; + const importSpecifier = `typeof import('${importPath}')`; + const startOffset = 'typeof import(\''.length; + res.transformedOffset = transformedContents.indexOf(importSpecifier) + startOffset + offsetInImportPath; + } + return res; +} + +transformManager.default.prototype.getTransformedOffset = patchedGetTransformedOffset; diff --git a/package.json b/package.json index 8a2eba2..6745d10 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ember-hbs-imports", - "version": "0.5.12", + "version": "0.5.13", "repository": { "type": "git", "url": "https://github.com/patricklx/ember-hbs-imports"