Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert
@babel/template
from Flow to TS (#12317)
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
- Loading branch information
1 parent
f80478c
commit 089c200
Showing
11 changed files
with
218 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
declare module "@babel/template" { | ||
declare type PublicOpts = { | ||
placeholderWhitelist?: ?Set<string>, | ||
placeholderPattern?: ?(RegExp | false), | ||
preserveComments?: ?boolean, | ||
syntacticPlaceholders?: ?boolean, | ||
}; | ||
|
||
declare type PublicReplacements = { [string]: mixed } | Array<mixed>; | ||
|
||
declare type TemplateBuilder<T> = { | ||
ast: { | ||
(tpl: string, opts: ?PublicOpts): T, | ||
(tpl: Array<string>, ...args: Array<mixed>): T, | ||
}, | ||
(opts: PublicOpts): TemplateBuilder<T>, | ||
(tpl: string, opts: ?PublicOpts): (?PublicReplacements) => T, | ||
(tpl: Array<string>, ...args: Array<mixed>): (?PublicReplacements) => T, | ||
}; | ||
|
||
declare type Smart = TemplateBuilder< | ||
Array<BabelNodeStatement> | BabelNodeStatement | ||
>; | ||
declare type Statement = TemplateBuilder<BabelNodeStatement>; | ||
declare type Statements = TemplateBuilder<Array<BabelNodeStatement>>; | ||
declare type Expression = TemplateBuilder<BabelNodeExpression>; | ||
declare type Program = TemplateBuilder<BabelNodeProgram>; | ||
|
||
declare export default Smart & { | ||
smart: Smart, | ||
statement: Statement, | ||
statements: Statements, | ||
expression: Expression, | ||
program: Program, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import * as t from "@babel/types"; | ||
|
||
export type Formatter<T> = { | ||
code: (source: string) => string; | ||
validate: (ast: t.File) => void; | ||
unwrap: (ast: t.File) => T; | ||
}; | ||
|
||
function makeStatementFormatter<T>( | ||
fn: (statements: Array<t.Statement>) => T, | ||
): Formatter<T> { | ||
return { | ||
// We need to prepend a ";" to force statement parsing so that | ||
// ExpressionStatement strings won't be parsed as directives. | ||
// Alongside that, we also prepend a comment so that when a syntax error | ||
// is encountered, the user will be less likely to get confused about | ||
// where the random semicolon came from. | ||
code: str => `/* @babel/template */;\n${str}`, | ||
validate: () => {}, | ||
unwrap: (ast: t.File): T => { | ||
return fn(ast.program.body.slice(1)); | ||
}, | ||
}; | ||
} | ||
|
||
export const smart = makeStatementFormatter(body => { | ||
if (body.length > 1) { | ||
return body; | ||
} else { | ||
return body[0]; | ||
} | ||
}); | ||
|
||
export const statements = makeStatementFormatter(body => body); | ||
|
||
export const statement = makeStatementFormatter(body => { | ||
// We do this validation when unwrapping since the replacement process | ||
// could have added or removed statements. | ||
if (body.length === 0) { | ||
throw new Error("Found nothing to return."); | ||
} | ||
if (body.length > 1) { | ||
throw new Error("Found multiple statements but wanted one"); | ||
} | ||
|
||
return body[0]; | ||
}); | ||
|
||
export const expression: Formatter<t.Expression> = { | ||
code: str => `(\n${str}\n)`, | ||
validate: ast => { | ||
if (ast.program.body.length > 1) { | ||
throw new Error("Found multiple statements but wanted one"); | ||
} | ||
if (expression.unwrap(ast).start === 0) { | ||
throw new Error("Parse result included parens."); | ||
} | ||
}, | ||
unwrap: ({ program }) => { | ||
const [stmt] = program.body; | ||
t.assertExpressionStatement(stmt); | ||
return stmt.expression; | ||
}, | ||
}; | ||
|
||
export const program: Formatter<t.Program> = { | ||
code: str => str, | ||
validate: () => {}, | ||
unwrap: ast => ast.program, | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import * as formatters from "./formatters"; | ||
import createTemplateBuilder from "./builder"; | ||
|
||
export const smart = createTemplateBuilder(formatters.smart); | ||
export const statement = createTemplateBuilder(formatters.statement); | ||
export const statements = createTemplateBuilder(formatters.statements); | ||
export const expression = createTemplateBuilder(formatters.expression); | ||
export const program = createTemplateBuilder(formatters.program); | ||
|
||
type DefaultTemplateBuilder = typeof smart & { | ||
smart: typeof smart; | ||
statement: typeof statement; | ||
statements: typeof statements; | ||
expression: typeof expression; | ||
program: typeof program; | ||
}; | ||
|
||
export default Object.assign(smart.bind(undefined) as DefaultTemplateBuilder, { | ||
smart, | ||
statement, | ||
statements, | ||
expression, | ||
program, | ||
ast: smart.ast, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.