Skip to content

Commit

Permalink
It works
Browse files Browse the repository at this point in the history
  • Loading branch information
NullVoxPopuli committed Nov 30, 2023
1 parent 20a34cc commit c7c6a8f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,31 +1,60 @@
import { GlintExtensionPreprocess } from '@glint/core/config-types';
import { parseTemplates } from 'ember-template-imports';
import { GLOBAL_TAG, PreprocessData, TemplateLocation } from './common';

const TEMPLATE_START = `[${GLOBAL_TAG}\``;
const TEMPLATE_END = '`]';

import { Preprocessor } from 'content-tag';

const p = new Preprocessor();

// TODO: have `content-tag` generate this instead
interface Parsed {
type: 'expression' | 'class-member';
tagName: 'template';
contents: string;
range: {
start: number;
end: number;
};
contentRange: {
start: number;
end: number;
};
startRange: {
end: number;
start: number;
};
endRange: {
start: number;
end: number;
};
}

export const preprocess: GlintExtensionPreprocess<PreprocessData> = (source, path) => {
let templates = parseTemplates(source, path, { templateTag: 'template' }).filter(
(match) => match.type === 'template-tag'
);
let templates = p.parse(source, path) as Parsed[];

let templateLocations: Array<TemplateLocation> = [];
let segments: Array<string> = [];
let sourceOffset = 0;
let delta = 0;

for (let template of templates) {
let { start, end } = template;
let startTagLength = template.start[0].length;
let endTagLength = template.end[0].length;
let startTagOffset = start.index ?? -1;
let endTagOffset = end.index ?? -1;
let startTagLength = template.startRange.end - template.startRange.start;
let endTagLength = template.endRange.end - template.endRange.start;
let startTagOffset = template.startRange.start;
let endTagOffset = template.endRange.start;

if (startTagOffset === -1 || endTagOffset === -1) continue;

let transformedStart = startTagOffset - delta;

/**
* TODO: we want content-tag to manage all this for us, as managing indicies
* can be error-prone.
*
* SEE: https://github.com/embroider-build/content-tag/issues/39#issuecomment-1832443310
*/
segments.push(source.slice(sourceOffset, startTagOffset));
segments.push(TEMPLATE_START);
delta += startTagLength - TEMPLATE_START.length;
Expand Down
7 changes: 4 additions & 3 deletions packages/environment-ember-template-imports/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@
"-private/**/*.{js,d.ts}",
"globals/index.d.ts"
],
"dependencies": {
"content-tag": "^1.1.2"
},
"peerDependencies": {
"@glint/environment-ember-loose": "^1.2.1",
"@glint/template": "^1.2.1",
"@types/ember__component": "^4.0.10",
"@types/ember__helper": "^4.0.1",
"@types/ember__modifier": "^4.0.3",
"@types/ember__routing": "^4.0.12",
"ember-template-imports": "^3.0.0"
"@types/ember__routing": "^4.0.12"
},
"peerDependenciesMeta": {
"@types/ember__component": {
Expand All @@ -55,7 +57,6 @@
"@types/ember__helper": "^4.0.1",
"@types/ember__modifier": "^4.0.3",
"@types/ember__routing": "^4.0.12",
"ember-template-imports": "^3.0.0",
"vitest": "^0.22.0"
},
"publishConfig": {
Expand Down
7 changes: 6 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5151,6 +5151,11 @@ [email protected]:
dependencies:
safe-buffer "5.2.1"

content-tag@^1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/content-tag/-/content-tag-1.1.2.tgz#a7adddfdd2d44c9418b795c91ee04c55f570a997"
integrity sha512-AZkfc6TUmW+/RbZJioPzOQPAHHXqyqK4B0GNckJDjBAPK3SyGrMfn21bfFky/qwi5uoLph5sjAHUkO3CL6/IgQ==

content-type@~1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/content-type/-/content-type-1.0.4.tgz#e138cc75e040c727b1966fe5e5f8c9aee256fe3b"
Expand Down Expand Up @@ -6461,7 +6466,7 @@ ember-template-imports@^1.1.1:
ember-cli-version-checker "^5.1.2"
validate-peer-dependencies "^1.1.0"

ember-template-imports@^3.0.0, ember-template-imports@^3.1.1:
ember-template-imports@^3.1.1:
version "3.4.0"
resolved "https://registry.yarnpkg.com/ember-template-imports/-/ember-template-imports-3.4.0.tgz#c40757e2d41e289ce08c0fe80671000bf216e0ef"
integrity sha512-3Cwcj3NXA129g3ZhmrQ/nYOxksFonTmB/qxyaSNTHrLBSoc93UZys47hBz13DlcfoeSCCrNt2Qpq1j890I04PQ==
Expand Down

0 comments on commit c7c6a8f

Please sign in to comment.