如何得到想要的输出? 对于文本文件,当满足以下条件之一时:
然后打印(像往常一样),
否则在行首插入注释“//”并打印
例如 输入:
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
以“{”开始并以“}”结束的多行 作为一行的第一个字符
getline
函数的任务,为了简单起见,让我们考虑只做两件事情的情况,即上述条件和 一行的第一个字段有字符串“type”或“const”(全部小写) 并且您正在使用以下file.txt
this should become comment
type Boolean = 0 | 1
this too should become comment
function func(){
if(true){
console.log("This is not comment")
return 123
}
}
this also should comment
然后
awk '$1~/type|const/{print;next}/{/{print;while(!/^}/){getline;print};next}{print "//",$0}' file.txt
给出输出
// this should become comment
type Boolean = 0 | 1
// this too should become comment
function func(){
if(true){
console.log("This is not comment")
return 123
}
}
// this also should comment
解释:如果第一个字段包含
type
或const
,那么print
按原样行并转到next
行,如果行包含{
然后print
按原样使用重复print
ing 行直到以 }
字符开头的行,完成后转到 next
行。所有尚未完成的行都将以 //
和输出字段分隔符(默认为空格)为前缀。
(在 GNU Awk 5.0.1 中测试)
这满足您列出的要求,使用任何 awk:
$ cat tst.awk
{
comment = 0
if ( inBlock ) {
if ( /^}/ ) {
inBlock = 0
}
}
else if ( /.*{$/ ) {
inBlock = 1
}
else if ( !( ($1 ~ /^(type|const|\/\/)/) || /\(.*)/ ) ) {
comment = 1
}
}
comment {
$0 = "//" $0
}
{ print }
$ awk -f tst.awk file
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
但它不会根据您显示的输入产生您显示的预期输出,因为预期输出不符合您列出的要求,请参阅我在您的问题下的 3 条评论。
如果我们在您的“请勿评论”列表中添加 2 个要求,我们可以更接近您发布的预期输出:
可以通过将上面的一行调整为:
$ cat tst.awk
{
comment = 0
if ( inBlock ) {
if ( /^}/ ) {
inBlock = 0
}
}
else if ( /.*{$/ ) {
inBlock = 1
}
else if ( NF && !( ($1 ~ /^(type|const|\/\/)/) || /\(.*)/ || /\{.*}/ ) ) {
comment = 1
}
}
comment {
$0 = "//" $0
}
{ print }
$ awk -f tst.awk file
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
但不评论这个:
renderFriendList(response.user.friendList)
因为他们都满足你的要求:
• eslint jsx-a11y label must associated with a control error
• 为什么我有时会看到“条目‘文件名’不是最新的。无法合并。”在“git reset --hard”和“git pull”之后?
• sphinx gettext 在先前匹配的 msg 前面插入空引号“”
• 在这个 Laravel 和 Alpine 应用程序中导致“l 不是函数”错误的原因是什么?
• jQuery 搜索页面过滤器 - 仅显示包含匹配内容的 h4 标题,并在未找到匹配项时返回“不匹配”
• 错误:Windows Server 2022 配置文件中的“重复集合条目”
• A 想用 apollo-federation 中的帖子扩展评论
• Laravel:带条件的多态关系(morphTo、morphMany)或常规关系(hasOne、hasMany)?