Issue
How to get the desired output? For a text file, when one of these conditions are met:
- the first field of a line has the string "type" or "const" (all lowercase)
- the line has "(" and ")" and do not have the string "ch" followed by a number (e.g. "ch6")
- a multiline which start with "{" somewhere of a line and end with "}" as the first character of a line
- the line start with comment "//"
- empty lines or lines of all white space
- a line has "{" and "}"
then print (as usual),
else insert comment "//" in the beginning of line and print
e.g. input:
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
output:
//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
I tried this script, which did not handle properly 'a multiline which start with "{" somewhere of a line and end with "}" as the first character of a line':
awk '{
if ($1 ~ /type/ || $1 ~ /const/ || $0 ~ /\(.*)/ || $0 ~ /{.*}/ || $0 ~ /\/\//)
print $0;
else
if ($0 !~ /}/)
print "//", $0;
else
print $0;
}' input
Solution
Using any awk:
$ cat tst.awk
{
comment = 0
if ( inBlock ) {
if ( /^}/ ) {
inBlock = 0
}
}
else if ( /\{$/ ) {
inBlock = 1
}
else if ( NF && !( \
( $1 ~ /^(type|const|\/\/)/ ) || \
( /\(.*)/ && !/ch[0-9]/ ) || \
( /\{.*}/ ) \
) ) {
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
Answered By - Ed Morton Answer Checked By - Timothy Miller (WPSolving Admin)