Skip to content

Commit d76260a

Browse files
Merge pull request #18 from ember-tooling/additional-apis
Add and document the ParseResultStringUtils
2 parents a609b16 + 8fa5f2b commit d76260a

File tree

4 files changed

+93
-11
lines changed

4 files changed

+93
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules/
2+
.log/

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ Properties / Methods:
9999
- `t.transformOneSync()`
100100
- `t.transformOne()`
101101
- `t.reverseInnerCoordinatesOf()` Given in-template coordinates, returns the coordinates in the context of the file
102+
- `t.stringUtils` Collection of utilities for working with parseResults
103+
- `t.stringUtils.contentBefore(parseResult)` return the string contents before the passed parse result, before the opening `<template>`
104+
- `t.stringUtils.originalContentOf(parseResult)` returns the original content of the parseResult, prior to any transformations
105+
- `t.stringUtils.openingTag(parseResult)` returns the opening `<template>` including any attributes are key-value pairs it may have on it
106+
- `t.stringUtils.closingTag(parseResult)` returns the clasing `</template>` which is expected to always be `=== '</template'`
102107
103108
### transform + transformSync
104109

src/transformer.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ export class Transformer {
7979
return this.#parseResults;
8080
}
8181

82+
/**
83+
* Utils for viewing the parts of a parseResult
84+
* with in JS-character-byte range
85+
*
86+
* @type {ParseResultStringUtils}
87+
*/
88+
get stringUtils() {
89+
return this.#stringUtils;
90+
}
91+
8292
/**
8393
* For a given set of coordinates, find the parseResult and return it
8494
* You don't need to provide the whole original coordinates object.

tests/transformer.StringUtils.test.ts

Lines changed: 77 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
11
import { describe, it, expect } from "vitest";
2-
import { ParseResultStringUtils } from "content-tag-utils";
2+
import { Transformer, ParseResultStringUtils } from "content-tag-utils";
33
import { Preprocessor } from "content-tag";
44

55
const p = new Preprocessor();
66

7-
function create(content: string) {
8-
let buffer = Buffer.from(content, "utf8");
9-
10-
return {
11-
stringUtils: new ParseResultStringUtils(buffer),
12-
parseResults: p.parse(content),
13-
};
14-
}
15-
167
let simpleTemplate = [
178
"export const Name = <template>",
189
" {{@name}}",
@@ -24,7 +15,82 @@ let simpleTemplate = [
2415
"",
2516
].join("\n");
2617

27-
describe("StringUtils", () => {
18+
describe("ParseResultStringUtils", () => {
19+
function create(content: string) {
20+
let buffer = Buffer.from(content, "utf8");
21+
22+
return {
23+
stringUtils: new ParseResultStringUtils(buffer),
24+
parseResults: p.parse(content),
25+
};
26+
}
27+
28+
describe("contentBefore", () => {
29+
it("works", () => {
30+
let x = create(simpleTemplate);
31+
32+
let result = x.stringUtils.contentBefore(x.parseResults[0]!);
33+
let result2 = x.stringUtils.contentBefore(x.parseResults[1]!);
34+
35+
expect(result).toMatchInlineSnapshot(`"export const Name = "`);
36+
expect(result2).toMatchInlineSnapshot(`
37+
"export const Name = <template>
38+
{{@name}}
39+
</template>;
40+
41+
export const Greeting = "
42+
`);
43+
});
44+
});
45+
46+
describe("originalContentOf", () => {
47+
it("works", () => {
48+
let x = create(simpleTemplate);
49+
50+
let result = x.stringUtils.originalContentOf(x.parseResults[0]!);
51+
let result2 = x.stringUtils.originalContentOf(x.parseResults[1]!);
52+
53+
expect(result).toMatchInlineSnapshot(`
54+
"
55+
{{@name}}
56+
"
57+
`);
58+
expect(result2).toMatchInlineSnapshot(`
59+
"
60+
Hello, <Name @name={{@name}} />!
61+
"
62+
`);
63+
});
64+
});
65+
describe("openingTag", () => {
66+
it("works", () => {
67+
let x = create(simpleTemplate);
68+
69+
let result = x.stringUtils.openingTag(x.parseResults[0]!);
70+
let result2 = x.stringUtils.openingTag(x.parseResults[1]!);
71+
72+
expect(result).toMatchInlineSnapshot(`"<template>"`);
73+
expect(result2).toMatchInlineSnapshot(`"<template>"`);
74+
});
75+
});
76+
describe("closingTag", () => {
77+
it("works", () => {
78+
let x = create(simpleTemplate);
79+
80+
let result = x.stringUtils.closingTag(x.parseResults[0]!);
81+
let result2 = x.stringUtils.closingTag(x.parseResults[1]!);
82+
83+
expect(result).toMatchInlineSnapshot(`"</template>"`);
84+
expect(result2).toMatchInlineSnapshot(`"</template>"`);
85+
});
86+
});
87+
});
88+
89+
describe("stringUtils", () => {
90+
function create(content: string) {
91+
return new Transformer(content);
92+
}
93+
2894
describe("contentBefore", () => {
2995
it("works", () => {
3096
let x = create(simpleTemplate);

0 commit comments

Comments
 (0)