Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up content-tag implementation #178

Merged
merged 32 commits into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
d95a4d6
Remove unnecessary Path type
gitKrystan Dec 2, 2023
488bf68
Clean up parse.ts
gitKrystan Dec 3, 2023
e0a1332
More parse.ts cleanup
gitKrystan Dec 3, 2023
b293e05
More parse.ts cleanup
gitKrystan Dec 3, 2023
c26162f
Align range terminology
gitKrystan Dec 3, 2023
8841439
Clean up convertAst
gitKrystan Dec 3, 2023
417fce2
Start printer cleanup
gitKrystan Dec 3, 2023
89e20bf
More printer cleanup
gitKrystan Dec 3, 2023
0e7542b
Clean up flattenDoc
gitKrystan Dec 3, 2023
7bc8365
Fix export default bug
gitKrystan Dec 3, 2023
207e074
Clean up isDefaultTemplate
gitKrystan Dec 3, 2023
d24bb45
Remove useless isAssignment meta
gitKrystan Dec 3, 2023
649c078
Remove unnecessary try/catch
gitKrystan Dec 3, 2023
2f2defe
Clean up fixPreviousPrint hack
gitKrystan Dec 3, 2023
ff7cadc
Clean up printer some more
gitKrystan Dec 3, 2023
3bb2b67
More printer cleanup
gitKrystan Dec 3, 2023
24d8309
Snapshot parse errors in tests
gitKrystan Dec 3, 2023
4cc3f00
Start merging prepare / preprocess
gitKrystan Dec 3, 2023
c4b3f2b
Revert "Snapshot parse errors in tests"
gitKrystan Dec 3, 2023
c9dc4d2
Snapshot parse errors in tests
gitKrystan Dec 3, 2023
3687e92
Clean up preprocessed
gitKrystan Dec 3, 2023
54a67ee
Move parse -> parse/index
gitKrystan Dec 3, 2023
88b86d2
Move normalizeWhitespace to separate file
gitKrystan Dec 3, 2023
e32aeb4
Remove unnecessary reverse()
gitKrystan Dec 3, 2023
9cf88fc
Clean up preprocess some more
gitKrystan Dec 3, 2023
b936e6a
Never minify
gitKrystan Dec 3, 2023
1ea060b
Fix lint
gitKrystan Dec 3, 2023
3e1e03a
Clean up convertAst
gitKrystan Dec 3, 2023
0904a26
Simplify preprocess
gitKrystan Dec 3, 2023
aabd8e3
Revert examples change
gitKrystan Dec 3, 2023
056f305
Remove unnecessary hasPrettierIgnore: undefined
gitKrystan Dec 3, 2023
4446b77
Simplify syntax error test output
gitKrystan Dec 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ module.exports = {
'@typescript-eslint/no-dynamic-delete': 'error',
'@typescript-eslint/no-extra-semi': 'error',
'@typescript-eslint/no-extraneous-class': 'error',
'@typescript-eslint/no-import-type-side-effects': 'error',
'@typescript-eslint/no-inferrable-types': 'error',
'@typescript-eslint/no-invalid-void-type': 'error',
'@typescript-eslint/no-require-imports': 'error',
'@typescript-eslint/no-unnecessary-condition': 'error',
Expand Down Expand Up @@ -72,6 +74,7 @@ module.exports = {
'jsdoc/tag-lines': 'off',
'simple-import-sort/imports': 'error',
'simple-import-sort/exports': 'error',
'unicorn/consistent-destructuring': 'off',
'unicorn/consistent-function-scoping': [
'error',
{ checkArrowFunctions: false },
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Parser, Plugin, Printer, SupportLanguage } from 'prettier';

import { PARSER_NAME, PRINTER_NAME } from './config.js';
import { options } from './options.js';
import { parser } from './parse.js';
import { parser } from './parse/index.js';
import { printer } from './print/index.js';

const languages: SupportLanguage[] = [
Expand Down
245 changes: 0 additions & 245 deletions src/parse.ts

This file was deleted.

121 changes: 121 additions & 0 deletions src/parse/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import type { NodePath } from '@babel/core';
import { traverse } from '@babel/core';
import type {
BlockStatement,
Node,
ObjectExpression,
StaticBlock,
} from '@babel/types';
import { Preprocessor } from 'content-tag';
import type { Parser } from 'prettier';
import { parsers as babelParsers } from 'prettier/plugins/babel.js';

import { PRINTER_NAME } from '../config';
import type { Options } from '../options.js';
import type { GlimmerTemplateInfo, RawGlimmerTemplate } from '../types/glimmer';
import { isDefaultTemplate } from '../types/glimmer';
import { assert } from '../utils';
import { normalizeWhitespace } from './whitespace';

const typescript = babelParsers['babel-ts'] as Parser<Node | undefined>;
const p = new Preprocessor();

/** Converts a node into a GlimmerTemplate node */
function convertNode(
path: NodePath,
node: BlockStatement | ObjectExpression | StaticBlock,
templateInfo: GlimmerTemplateInfo,
): void {
Object.assign(node, templateInfo, {
type: 'FunctionDeclaration',
extra: Object.assign(node.extra ?? {}, templateInfo.extra, {
isGlimmerTemplate: true,
isDefaultTemplate: isDefaultTemplate(path),
}),
});
}

/** Traverses the AST and replaces the transformed template parts with other AST */
function convertAst(ast: Node, templateInfos: GlimmerTemplateInfo[]): void {
let counter = 0;

traverse(ast, {
enter(path) {
const { node } = path;
if (
node.type === 'ObjectExpression' ||
node.type === 'BlockStatement' ||
node.type === 'StaticBlock'
) {
const { range } = node;
assert('expected range', range);

const templateInfo = templateInfos.find(
(p) =>
(p.range[0] === range[0] && p.range[1] === range[1]) ||
(p.range[0] === range[0] - 1 && p.range[1] === range[1] + 1) ||
(p.range[0] === range[0] && p.range[1] === range[1] + 1),
);

if (!templateInfo) {
return null;
}

convertNode(path, node, templateInfo);

counter++;
}
return null;
},
});

if (counter !== templateInfos.length) {
throw new Error('failed to process all templates');
}
}

/**
* Pre-processes the template info, parsing the template content to Glimmer AST,
* fixing the offsets and locations of all nodes also calculates the block
* params locations & ranges and adding it to the info
*/
function preprocess(code: string): {
code: string;
templateInfos: GlimmerTemplateInfo[];
} {
const templateNodes = p.parse(code) as RawGlimmerTemplate[];
const templateInfos: GlimmerTemplateInfo[] = [];
let output = code;
for (const templateNode of templateNodes) {
output = normalizeWhitespace(templateNode, code, output);

const template = code.slice(
templateNode.contentRange.start,
templateNode.contentRange.end,
);
const templateInfo: GlimmerTemplateInfo = {
range: [templateNode.range.start, templateNode.range.end],
start: templateNode.range.start,
end: templateNode.range.end,
extra: {
template,
},
};
templateInfos.push(templateInfo);
}

return { templateInfos, code: output };
}

export const parser: Parser<Node | undefined> = {
...typescript,
astFormat: PRINTER_NAME,

async parse(code: string, options: Options): Promise<Node> {
const preprocessed = preprocess(code);
const ast = await typescript.parse(preprocessed.code, options);
assert('expected ast', ast);
convertAst(ast, preprocessed.templateInfos);
return ast;
},
};
Loading