PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, December 14, 2022

[FIXED] What is the purpose of these extra curly braces within an else block?

 December 14, 2022     javascript, scope, syntax, typescript     No comments   

Issue

I'm looking over some typescript code, and I see extra curly braces being used within an else block. Do these extra braces serve any purpose? Or are they simply to add extra grouping to two almost identical code snippets? Given that this is a very popular Microsoft repo, it doubt its accidental.

  if (playwrightTestPackagePath) {
    require(playwrightTestPackagePath).addTestCommand(program);
    require(playwrightTestPackagePath).addShowReportCommand(program);
    require(playwrightTestPackagePath).addListFilesCommand(program);
  } else {
    {
      const command = program.command('test').allowUnknownOption(true);
      command.description('Run tests with Playwright Test. Available in @playwright/test package.');
      command.action(async () => {
        console.error('Please install @playwright/test package to use Playwright Test.');
        console.error('  npm install -D @playwright/test');
        process.exit(1);
      });
    }

    {
      const command = program.command('show-report').allowUnknownOption(true);
      command.description('Show Playwright Test HTML report. Available in @playwright/test package.');
      command.action(async () => {
        console.error('Please install @playwright/test package to use Playwright Test.');
        console.error('  npm install -D @playwright/test');
        process.exit(1);
      });
    }
  }
}

Source


Solution

It's just block for creating new scope for variables. You can see const command = ... - this can't be in one scope or error will be thrown: SyntaxError: redeclaration of const command.

// no errors
{
  const x = 1;
  console.log(x) // 1
}
{
  const x = 2;
  console.log(x) // 2
}
{
  const x = 3;
  console.log(x) // 3
}

// error
const x = 1
const x = 2 // Uncaught SyntaxError: redeclaration of const x
const x = 3

I think it's just for avoiding naming like command2, command3 etc.



Answered By - Фарид Ахмедов
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing