Skip to content

Commit 5bdc749

Browse files
author
andy-ms
committed
Enable 'no-unused-expression' tslint rule
1 parent bb7fb7d commit 5bdc749

File tree

8 files changed

+77
-70
lines changed

8 files changed

+77
-70
lines changed

src/compiler/checker.ts

+51-49
Original file line numberDiff line numberDiff line change
@@ -1907,8 +1907,9 @@ namespace ts {
19071907
* Extends one symbol table with another while collecting information on name collisions for error message generation into the `lookupTable` argument
19081908
* Not passing `lookupTable` and `exportNode` disables this collection, and just extends the tables
19091909
*/
1910-
function extendExportSymbols(target: SymbolTable, source: SymbolTable, lookupTable?: ExportCollisionTrackerTable, exportNode?: ExportDeclaration) {
1911-
source && source.forEach((sourceSymbol, id) => {
1910+
function extendExportSymbols(target: SymbolTable, source: SymbolTable | undefined, lookupTable?: ExportCollisionTrackerTable, exportNode?: ExportDeclaration) {
1911+
if (!source) return;
1912+
source.forEach((sourceSymbol, id) => {
19121913
if (id === "default") return;
19131914

19141915
const targetSymbol = target.get(id);
@@ -16989,8 +16990,7 @@ namespace ts {
1698916990
* @returns On success, the expression's signature's return type. On failure, anyType.
1699016991
*/
1699116992
function checkCallExpression(node: CallExpression | NewExpression): Type {
16992-
// Grammar checking; stop grammar-checking if checkGrammarTypeArguments return true
16993-
checkGrammarTypeArguments(node, node.typeArguments) || checkGrammarArguments(node.arguments);
16993+
if (!checkGrammarTypeArguments(node, node.typeArguments)) checkGrammarArguments(node.arguments);
1699416994

1699516995
const signature = getResolvedSignature(node);
1699616996

@@ -17037,7 +17037,7 @@ namespace ts {
1703717037

1703817038
function checkImportCallExpression(node: ImportCall): Type {
1703917039
// Check grammar of dynamic import
17040-
checkGrammarArguments(node.arguments) || checkGrammarImportCallExpression(node);
17040+
if (!checkGrammarArguments(node.arguments)) checkGrammarImportCallExpression(node);
1704117041

1704217042
if (node.arguments.length === 0) {
1704317043
return createPromiseReturnType(node, anyType);
@@ -18712,9 +18712,7 @@ namespace ts {
1871218712
// It is a SyntaxError if the Identifier "eval" or the Identifier "arguments" occurs as the
1871318713
// Identifier in a PropertySetParameterList of a PropertyAssignment that is contained in strict code
1871418714
// or if its FunctionBody is strict code(11.1.5).
18715-
18716-
// Grammar checking
18717-
checkGrammarDecorators(node) || checkGrammarModifiers(node);
18715+
checkGrammarDecoratorsAndModifiers(node);
1871818716

1871918717
checkVariableLikeDeclaration(node);
1872018718
const func = getContainingFunction(node);
@@ -19104,14 +19102,13 @@ namespace ts {
1910419102

1910519103
function checkPropertyDeclaration(node: PropertyDeclaration) {
1910619104
// Grammar checking
19107-
checkGrammarDecorators(node) || checkGrammarModifiers(node) || checkGrammarProperty(node) || checkGrammarComputedPropertyName(node.name);
19108-
19105+
if (!checkGrammarDecoratorsAndModifiers(node) && !checkGrammarProperty(node)) checkGrammarComputedPropertyName(node.name);
1910919106
checkVariableLikeDeclaration(node);
1911019107
}
1911119108

1911219109
function checkMethodDeclaration(node: MethodDeclaration) {
1911319110
// Grammar checking
19114-
checkGrammarMethod(node) || checkGrammarComputedPropertyName(node.name);
19111+
if (!checkGrammarMethod(node)) checkGrammarComputedPropertyName(node.name);
1911519112

1911619113
// Grammar checking for modifiers is done inside the function checkGrammarFunctionLikeDeclaration
1911719114
checkFunctionOrMethodDeclaration(node);
@@ -19127,7 +19124,7 @@ namespace ts {
1912719124
// Grammar check on signature of constructor and modifier of the constructor is done in checkSignatureDeclaration function.
1912819125
checkSignatureDeclaration(node);
1912919126
// Grammar check for checking only related to constructorDeclaration
19130-
checkGrammarConstructorTypeParameters(node) || checkGrammarConstructorTypeAnnotation(node);
19127+
if (!checkGrammarConstructorTypeParameters(node)) checkGrammarConstructorTypeAnnotation(node);
1913119128

1913219129
checkSourceElement(node.body);
1913319130
registerForUnusedIdentifiersCheck(node);
@@ -19224,7 +19221,7 @@ namespace ts {
1922419221
function checkAccessorDeclaration(node: AccessorDeclaration) {
1922519222
if (produceDiagnostics) {
1922619223
// Grammar checking accessors
19227-
checkGrammarFunctionLikeDeclaration(node) || checkGrammarAccessor(node) || checkGrammarComputedPropertyName(node.name);
19224+
if (!(checkGrammarFunctionLikeDeclaration(node) || checkGrammarAccessor(node))) checkGrammarComputedPropertyName(node.name);
1922819225

1922919226
checkDecorators(node);
1923019227
checkSignatureDeclaration(node);
@@ -20987,8 +20984,7 @@ namespace ts {
2098720984

2098820985
function checkVariableStatement(node: VariableStatement) {
2098920986
// Grammar checking
20990-
checkGrammarDecorators(node) || checkGrammarModifiers(node) || checkGrammarVariableDeclarationList(node.declarationList) || checkGrammarForDisallowedLetOrConstStatement(node);
20991-
20987+
if (!checkGrammarDecoratorsAndModifiers(node) && !checkGrammarVariableDeclarationList(node.declarationList)) checkGrammarForDisallowedLetOrConstStatement(node);
2099220988
forEach(node.declarationList.declarations, checkSourceElement);
2099320989
}
2099420990

@@ -21496,7 +21492,7 @@ namespace ts {
2149621492

2149721493
function checkBreakOrContinueStatement(node: BreakOrContinueStatement) {
2149821494
// Grammar checking
21499-
checkGrammarStatementInAmbientContext(node) || checkGrammarBreakOrContinueStatement(node);
21495+
if (!checkGrammarStatementInAmbientContext(node)) checkGrammarBreakOrContinueStatement(node);
2150021496

2150121497
// TODO: Check that target label is valid
2150221498
}
@@ -22177,7 +22173,7 @@ namespace ts {
2217722173

2217822174
function checkInterfaceDeclaration(node: InterfaceDeclaration) {
2217922175
// Grammar checking
22180-
checkGrammarDecorators(node) || checkGrammarModifiers(node) || checkGrammarInterfaceDeclaration(node);
22176+
if (!checkGrammarDecoratorsAndModifiers(node)) checkGrammarInterfaceDeclaration(node);
2218122177

2218222178
checkTypeParameters(node.typeParameters);
2218322179
if (produceDiagnostics) {
@@ -22219,7 +22215,7 @@ namespace ts {
2221922215

2222022216
function checkTypeAliasDeclaration(node: TypeAliasDeclaration) {
2222122217
// Grammar checking
22222-
checkGrammarDecorators(node) || checkGrammarModifiers(node);
22218+
checkGrammarDecoratorsAndModifiers(node);
2222322219

2222422220
checkTypeNameIsReserved(node.name, Diagnostics.Type_alias_name_cannot_be_0);
2222522221
checkTypeParameters(node.typeParameters);
@@ -22389,7 +22385,7 @@ namespace ts {
2238922385
}
2239022386

2239122387
// Grammar checking
22392-
checkGrammarDecorators(node) || checkGrammarModifiers(node);
22388+
checkGrammarDecoratorsAndModifiers(node);
2239322389

2239422390
checkTypeNameIsReserved(node.name, Diagnostics.Enum_name_cannot_be_0);
2239522391
checkCollisionWithCapturedThisVariable(node, node.name);
@@ -22492,7 +22488,7 @@ namespace ts {
2249222488
return;
2249322489
}
2249422490

22495-
if (!checkGrammarDecorators(node) && !checkGrammarModifiers(node)) {
22491+
if (!checkGrammarDecoratorsAndModifiers(node)) {
2249622492
if (!inAmbientContext && node.name.kind === SyntaxKind.StringLiteral) {
2249722493
grammarErrorOnNode(node.name, Diagnostics.Only_ambient_modules_can_use_quoted_names);
2249822494
}
@@ -22715,7 +22711,7 @@ namespace ts {
2271522711
// If we hit an import declaration in an illegal context, just bail out to avoid cascading errors.
2271622712
return;
2271722713
}
22718-
if (!checkGrammarDecorators(node) && !checkGrammarModifiers(node) && hasModifiers(node)) {
22714+
if (!checkGrammarDecoratorsAndModifiers(node) && hasModifiers(node)) {
2271922715
grammarErrorOnFirstToken(node, Diagnostics.An_import_declaration_cannot_have_modifiers);
2272022716
}
2272122717
if (checkExternalImportOrExportDeclaration(node)) {
@@ -22742,7 +22738,7 @@ namespace ts {
2274222738
return;
2274322739
}
2274422740

22745-
checkGrammarDecorators(node) || checkGrammarModifiers(node);
22741+
checkGrammarDecoratorsAndModifiers(node);
2274622742
if (isInternalModuleImportEqualsDeclaration(node) || checkExternalImportOrExportDeclaration(node)) {
2274722743
checkImportBinding(node);
2274822744
if (hasModifier(node, ModifierFlags.Export)) {
@@ -22778,7 +22774,7 @@ namespace ts {
2277822774
return;
2277922775
}
2278022776

22781-
if (!checkGrammarDecorators(node) && !checkGrammarModifiers(node) && hasModifiers(node)) {
22777+
if (!checkGrammarDecoratorsAndModifiers(node) && hasModifiers(node)) {
2278222778
grammarErrorOnFirstToken(node, Diagnostics.An_export_declaration_cannot_have_modifiers);
2278322779
}
2278422780

@@ -22851,7 +22847,7 @@ namespace ts {
2285122847
return;
2285222848
}
2285322849
// Grammar checking
22854-
if (!checkGrammarDecorators(node) && !checkGrammarModifiers(node) && hasModifiers(node)) {
22850+
if (!checkGrammarDecoratorsAndModifiers(node) && hasModifiers(node)) {
2285522851
grammarErrorOnFirstToken(node, Diagnostics.An_export_assignment_cannot_have_modifiers);
2285622852
}
2285722853
if (node.expression.kind === SyntaxKind.Identifier) {
@@ -22896,29 +22892,31 @@ namespace ts {
2289622892
}
2289722893
// Checks for export * conflicts
2289822894
const exports = getExportsOfModule(moduleSymbol);
22899-
exports && exports.forEach(({ declarations, flags }, id) => {
22900-
if (id === "__export") {
22901-
return;
22902-
}
22903-
// ECMA262: 15.2.1.1 It is a Syntax Error if the ExportedNames of ModuleItemList contains any duplicate entries.
22904-
// (TS Exceptions: namespaces, function overloads, enums, and interfaces)
22905-
if (flags & (SymbolFlags.Namespace | SymbolFlags.Interface | SymbolFlags.Enum)) {
22906-
return;
22907-
}
22908-
const exportedDeclarationsCount = countWhere(declarations, isNotOverloadAndNotAccessor);
22909-
if (flags & SymbolFlags.TypeAlias && exportedDeclarationsCount <= 2) {
22910-
// it is legal to merge type alias with other values
22911-
// so count should be either 1 (just type alias) or 2 (type alias + merged value)
22912-
return;
22913-
}
22914-
if (exportedDeclarationsCount > 1) {
22915-
for (const declaration of declarations) {
22916-
if (isNotOverload(declaration)) {
22917-
diagnostics.add(createDiagnosticForNode(declaration, Diagnostics.Cannot_redeclare_exported_variable_0, unescapeLeadingUnderscores(id)));
22895+
if (exports) {
22896+
exports.forEach(({ declarations, flags }, id) => {
22897+
if (id === "__export") {
22898+
return;
22899+
}
22900+
// ECMA262: 15.2.1.1 It is a Syntax Error if the ExportedNames of ModuleItemList contains any duplicate entries.
22901+
// (TS Exceptions: namespaces, function overloads, enums, and interfaces)
22902+
if (flags & (SymbolFlags.Namespace | SymbolFlags.Interface | SymbolFlags.Enum)) {
22903+
return;
22904+
}
22905+
const exportedDeclarationsCount = countWhere(declarations, isNotOverloadAndNotAccessor);
22906+
if (flags & SymbolFlags.TypeAlias && exportedDeclarationsCount <= 2) {
22907+
// it is legal to merge type alias with other values
22908+
// so count should be either 1 (just type alias) or 2 (type alias + merged value)
22909+
return;
22910+
}
22911+
if (exportedDeclarationsCount > 1) {
22912+
for (const declaration of declarations) {
22913+
if (isNotOverload(declaration)) {
22914+
diagnostics.add(createDiagnosticForNode(declaration, Diagnostics.Cannot_redeclare_exported_variable_0, unescapeLeadingUnderscores(id)));
22915+
}
2291822916
}
2291922917
}
22920-
}
22921-
});
22918+
});
22919+
}
2292222920
links.exportsChecked = true;
2292322921
}
2292422922
}
@@ -24551,12 +24549,16 @@ namespace ts {
2455124549
}
2455224550

2455324551
// GRAMMAR CHECKING
24552+
function checkGrammarDecoratorsAndModifiers(node: Node): boolean {
24553+
return checkGrammarDecorators(node) || checkGrammarModifiers(node);
24554+
}
24555+
2455424556
function checkGrammarDecorators(node: Node): boolean {
2455524557
if (!node.decorators) {
2455624558
return false;
2455724559
}
2455824560
if (!nodeCanBeDecorated(node)) {
24559-
if (node.kind === SyntaxKind.MethodDeclaration && !ts.nodeIsPresent((<MethodDeclaration>node).body)) {
24561+
if (node.kind === SyntaxKind.MethodDeclaration && !nodeIsPresent((<MethodDeclaration>node).body)) {
2456024562
return grammarErrorOnFirstToken(node, Diagnostics.A_decorator_can_only_decorate_a_method_implementation_not_an_overload);
2456124563
}
2456224564
else {
@@ -24906,7 +24908,7 @@ namespace ts {
2490624908
function checkGrammarFunctionLikeDeclaration(node: FunctionLikeDeclaration): boolean {
2490724909
// Prevent cascading error by short-circuit
2490824910
const file = getSourceFileOfNode(node);
24909-
return checkGrammarDecorators(node) || checkGrammarModifiers(node) || checkGrammarTypeParameterList(node.typeParameters, file) ||
24911+
return checkGrammarDecoratorsAndModifiers(node) || checkGrammarTypeParameterList(node.typeParameters, file) ||
2491024912
checkGrammarParameterList(node.parameters) || checkGrammarArrowFunction(node, file);
2491124913
}
2491224914

@@ -24962,7 +24964,7 @@ namespace ts {
2496224964

2496324965
function checkGrammarIndexSignature(node: SignatureDeclaration) {
2496424966
// Prevent cascading error by short-circuit
24965-
return checkGrammarDecorators(node) || checkGrammarModifiers(node) || checkGrammarIndexSignatureParameters(node);
24967+
return checkGrammarDecoratorsAndModifiers(node) || checkGrammarIndexSignatureParameters(node);
2496624968
}
2496724969

2496824970
function checkGrammarForAtLeastOneTypeArgument(node: Node, typeArguments: NodeArray<TypeNode>): boolean {
@@ -25013,7 +25015,7 @@ namespace ts {
2501325015
let seenExtendsClause = false;
2501425016
let seenImplementsClause = false;
2501525017

25016-
if (!checkGrammarDecorators(node) && !checkGrammarModifiers(node) && node.heritageClauses) {
25018+
if (!checkGrammarDecoratorsAndModifiers(node) && node.heritageClauses) {
2501725019
for (const heritageClause of node.heritageClauses) {
2501825020
if (heritageClause.token === SyntaxKind.ExtendsKeyword) {
2501925021
if (seenExtendsClause) {

src/harness/fourslash.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,9 @@ namespace FourSlash {
221221
private addMatchedInputFile(referenceFilePath: string, extensions: ReadonlyArray<string>) {
222222
const inputFiles = this.inputFiles;
223223
const languageServiceAdapterHost = this.languageServiceAdapterHost;
224-
if (!extensions) {
225-
tryAdd(referenceFilePath);
226-
}
227-
else {
228-
tryAdd(referenceFilePath) || ts.forEach(extensions, ext => tryAdd(referenceFilePath + ext));
224+
const didAdd = tryAdd(referenceFilePath);
225+
if (extensions && !didAdd) {
226+
ts.forEach(extensions, ext => tryAdd(referenceFilePath + ext));
229227
}
230228

231229
function tryAdd(path: string) {

src/harness/parallel/worker.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ namespace Harness.Parallel.Worker {
5757
return cleanup();
5858
}
5959
try {
60-
beforeFunc && beforeFunc();
60+
if (beforeFunc) {
61+
beforeFunc();
62+
}
6163
}
6264
catch (e) {
6365
errors.push({ error: `Error executing before function: ${e.message}`, stack: e.stack, name: [...namestack] });
@@ -69,7 +71,9 @@ namespace Harness.Parallel.Worker {
6971
testList.forEach(({ name, callback, kind }) => executeCallback(name, callback, kind));
7072

7173
try {
72-
afterFunc && afterFunc();
74+
if (afterFunc) {
75+
afterFunc();
76+
}
7377
}
7478
catch (e) {
7579
errors.push({ error: `Error executing after function: ${e.message}`, stack: e.stack, name: [...namestack] });

src/harness/unittests/languageService.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export function Component(x: Config): any;`
4545
readDirectory: noop as any,
4646
});
4747
const definitions = languageService.getDefinitionAtPosition("foo.ts", 160); // 160 is the latter `vueTemplateHtml` position
48-
expect(definitions).to.exist;
48+
expect(definitions).to.exist; // tslint:disable-line no-unused-expression
4949
});
5050
});
5151
}

src/harness/unittests/session.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ namespace ts.server {
317317

318318
session.send = Session.prototype.send;
319319
assert(session.send);
320-
expect(session.send(msg)).to.not.exist;
320+
expect(session.send(msg)).to.not.exist; // tslint:disable-line no-unused-expression
321321
expect(lastWrittenToHost).to.equal(resultMsg);
322322
});
323323
});
@@ -524,14 +524,14 @@ namespace ts.server {
524524
});
525525
});
526526
it("has access to the project service", () => {
527-
class ServiceSession extends TestSession {
527+
// tslint:disable-next-line no-unused-expression
528+
new class extends TestSession {
528529
constructor() {
529530
super();
530531
assert(this.projectService);
531532
expect(this.projectService).to.be.instanceOf(ProjectService);
532533
}
533-
}
534-
new ServiceSession();
534+
}();
535535
});
536536
});
537537

0 commit comments

Comments
 (0)