Skip to content

Commit

Permalink
Merge pull request #259 from MichalBryxi/push-uwsuomlwwynt
Browse files Browse the repository at this point in the history
  • Loading branch information
gitKrystan authored Apr 3, 2024
2 parents ee198df + fc5b890 commit b728f4f
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 31 deletions.
31 changes: 19 additions & 12 deletions src/parse/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,24 +92,14 @@ function convertAst(ast: File, templates: Template[]): void {
* fixing the offsets and locations of all nodes also calculates the block
* params locations & ranges and adding it to the info
*/
function preprocess(
export function preprocess(
code: string,
fileName: string,
): {
code: string;
templates: Template[];
} {
const rawTemplates = p.parse(code, fileName);
const templates: Template[] = rawTemplates.map((r) => ({
type: r.type,
range: r.range,
contentRange: r.contentRange,
contents: r.contents,
utf16Range: {
start: byteToCharIndex(code, r.range.start),
end: byteToCharIndex(code, r.range.end),
},
}));
const templates = codeToGlimmerAst(code, fileName);

for (const template of templates) {
code = preprocessTemplateRange(template, code);
Expand All @@ -130,3 +120,20 @@ export const parser: Parser<Node | undefined> = {
return ast;
},
};

/** Pre-processes the template info, parsing the template content to Glimmer AST. */
export function codeToGlimmerAst(code: string, fileName: string): Template[] {
const rawTemplates = p.parse(code, fileName);
const templates: Template[] = rawTemplates.map((r) => ({
type: r.type,
range: r.range,
contentRange: r.contentRange,
contents: r.contents,
utf16Range: {
start: byteToCharIndex(code, r.range.start),
end: byteToCharIndex(code, r.range.end),
},
}));

return templates;
}
6 changes: 5 additions & 1 deletion src/parse/preprocess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export interface Template {

const BufferMap: Map<string, Buffer> = new Map();

export const PLACEHOLDER = '~';

function getBuffer(s: string): Buffer {
let buf = BufferMap.get(s);
if (!buf) {
Expand Down Expand Up @@ -76,7 +78,9 @@ export function preprocessTemplateRange(
}
}

const content = template.contents.replaceAll('/', '\\/');
// We need to replace forward slash with _something else_, because
// forward slash breaks the parsed templates.
const content = template.contents.replaceAll('/', PLACEHOLDER);
const tplLength = template.range.end - template.range.start;
const spaces =
tplLength - byteLength(content) - prefix.length - suffix.length;
Expand Down
15 changes: 15 additions & 0 deletions tests/cases/gts/issue-255.gts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Component from '@glimmer/component';

export default class PooComponent extends Component {
<template>
Testing line, incorrectly indented.
{{#if true}}
{{#if true}}
<link href="/////styles/" />
/* hi */
{{else}}
<link href="/////styles/" />
{{/if}}
{{/if}}
</template>
}
19 changes: 19 additions & 0 deletions tests/unit-tests/__snapshots__/format.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,25 @@ export default class MultiByteCharComponent extends Component {
"
`;
exports[`format > config > default > it formats ../cases/gts/issue-255.gts 1`] = `
"import Component from "@glimmer/component";
export default class PooComponent extends Component {
<template>
Testing line, incorrectly indented.
{{#if true}}
{{#if true}}
<link href="/////styles/" />
/* hi */
{{else}}
<link href="/////styles/" />
{{/if}}
{{/if}}
</template>
}
"
`;
exports[`format > config > default > it formats ../cases/gts/js-only.gts 1`] = `
"const num: number = 1;
"
Expand Down
18 changes: 0 additions & 18 deletions tests/unit-tests/ambiguous/__snapshots__/semi-false.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4217,24 +4217,6 @@ class MyComponent extends Component {
"
`;

exports[`ambiguous > config > semi: false > ["oops"] > with semi, with newline > it formats ../cases/gjs/component-class-with-content-before-template.gjs 2`] = `
"import Component from "@glimmer/component"

/** It's a component */
class MyComponent extends Component {
get whatever() {}

<template>
<h1>
Class top level template. Class top level template. Class top level
template. Class top level template. Class top level template.
</h1>
</template>
["oops"]
}
"
`;

exports[`ambiguous > config > semi: false > ["oops"] > with semi, with newline > it formats ../cases/gjs/default-export.gjs 1`] = `
"<template>
Explicit default export module top level component. Explicit default export
Expand Down
19 changes: 19 additions & 0 deletions tests/unit-tests/config/__snapshots__/semi-false.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,25 @@ export default class MultiByteCharComponent extends Component {
"
`;
exports[`config > semi: false > it formats ../cases/gts/issue-255.gts 1`] = `
"import Component from "@glimmer/component"
export default class PooComponent extends Component {
<template>
Testing line, incorrectly indented.
{{#if true}}
{{#if true}}
<link href="/////styles/" />
/* hi */
{{else}}
<link href="/////styles/" />
{{/if}}
{{/if}}
</template>
}
"
`;
exports[`config > semi: false > it formats ../cases/gts/js-only.gts 1`] = `
"const num: number = 1
"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,25 @@ export default class MultiByteCharComponent extends Component {
"
`;
exports[`config > templateExportDefault: true > it formats ../cases/gts/issue-255.gts 1`] = `
"import Component from "@glimmer/component";
export default class PooComponent extends Component {
<template>
Testing line, incorrectly indented.
{{#if true}}
{{#if true}}
<link href="/////styles/" />
/* hi */
{{else}}
<link href="/////styles/" />
{{/if}}
{{/if}}
</template>
}
"
`;
exports[`config > templateExportDefault: true > it formats ../cases/gts/js-only.gts 1`] = `
"const num: number = 1;
"
Expand Down
64 changes: 64 additions & 0 deletions tests/unit-tests/preprocess.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { describe, expect, test } from 'vitest';

import { codeToGlimmerAst } from '../../src/parse/index.js';
import {
PLACEHOLDER,
preprocessTemplateRange,
} from '../../src/parse/preprocess.js';

const TEST_CASES = [
{
code: '<template>hi</template>',
expected: [`{/*hi */}`],
},
{
code: '<template>/* hi */</template>',
expected: [`{/*${PLACEHOLDER}* hi *${PLACEHOLDER} */}`],
},
{
code: '<template><div>hi</div></template>',
expected: [`{/*<div>hi<${PLACEHOLDER}div> */}`],
},
{
code: '<template>{{#if true}}hi{{/if}}</template>',
expected: [`{/*{{#if true}}hi{{${PLACEHOLDER}if}} */}`],
},
{
code: '<template>////////////////</template>',
expected: [
`{/*${PLACEHOLDER}${PLACEHOLDER}${PLACEHOLDER}${PLACEHOLDER}${PLACEHOLDER}${PLACEHOLDER}${PLACEHOLDER}${PLACEHOLDER}${PLACEHOLDER}${PLACEHOLDER}${PLACEHOLDER}${PLACEHOLDER}${PLACEHOLDER}${PLACEHOLDER}${PLACEHOLDER}${PLACEHOLDER} */}`,
],
},
{
code: '<template>💩</template>',
expected: [`{/*💩 */}`],
},
{
code: 'const a = <template>foo</template>; const b = <template>bar</template>;',
expected: [
`const a = {/*foo */}; const b = <template>bar</template>;`,
`const a = <template>foo</template>; const b = {/*bar */};`,
],
},
{
code: `const a = <template>💩💩💩💩💩💩💩</template>; const b = <template>💩</template>`,
expected: [
`const a = {/*💩💩💩💩💩💩💩 */}; const b = <template>💩</template>`,
`const a = <template>💩💩💩💩💩💩💩</template>; const b = {/*💩 */}`,
],
},
];
const FILE_NAME = 'foo.gts';

describe('preprocess', () => {
for (const testCase of TEST_CASES) {
test(`preprocessTemplateRange ${testCase.code}`, () => {
const templates = codeToGlimmerAst(testCase.code, FILE_NAME);
for (const [index, template] of templates.entries()) {
const received = preprocessTemplateRange(template, testCase.code);
expect(received).toEqual(testCase.expected[index]);
expect(received).toHaveLength(testCase.code.length);
}
});
}
});

0 comments on commit b728f4f

Please sign in to comment.