[FEAT] Extract the Handlebars parser (#1713)

Extracts the parser to `@handlebars/parser`, where it can be shared
between different implementations. This means that e.g. Glimmer/Ember
will be able to iterate on new features without forcing Handlebars to
adopt them immediately, and vice versa. All implementors will be able to
absorb changes as it makes sense for them.
This commit is contained in:
Chris Garrett
2021-02-14 23:56:25 -08:00
committed by GitHub
parent 071003b89c
commit 19bdace85a
34 changed files with 58 additions and 2268 deletions
+28 -175
View File
@@ -13,6 +13,12 @@
* https://github.com/DefinitelyTyped/DefinitelyTyped/commits/1ce60bdc07f10e0b076778c6c953271c072bc894/types/handlebars/index.d.ts
*/
// TypeScript Version: 2.3
import {
parse,
parseWithoutProcessing,
ParseOptions,
AST
} from '@handlebars/parser';
declare namespace Handlebars {
export interface TemplateDelegate<T = any> {
@@ -50,10 +56,7 @@ declare namespace Handlebars {
[key: string]: HelperDelegate;
}
export interface ParseOptions {
srcName?: string;
ignoreStandalone?: boolean;
}
export { parse, parseWithoutProcessing, ParseOptions };
export function registerHelper(name: string, fn: HelperDelegate): void;
export function registerHelper(name: HelperDeclareSpec): void;
@@ -71,8 +74,7 @@ declare namespace Handlebars {
export function createFrame(object: any): any;
export function blockParams(obj: any[], ids: any[]): any[];
export function log(level: number, obj: any): void;
export function parse(input: string, options?: ParseOptions): hbs.AST.Program;
export function parseWithoutProcessing(input: string, options?: ParseOptions): hbs.AST.Program;
export function compile<T = any>(input: any, options?: CompileOptions): HandlebarsTemplateDelegate<T>;
export function precompile(input: any, options?: PrecompileOptions): TemplateSpecification;
export function template<T = any>(precompilation: TemplateSpecification): HandlebarsTemplateDelegate<T>;
@@ -127,7 +129,7 @@ declare namespace Handlebars {
export const helpers: hbs.AST.helpers;
}
interface ICompiler {
export interface ICompiler {
accept(node: hbs.AST.Node): void;
Program(program: hbs.AST.Program): void;
BlockStatement(block: hbs.AST.BlockStatement): void;
@@ -191,25 +193,25 @@ declare namespace Handlebars {
/**
* Implement this interface on your MVW/MVVM/MVC views such as Backbone.View
**/
interface HandlebarsTemplatable {
export interface HandlebarsTemplatable {
template: HandlebarsTemplateDelegate;
}
// NOTE: for backward compatibility of this typing
type HandlebarsTemplateDelegate<T = any> = Handlebars.TemplateDelegate<T>;
export type HandlebarsTemplateDelegate<T = any> = Handlebars.TemplateDelegate<T>;
interface HandlebarsTemplates {
export interface HandlebarsTemplates {
[index: string]: HandlebarsTemplateDelegate;
}
interface TemplateSpecification {
export interface TemplateSpecification {
}
// for backward compatibility of this typing
type RuntimeOptions = Handlebars.RuntimeOptions;
export type RuntimeOptions = Handlebars.RuntimeOptions;
interface CompileOptions {
export interface CompileOptions {
data?: boolean;
compat?: boolean;
knownHelpers?: KnownHelpers;
@@ -222,11 +224,11 @@ interface CompileOptions {
explicitPartialContext?: boolean;
}
type KnownHelpers = {
export type KnownHelpers = {
[name in BuiltinHelperName | CustomHelperName]: boolean;
};
type BuiltinHelperName =
export type BuiltinHelperName =
"helperMissing"|
"blockHelperMissing"|
"each"|
@@ -236,21 +238,23 @@ type BuiltinHelperName =
"log"|
"lookup";
type CustomHelperName = string;
export type CustomHelperName = string;
interface PrecompileOptions extends CompileOptions {
export interface PrecompileOptions extends CompileOptions {
srcName?: string;
destName?: string;
}
declare namespace hbs {
export namespace hbs {
// for backward compatibility of this typing
type SafeString = Handlebars.SafeString;
export type SafeString = Handlebars.SafeString;
type Utils = typeof Handlebars.Utils;
export type Utils = typeof Handlebars.Utils;
export { AST }
}
interface Logger {
export interface Logger {
DEBUG: number;
INFO: number;
WARN: number;
@@ -262,161 +266,10 @@ interface Logger {
log(level: number, obj: string): void;
}
type CompilerInfo = [number/* revision */, string /* versions */];
declare namespace hbs {
namespace AST {
interface Node {
type: string;
loc: SourceLocation;
}
interface SourceLocation {
source: string;
start: Position;
end: Position;
}
interface Position {
line: number;
column: number;
}
interface Program extends Node {
body: Statement[];
blockParams: string[];
}
interface Statement extends Node {}
interface MustacheStatement extends Statement {
type: 'MustacheStatement';
path: PathExpression | Literal;
params: Expression[];
hash: Hash;
escaped: boolean;
strip: StripFlags;
}
interface Decorator extends MustacheStatement { }
interface BlockStatement extends Statement {
type: 'BlockStatement';
path: PathExpression;
params: Expression[];
hash: Hash;
program: Program;
inverse: Program;
openStrip: StripFlags;
inverseStrip: StripFlags;
closeStrip: StripFlags;
}
interface DecoratorBlock extends BlockStatement { }
interface PartialStatement extends Statement {
type: 'PartialStatement';
name: PathExpression | SubExpression;
params: Expression[];
hash: Hash;
indent: string;
strip: StripFlags;
}
interface PartialBlockStatement extends Statement {
type: 'PartialBlockStatement';
name: PathExpression | SubExpression;
params: Expression[];
hash: Hash;
program: Program;
openStrip: StripFlags;
closeStrip: StripFlags;
}
interface ContentStatement extends Statement {
type: 'ContentStatement';
value: string;
original: StripFlags;
}
interface CommentStatement extends Statement {
type: 'CommentStatement';
value: string;
strip: StripFlags;
}
interface Expression extends Node {}
interface SubExpression extends Expression {
type: 'SubExpression';
path: PathExpression;
params: Expression[];
hash: Hash;
}
interface PathExpression extends Expression {
type: 'PathExpression';
data: boolean;
depth: number;
parts: string[];
original: string;
}
interface Literal extends Expression {}
interface StringLiteral extends Literal {
type: 'StringLiteral';
value: string;
original: string;
}
interface BooleanLiteral extends Literal {
type: 'BooleanLiteral';
value: boolean;
original: boolean;
}
interface NumberLiteral extends Literal {
type: 'NumberLiteral';
value: number;
original: number;
}
interface UndefinedLiteral extends Literal {
type: 'UndefinedLiteral';
}
interface NullLiteral extends Literal {
type: 'NullLiteral';
}
interface Hash extends Node {
type: 'Hash';
pairs: HashPair[];
}
interface HashPair extends Node {
type: 'HashPair';
key: string;
value: Expression;
}
interface StripFlags {
open: boolean;
close: boolean;
}
interface helpers {
helperExpression(node: Node): boolean;
scopeId(path: PathExpression): boolean;
simpleId(path: PathExpression): boolean;
}
}
}
declare module "handlebars" {
export = Handlebars;
}
export type CompilerInfo = [number/* revision */, string /* versions */];
declare module "handlebars/runtime" {
export = Handlebars;
}
export default Handlebars;