Skip to content

Commit

Permalink
Merge pull request #178 from gitKrystan/cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
gitKrystan authored Dec 3, 2023
2 parents 3811c0b + 4446b77 commit 8dd633d
Show file tree
Hide file tree
Showing 15 changed files with 1,176 additions and 465 deletions.
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

0 comments on commit 8dd633d

Please sign in to comment.