ts-morph#VariableStatement TypeScript Examples
The following examples show how to use
ts-morph#VariableStatement.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: export.ts From cli with MIT License | 6 votes |
// 获取所有的导出语句(可根据export的identifier查找)
export function getExportVariableStatements(
source: SourceFile,
varIdentifier?: string
): VariableStatement | VariableStatement[] {
const topLevelVarStatements = source
.getFirstChildByKind(SyntaxKind.SyntaxList)
.getChildrenOfKind(SyntaxKind.VariableStatement);
const exportVarStatements = topLevelVarStatements.filter(v => {
const syntaxBeforeVarIdentifier = v.getFirstChildIfKind(
SyntaxKind.SyntaxList
);
return (
syntaxBeforeVarIdentifier &&
syntaxBeforeVarIdentifier.getText() &&
syntaxBeforeVarIdentifier.getText() === 'export'
);
});
if (varIdentifier) {
return exportVarStatements.find(statement => {
return (
statement
.getFirstChildByKind(SyntaxKind.VariableDeclarationList)
.getFirstChildByKind(SyntaxKind.SyntaxList)
.getFirstChildByKind(SyntaxKind.VariableDeclaration)
.getFirstChildByKind(SyntaxKind.Identifier)
.getText() === varIdentifier
);
});
}
return exportVarStatements;
}
Example #2
Source File: config.ts From cli with MIT License | 5 votes |
// config.x = {}
// 仅适用于默认导出方法形式
export function addConfigKey(
source: SourceFile,
key: string,
value: any,
apply = true
) {
const arrowFunc = source
.getFirstChildByKind(SyntaxKind.SyntaxList)
.getFirstChildByKind(SyntaxKind.ExportAssignment)
.getFirstChildByKind(SyntaxKind.ArrowFunction);
const returnStatement = arrowFunc
.getFirstChildByKind(SyntaxKind.Block)
.getFirstChildByKind(SyntaxKind.ReturnStatement);
const stripedReturnStatement = strip(returnStatement.getText());
const configVarIdentifier = arrowFunc
.getBody()
// const config = {} as DefaultConfig;
.getFirstChildByKind(SyntaxKind.VariableStatement)
.getFirstChildByKind(SyntaxKind.VariableDeclarationList)
.getFirstChildByKind(SyntaxKind.SyntaxList)
// [ 'Identifier', 'EqualsToken', 'AsExpression' ]
.getFirstChildByKind(SyntaxKind.VariableDeclaration)
.getFirstChildByKind(SyntaxKind.Identifier)
.getText();
const existPropAssignmentKeys = arrowFunc
.getBody()
.getFirstChildByKind(SyntaxKind.SyntaxList)
.getChildrenOfKind(SyntaxKind.ExpressionStatement)
.map(child => {
const propsAssignTokens = child
// a.b = x
.getFirstChildByKind(SyntaxKind.BinaryExpression)
// a.b
.getFirstChildByKind(SyntaxKind.PropertyAccessExpression);
return propsAssignTokens;
})
.map(prop => {
// [ 'Identifier', 'DotToken', 'Identifier' ]
const children = prop.getChildren();
if (
children.length === 3 &&
children[1].getKind() === SyntaxKind.DotToken
) {
return children[2].getText();
}
});
if (existPropAssignmentKeys.includes(key)) {
console.error(`${configVarIdentifier}.${key} exist !`);
process.exit(0);
}
returnStatement.remove();
// FIXME:
arrowFunc.addStatements(
`${configVarIdentifier}.${key} = ${JSON.stringify(value)}`
);
arrowFunc.addStatements(stripedReturnStatement);
apply && source.saveSync();
// const absWritePath = source.getFilePath();
// const formatted = prettier.format(
// fs.readFileSync(absWritePath, { encoding: 'utf8' }),
// {
// parser: 'typescript',
// }
// );
// fs.writeFileSync(absWritePath, formatted);
}