LoginSignup
0
0

More than 5 years have passed since last update.

定数エクスポートをCompiler APIを使ってTypeScriptのASTをつくる

Last updated at Posted at 2019-01-24
import ts from 'typescript';

ts.createVariableStatement(
    ts.createModifiersFromModifierFlags(ts.ModifierFlags.Const | ts.ModifierFlags.Export),
    ts.createVariableDeclaration('name', undefined, ts.createStringLiteral('value'))
);

結果

export const name = "value";

出力する方法は略。
see also https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#creating-and-printing-a-typescript-ast

メモ

  • createModifiersFromModifierFlagsがModifierのリストを返してる事に気づかなくてはまった
    • exportとかconstがでなくてはまった。
  • createVariableStatementの第2引数に VariableDecrationのリストを渡すと変な動きをしてはまった
    • export const var name = 'value';みたいになった

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0