插入评论//当条件不匹配

问题描述 投票:0回答:0

如何得到想要的输出? 对于文本文件,当满足以下条件之一时:

  • 一行的第一个字段有字符串“type”或“const”(全部小写)
  • 该行有“(”和“)”
  • 以“{”开头的多行,以“}”作为一行的第一个字符结束
  • 以注释“//”开头的行 然后打印(像往常一样) 否则在行首插入注释“//”并打印

例如 输入:

TypeScript ch6 (2/3) Advanced Function Types
 Types TypeScript  
  Totality TSC Flag: noImplicitReturns Advanced Object Types
   The keyof operator TSC Flag: keyofStringsOnly The Record Type Mapped Types Built-in mapped types 
type Weekday = 'Mon' | 'Tue' | 'Wed'|'Thu'|'Fri'

type Day = Weekday | 'Sat' | 'Sun'
// Error: Function lacks ending return statement and return type does not include 'undefined'.
function getNextDay(w: Weekday): Day {
  switch(w) {
    case 'Mon': return 'Tue'
  }
}
{  }
{
}
{
  {
    test
  }
}
const i = 1
 TSC Flag: noImplicitReturns tsconfig"noImplicitReturns": true // Error: Not all code paths return a value.
function isBig(n: number) {
  if (n >= 100) {
    return true
  }
}
 Advanced Object Types Type Operators for Object Types 
 Companion Object Pattern
type APIResponse = {
  user: {
    userId: string,
    friendList: {
      count: number,
      friends: {
        firstName: string,
        lastName: string
      }[]
    }
  }
}
function getAPIResponse(): Promise<APIResponse> {
  // ...
}
const response = await getAPIResponse()
renderFriendList(response.user.friendList)
 friendListtypehint any FriendList

输出:

//TypeScript ch6 (2/3) Advanced Function Types
// Types TypeScript  
//  Totality TSC Flag: noImplicitReturns Advanced Object Types
//   The keyof operator TSC Flag: keyofStringsOnly The Record Type Mapped Types Built-in mapped types 
type Weekday = 'Mon' | 'Tue' | 'Wed'|'Thu'|'Fri'

type Day = Weekday | 'Sat' | 'Sun'
// Error: Function lacks ending return statement and return type does not include 'undefined'.
function getNextDay(w: Weekday): Day {
  switch(w) {
    case 'Mon': return 'Tue'
  }
}
{  }
{
}
{
  {
    test
  }
}
const i = 1
// TSC Flag: noImplicitReturns tsconfig"noImplicitReturns": true // Error: Not all code paths return a value.
function isBig(n: number) {
  if (n >= 100) {
    return true
  }
}
// Advanced Object Types Type Operators for Object Types 
// Companion Object Pattern
type APIResponse = {
  user: {
    userId: string,
    friendList: {
      count: number,
      friends: {
        firstName: string,
        lastName: string
      }[]
    }
  }
}
function getAPIResponse(): Promise<APIResponse> {
  // ...
}
const response = await getAPIResponse()
renderFriendList(response.user.friendList)
// friendListtypehint any FriendList

我试过这个脚本,它没有正确处理“以“{”开头并以“}”作为一行的第一个字符的多行”:

awk '{
  if ($1 ~ /type/ || $1 ~ /const/ || $0 ~ /\(.*)/ || $0 ~ /{.*}/ || $0 ~ /\/\//)
    print $0;
  else
    if ($0 !~ /}/)
      print "//", $0;
    else 
      print $0;
}' input
awk sed
© www.soinside.com 2019 - 2024. All rights reserved.