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);
});
}
}
}
CodePudding user response:
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.