diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index 2eca9dca05c4a..47aa4b53f54b1 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -70,6 +70,13 @@ namespace ts { category: Diagnostics.Command_line_Options, description: Diagnostics.Watch_input_files, }, + { + name: "treatWarningsAsErrors", + type: "boolean", + showInSimplifiedHelpView: true, + category: Diagnostics.Command_line_Options, + description: Diagnostics.Treat_warnings_as_errors + }, // Basic { diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index f33cc4449f936..2bb04abebd3bd 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -3115,7 +3115,7 @@ "code": 6132 }, "'{0}' is declared but its value is never read.": { - "category": "Error", + "category": "Warning", "code": 6133 }, "Report errors on unused locals.": { @@ -3399,19 +3399,19 @@ "code": 7026 }, "Unreachable code detected.": { - "category": "Error", + "category": "Warning", "code": 7027 }, "Unused label.": { - "category": "Error", + "category": "Warning", "code": 7028 }, "Fallthrough case in switch.": { - "category": "Error", + "category": "Warning", "code": 7029 }, "Not all code paths return a value.": { - "category": "Error", + "category": "Warning", "code": 7030 }, "Binding element '{0}' implicitly has an '{1}' type.": { @@ -3438,6 +3438,10 @@ "category": "Error", "code": 7036 }, + "Treat warnings as errors": { + "category": "Warning", + "code": 7037 + }, "You cannot rename this element.": { "category": "Error", diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 1d325983f5fc6..aeefe9c7bf939 100755 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -279,6 +279,7 @@ namespace ts { let output = ""; for (const diagnostic of diagnostics) { let context = ""; + const categoryColor = getCategoryFormat(diagnostic.category); if (diagnostic.file) { const { start, length, file } = diagnostic; const { line: firstLine, character: firstLineChar } = getLineAndCharacterOfPosition(file, start); @@ -313,7 +314,7 @@ namespace ts { // Output the gutter and the error span for the line using tildes. context += formatAndReset(padLeft("", gutterWidth), gutterStyleSequence) + gutterSeparator; - context += redForegroundEscapeSequence; + context += categoryColor; if (i === firstLine) { // If we're on the last line, then limit it to the last character of the last line. // Otherwise, we'll just squiggle the rest of the line, giving 'slice' no end position. @@ -336,7 +337,6 @@ namespace ts { output += `${ relativeFileName }(${ firstLine + 1 },${ firstLineChar + 1 }): `; } - const categoryColor = getCategoryFormat(diagnostic.category); const category = DiagnosticCategory[diagnostic.category].toLowerCase(); output += `${ formatAndReset(category, categoryColor) } TS${ diagnostic.code }: ${ flattenDiagnosticMessageText(diagnostic.messageText, host.getNewLine()) }`; @@ -1142,12 +1142,14 @@ namespace ts { ...program.getGlobalDiagnostics(cancellationToken), ...program.getSemanticDiagnostics(sourceFile, cancellationToken) ]; + const hasError = some(diagnostics, d => d.category === DiagnosticCategory.Error); - if (diagnostics.length === 0 && program.getCompilerOptions().declaration) { + if (!hasError && program.getCompilerOptions().declaration) { declarationDiagnostics = program.getDeclarationDiagnostics(/*sourceFile*/ undefined, cancellationToken); } - if (diagnostics.length > 0 || declarationDiagnostics.length > 0) { + const hasDeclarationError = some(declarationDiagnostics, d => d.category === DiagnosticCategory.Error); + if (hasError || hasDeclarationError) { return { diagnostics: concatenate(diagnostics, declarationDiagnostics), sourceMaps: undefined, @@ -1292,10 +1294,21 @@ namespace ts { }); } + function shouldTreatWarningsAsErrors() { + const setValue = program.getCompilerOptions().treatWarningsAsErrors; + if (typeof setValue === "undefined") { + return true; + } + return setValue; + } + /** * Skip errors if previous line start with '// @ts-ignore' comment, not counting non-empty non-comment lines */ function shouldReportDiagnostic(diagnostic: Diagnostic) { + if (diagnostic.category === DiagnosticCategory.Warning && shouldTreatWarningsAsErrors()) { + diagnostic.category = DiagnosticCategory.Error; + } const { file, start } = diagnostic; if (file) { const lineStarts = getLineStarts(file); diff --git a/src/compiler/tsc.ts b/src/compiler/tsc.ts index 680daeeb4856f..983a427796975 100644 --- a/src/compiler/tsc.ts +++ b/src/compiler/tsc.ts @@ -182,11 +182,11 @@ namespace ts { // If we didn't have any syntactic errors, then also try getting the global and // semantic errors. - if (diagnostics.length === 0) { - diagnostics = program.getOptionsDiagnostics().concat(program.getGlobalDiagnostics()); + if (!some(diagnostics, d => d.category === DiagnosticCategory.Error)) { + diagnostics = diagnostics.concat(program.getOptionsDiagnostics().concat(program.getGlobalDiagnostics())); - if (diagnostics.length === 0) { - diagnostics = program.getSemanticDiagnostics().slice(); + if (!some(diagnostics, d => d.category === DiagnosticCategory.Error)) { + diagnostics = diagnostics.concat(program.getSemanticDiagnostics()); } } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 3f9b492f90342..48e18189d5625 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -3764,6 +3764,8 @@ namespace ts { /*@internal*/ version?: boolean; /*@internal*/ watch?: boolean; + treatWarningsAsErrors?: boolean; + [option: string]: CompilerOptionsValue | JsonSourceFile | undefined; } diff --git a/src/compiler/watch.ts b/src/compiler/watch.ts index 53692fe1e0488..bba5891743272 100644 --- a/src/compiler/watch.ts +++ b/src/compiler/watch.ts @@ -114,11 +114,12 @@ namespace ts { }); } - if (emitSkipped && diagnostics.length > 0) { + const hasError = some(diagnostics, d => d.category === DiagnosticCategory.Error); + if (emitSkipped && hasError) { // If the emitter didn't emit anything, then pass that value along. return ExitStatus.DiagnosticsPresent_OutputsSkipped; } - else if (diagnostics.length > 0) { + else if (hasError) { // The emitter emitted something, inform the caller if that happened in the presence // of diagnostics or not. return ExitStatus.DiagnosticsPresent_OutputsGenerated; @@ -149,11 +150,11 @@ namespace ts { // If we didn't have any syntactic errors, then also try getting the global and // semantic errors. - if (diagnostics.length === 0) { + if (!some(diagnostics, d => d.category === DiagnosticCategory.Error)) { addRange(diagnostics, program.getOptionsDiagnostics()); addRange(diagnostics, program.getGlobalDiagnostics()); - if (diagnostics.length === 0) { + if (!some(diagnostics, d => d.category === DiagnosticCategory.Error)) { reportSemanticDiagnostics = true; } } diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index ee118279bcb18..0241b9eab4f40 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -2269,6 +2269,7 @@ declare namespace ts { types?: string[]; /** Paths used to compute primary types search locations */ typeRoots?: string[]; + treatWarningsAsErrors?: boolean; [option: string]: CompilerOptionsValue | JsonSourceFile | undefined; } interface TypeAcquisition { diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index 89591ca1886fb..7e501c50337cf 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -2269,6 +2269,7 @@ declare namespace ts { types?: string[]; /** Paths used to compute primary types search locations */ typeRoots?: string[]; + treatWarningsAsErrors?: boolean; [option: string]: CompilerOptionsValue | JsonSourceFile | undefined; } interface TypeAcquisition { diff --git a/tests/baselines/reference/treatWarningsAsErrorsFalse.errors.txt b/tests/baselines/reference/treatWarningsAsErrorsFalse.errors.txt new file mode 100644 index 0000000000000..87c23cdc394ec --- /dev/null +++ b/tests/baselines/reference/treatWarningsAsErrorsFalse.errors.txt @@ -0,0 +1,39 @@ +tests/cases/compiler/treatWarningsAsErrorsFalse.ts(1,14): warning TS6133: 'p' is declared but its value is never read. +tests/cases/compiler/treatWarningsAsErrorsFalse.ts(2,5): warning TS7028: Unused label. +tests/cases/compiler/treatWarningsAsErrorsFalse.ts(5,11): warning TS6133: 'y' is declared but its value is never read. +tests/cases/compiler/treatWarningsAsErrorsFalse.ts(7,9): warning TS7029: Fallthrough case in switch. +tests/cases/compiler/treatWarningsAsErrorsFalse.ts(9,14): error TS2678: Type '"b"' is not comparable to type '"a"'. +tests/cases/compiler/treatWarningsAsErrorsFalse.ts(10,13): warning TS7030: Not all code paths return a value. +tests/cases/compiler/treatWarningsAsErrorsFalse.ts(11,13): warning TS7027: Unreachable code detected. + + +==== tests/cases/compiler/treatWarningsAsErrorsFalse.ts (7 errors) ==== + function foo(p: any): string { // unused parameter + ~ +!!! warning TS6133: 'p' is declared but its value is never read. + foo: while (false) {} // unused label + ~~~ +!!! warning TS7028: Unused label. + + const x: { kind: "a" } | { kind: "b" } = { kind: "a" }; + const y = "any"; // unused variable + ~ +!!! warning TS6133: 'y' is declared but its value is never read. + switch (x.kind) { + case "a": + ~~~~ +!!! warning TS7029: Fallthrough case in switch. + void x; // implicit fallthrough + case "b": + ~~~ +!!! error TS2678: Type '"b"' is not comparable to type '"a"'. + return; // implicit return + ~~~~~~~ +!!! warning TS7030: Not all code paths return a value. + if (x === x) { // unreachable code + ~~ +!!! warning TS7027: Unreachable code detected. + void x; + } + } + } \ No newline at end of file diff --git a/tests/baselines/reference/treatWarningsAsErrorsFalse.js b/tests/baselines/reference/treatWarningsAsErrorsFalse.js new file mode 100644 index 0000000000000..dd1a7de25d287 --- /dev/null +++ b/tests/baselines/reference/treatWarningsAsErrorsFalse.js @@ -0,0 +1,32 @@ +//// [treatWarningsAsErrorsFalse.ts] +function foo(p: any): string { // unused parameter + foo: while (false) {} // unused label + + const x: { kind: "a" } | { kind: "b" } = { kind: "a" }; + const y = "any"; // unused variable + switch (x.kind) { + case "a": + void x; // implicit fallthrough + case "b": + return; // implicit return + if (x === x) { // unreachable code + void x; + } + } +} + +//// [treatWarningsAsErrorsFalse.js] +function foo(p) { + foo: while (false) { } // unused label + var x = { kind: "a" }; + var y = "any"; // unused variable + switch (x.kind) { + case "a": + void x; // implicit fallthrough + case "b": + return; // implicit return + if (x === x) { + void x; + } + } +} diff --git a/tests/baselines/reference/treatWarningsAsErrorsFalse.symbols b/tests/baselines/reference/treatWarningsAsErrorsFalse.symbols new file mode 100644 index 0000000000000..5867be9e4a041 --- /dev/null +++ b/tests/baselines/reference/treatWarningsAsErrorsFalse.symbols @@ -0,0 +1,36 @@ +=== tests/cases/compiler/treatWarningsAsErrorsFalse.ts === +function foo(p: any): string { // unused parameter +>foo : Symbol(foo, Decl(treatWarningsAsErrorsFalse.ts, 0, 0)) +>p : Symbol(p, Decl(treatWarningsAsErrorsFalse.ts, 0, 13)) + + foo: while (false) {} // unused label + + const x: { kind: "a" } | { kind: "b" } = { kind: "a" }; +>x : Symbol(x, Decl(treatWarningsAsErrorsFalse.ts, 3, 9)) +>kind : Symbol(kind, Decl(treatWarningsAsErrorsFalse.ts, 3, 14)) +>kind : Symbol(kind, Decl(treatWarningsAsErrorsFalse.ts, 3, 30)) +>kind : Symbol(kind, Decl(treatWarningsAsErrorsFalse.ts, 3, 46)) + + const y = "any"; // unused variable +>y : Symbol(y, Decl(treatWarningsAsErrorsFalse.ts, 4, 9)) + + switch (x.kind) { +>x.kind : Symbol(kind, Decl(treatWarningsAsErrorsFalse.ts, 3, 14)) +>x : Symbol(x, Decl(treatWarningsAsErrorsFalse.ts, 3, 9)) +>kind : Symbol(kind, Decl(treatWarningsAsErrorsFalse.ts, 3, 14)) + + case "a": + void x; // implicit fallthrough +>x : Symbol(x, Decl(treatWarningsAsErrorsFalse.ts, 3, 9)) + + case "b": + return; // implicit return + if (x === x) { // unreachable code +>x : Symbol(x, Decl(treatWarningsAsErrorsFalse.ts, 3, 9)) +>x : Symbol(x, Decl(treatWarningsAsErrorsFalse.ts, 3, 9)) + + void x; +>x : Symbol(x, Decl(treatWarningsAsErrorsFalse.ts, 3, 9)) + } + } +} diff --git a/tests/baselines/reference/treatWarningsAsErrorsFalse.types b/tests/baselines/reference/treatWarningsAsErrorsFalse.types new file mode 100644 index 0000000000000..6f3c8088734e1 --- /dev/null +++ b/tests/baselines/reference/treatWarningsAsErrorsFalse.types @@ -0,0 +1,48 @@ +=== tests/cases/compiler/treatWarningsAsErrorsFalse.ts === +function foo(p: any): string { // unused parameter +>foo : (p: any) => string +>p : any + + foo: while (false) {} // unused label +>foo : any +>false : false + + const x: { kind: "a" } | { kind: "b" } = { kind: "a" }; +>x : { kind: "a"; } | { kind: "b"; } +>kind : "a" +>kind : "b" +>{ kind: "a" } : { kind: "a"; } +>kind : string +>"a" : "a" + + const y = "any"; // unused variable +>y : "any" +>"any" : "any" + + switch (x.kind) { +>x.kind : "a" +>x : { kind: "a"; } +>kind : "a" + + case "a": +>"a" : "a" + + void x; // implicit fallthrough +>void x : undefined +>x : { kind: "a"; } + + case "b": +>"b" : "b" + + return; // implicit return + if (x === x) { // unreachable code +>x === x : boolean +>x : { kind: "a"; } | { kind: "b"; } +>x : { kind: "a"; } | { kind: "b"; } + + void x; +>void x : undefined +>x : { kind: "a"; } | { kind: "b"; } + } + } +} diff --git a/tests/baselines/reference/treatWarningsAsErrorsFalseNoEmitOnError.errors.txt b/tests/baselines/reference/treatWarningsAsErrorsFalseNoEmitOnError.errors.txt new file mode 100644 index 0000000000000..4fb888d6064b8 --- /dev/null +++ b/tests/baselines/reference/treatWarningsAsErrorsFalseNoEmitOnError.errors.txt @@ -0,0 +1,36 @@ +tests/cases/compiler/treatWarningsAsErrorsFalseNoEmitOnError.ts(1,14): warning TS6133: 'p' is declared but its value is never read. +tests/cases/compiler/treatWarningsAsErrorsFalseNoEmitOnError.ts(2,5): warning TS7028: Unused label. +tests/cases/compiler/treatWarningsAsErrorsFalseNoEmitOnError.ts(5,11): warning TS6133: 'y' is declared but its value is never read. +tests/cases/compiler/treatWarningsAsErrorsFalseNoEmitOnError.ts(7,9): warning TS7029: Fallthrough case in switch. +tests/cases/compiler/treatWarningsAsErrorsFalseNoEmitOnError.ts(10,13): warning TS7030: Not all code paths return a value. +tests/cases/compiler/treatWarningsAsErrorsFalseNoEmitOnError.ts(11,13): warning TS7027: Unreachable code detected. + + +==== tests/cases/compiler/treatWarningsAsErrorsFalseNoEmitOnError.ts (6 errors) ==== + function foo(p: any): string { // unused parameter + ~ +!!! warning TS6133: 'p' is declared but its value is never read. + foo: while (false) {} // unused label + ~~~ +!!! warning TS7028: Unused label. + + const x: { kind: "a" } | { kind: "b" } = { kind: "a" }; + const y = "any"; // unused variable + ~ +!!! warning TS6133: 'y' is declared but its value is never read. + switch (x.kind) { + case "a": + ~~~~ +!!! warning TS7029: Fallthrough case in switch. + void x; // implicit fallthrough + case "a": + return; // implicit return + ~~~~~~~ +!!! warning TS7030: Not all code paths return a value. + if (x === x) { // unreachable code + ~~ +!!! warning TS7027: Unreachable code detected. + void x; + } + } + } \ No newline at end of file diff --git a/tests/baselines/reference/treatWarningsAsErrorsFalseNoEmitOnError.js b/tests/baselines/reference/treatWarningsAsErrorsFalseNoEmitOnError.js new file mode 100644 index 0000000000000..5ea484a6aea2f --- /dev/null +++ b/tests/baselines/reference/treatWarningsAsErrorsFalseNoEmitOnError.js @@ -0,0 +1,32 @@ +//// [treatWarningsAsErrorsFalseNoEmitOnError.ts] +function foo(p: any): string { // unused parameter + foo: while (false) {} // unused label + + const x: { kind: "a" } | { kind: "b" } = { kind: "a" }; + const y = "any"; // unused variable + switch (x.kind) { + case "a": + void x; // implicit fallthrough + case "a": + return; // implicit return + if (x === x) { // unreachable code + void x; + } + } +} + +//// [treatWarningsAsErrorsFalseNoEmitOnError.js] +function foo(p) { + foo: while (false) { } // unused label + var x = { kind: "a" }; + var y = "any"; // unused variable + switch (x.kind) { + case "a": + void x; // implicit fallthrough + case "a": + return; // implicit return + if (x === x) { + void x; + } + } +} diff --git a/tests/baselines/reference/treatWarningsAsErrorsFalseNoEmitOnError.symbols b/tests/baselines/reference/treatWarningsAsErrorsFalseNoEmitOnError.symbols new file mode 100644 index 0000000000000..780cc13aa66e1 --- /dev/null +++ b/tests/baselines/reference/treatWarningsAsErrorsFalseNoEmitOnError.symbols @@ -0,0 +1,36 @@ +=== tests/cases/compiler/treatWarningsAsErrorsFalseNoEmitOnError.ts === +function foo(p: any): string { // unused parameter +>foo : Symbol(foo, Decl(treatWarningsAsErrorsFalseNoEmitOnError.ts, 0, 0)) +>p : Symbol(p, Decl(treatWarningsAsErrorsFalseNoEmitOnError.ts, 0, 13)) + + foo: while (false) {} // unused label + + const x: { kind: "a" } | { kind: "b" } = { kind: "a" }; +>x : Symbol(x, Decl(treatWarningsAsErrorsFalseNoEmitOnError.ts, 3, 9)) +>kind : Symbol(kind, Decl(treatWarningsAsErrorsFalseNoEmitOnError.ts, 3, 14)) +>kind : Symbol(kind, Decl(treatWarningsAsErrorsFalseNoEmitOnError.ts, 3, 30)) +>kind : Symbol(kind, Decl(treatWarningsAsErrorsFalseNoEmitOnError.ts, 3, 46)) + + const y = "any"; // unused variable +>y : Symbol(y, Decl(treatWarningsAsErrorsFalseNoEmitOnError.ts, 4, 9)) + + switch (x.kind) { +>x.kind : Symbol(kind, Decl(treatWarningsAsErrorsFalseNoEmitOnError.ts, 3, 14)) +>x : Symbol(x, Decl(treatWarningsAsErrorsFalseNoEmitOnError.ts, 3, 9)) +>kind : Symbol(kind, Decl(treatWarningsAsErrorsFalseNoEmitOnError.ts, 3, 14)) + + case "a": + void x; // implicit fallthrough +>x : Symbol(x, Decl(treatWarningsAsErrorsFalseNoEmitOnError.ts, 3, 9)) + + case "a": + return; // implicit return + if (x === x) { // unreachable code +>x : Symbol(x, Decl(treatWarningsAsErrorsFalseNoEmitOnError.ts, 3, 9)) +>x : Symbol(x, Decl(treatWarningsAsErrorsFalseNoEmitOnError.ts, 3, 9)) + + void x; +>x : Symbol(x, Decl(treatWarningsAsErrorsFalseNoEmitOnError.ts, 3, 9)) + } + } +} diff --git a/tests/baselines/reference/treatWarningsAsErrorsFalseNoEmitOnError.types b/tests/baselines/reference/treatWarningsAsErrorsFalseNoEmitOnError.types new file mode 100644 index 0000000000000..67f4464f0acfa --- /dev/null +++ b/tests/baselines/reference/treatWarningsAsErrorsFalseNoEmitOnError.types @@ -0,0 +1,48 @@ +=== tests/cases/compiler/treatWarningsAsErrorsFalseNoEmitOnError.ts === +function foo(p: any): string { // unused parameter +>foo : (p: any) => string +>p : any + + foo: while (false) {} // unused label +>foo : any +>false : false + + const x: { kind: "a" } | { kind: "b" } = { kind: "a" }; +>x : { kind: "a"; } | { kind: "b"; } +>kind : "a" +>kind : "b" +>{ kind: "a" } : { kind: "a"; } +>kind : string +>"a" : "a" + + const y = "any"; // unused variable +>y : "any" +>"any" : "any" + + switch (x.kind) { +>x.kind : "a" +>x : { kind: "a"; } +>kind : "a" + + case "a": +>"a" : "a" + + void x; // implicit fallthrough +>void x : undefined +>x : { kind: "a"; } + + case "a": +>"a" : "a" + + return; // implicit return + if (x === x) { // unreachable code +>x === x : boolean +>x : { kind: "a"; } | { kind: "b"; } +>x : { kind: "a"; } | { kind: "b"; } + + void x; +>void x : undefined +>x : { kind: "a"; } | { kind: "b"; } + } + } +} diff --git a/tests/baselines/reference/treatWarningsAsErrorsFalsePretty.errors.txt b/tests/baselines/reference/treatWarningsAsErrorsFalsePretty.errors.txt new file mode 100644 index 0000000000000..b9121ef70bc41 --- /dev/null +++ b/tests/baselines/reference/treatWarningsAsErrorsFalsePretty.errors.txt @@ -0,0 +1,67 @@ + +tests/cases/compiler/treatWarningsAsErrorsFalsePretty.ts(1,14): warning TS6133: 'p' is declared but its value is never read. + +1 function foo(p: any): string { // unused parameter +   ~ + +tests/cases/compiler/treatWarningsAsErrorsFalsePretty.ts(2,5): warning TS7028: Unused label. + +2 foo: while (false) {} // unused label +   ~~~ + +tests/cases/compiler/treatWarningsAsErrorsFalsePretty.ts(5,11): warning TS6133: 'y' is declared but its value is never read. + +5 const y = "any"; // unused variable +   ~ + +tests/cases/compiler/treatWarningsAsErrorsFalsePretty.ts(7,9): warning TS7029: Fallthrough case in switch. + +7 case "a": +   ~~~~ + +tests/cases/compiler/treatWarningsAsErrorsFalsePretty.ts(9,14): error TS2678: Type '"b"' is not comparable to type '"a"'. + +9 case "b": +   ~~~ + +tests/cases/compiler/treatWarningsAsErrorsFalsePretty.ts(10,13): warning TS7030: Not all code paths return a value. + +10 return; // implicit return +   ~~~~~~~ + +tests/cases/compiler/treatWarningsAsErrorsFalsePretty.ts(11,13): warning TS7027: Unreachable code detected. + +11 if (x === x) { // unreachable code +   ~~ + + +==== tests/cases/compiler/treatWarningsAsErrorsFalsePretty.ts (7 errors) ==== + function foo(p: any): string { // unused parameter + ~ +!!! warning TS6133: 'p' is declared but its value is never read. + foo: while (false) {} // unused label + ~~~ +!!! warning TS7028: Unused label. + + const x: { kind: "a" } | { kind: "b" } = { kind: "a" }; + const y = "any"; // unused variable + ~ +!!! warning TS6133: 'y' is declared but its value is never read. + switch (x.kind) { + case "a": + ~~~~ +!!! warning TS7029: Fallthrough case in switch. + void x; // implicit fallthrough + case "b": + ~~~ +!!! error TS2678: Type '"b"' is not comparable to type '"a"'. + return; // implicit return + ~~~~~~~ +!!! warning TS7030: Not all code paths return a value. + if (x === x) { // unreachable code + ~~ +!!! warning TS7027: Unreachable code detected. + void x; + } + } + } \ No newline at end of file diff --git a/tests/baselines/reference/treatWarningsAsErrorsFalsePretty.js b/tests/baselines/reference/treatWarningsAsErrorsFalsePretty.js new file mode 100644 index 0000000000000..90bbd4d6007d1 --- /dev/null +++ b/tests/baselines/reference/treatWarningsAsErrorsFalsePretty.js @@ -0,0 +1,32 @@ +//// [treatWarningsAsErrorsFalsePretty.ts] +function foo(p: any): string { // unused parameter + foo: while (false) {} // unused label + + const x: { kind: "a" } | { kind: "b" } = { kind: "a" }; + const y = "any"; // unused variable + switch (x.kind) { + case "a": + void x; // implicit fallthrough + case "b": + return; // implicit return + if (x === x) { // unreachable code + void x; + } + } +} + +//// [treatWarningsAsErrorsFalsePretty.js] +function foo(p) { + foo: while (false) { } // unused label + var x = { kind: "a" }; + var y = "any"; // unused variable + switch (x.kind) { + case "a": + void x; // implicit fallthrough + case "b": + return; // implicit return + if (x === x) { + void x; + } + } +} diff --git a/tests/baselines/reference/treatWarningsAsErrorsFalsePretty.symbols b/tests/baselines/reference/treatWarningsAsErrorsFalsePretty.symbols new file mode 100644 index 0000000000000..d3df68b8c5fd5 --- /dev/null +++ b/tests/baselines/reference/treatWarningsAsErrorsFalsePretty.symbols @@ -0,0 +1,36 @@ +=== tests/cases/compiler/treatWarningsAsErrorsFalsePretty.ts === +function foo(p: any): string { // unused parameter +>foo : Symbol(foo, Decl(treatWarningsAsErrorsFalsePretty.ts, 0, 0)) +>p : Symbol(p, Decl(treatWarningsAsErrorsFalsePretty.ts, 0, 13)) + + foo: while (false) {} // unused label + + const x: { kind: "a" } | { kind: "b" } = { kind: "a" }; +>x : Symbol(x, Decl(treatWarningsAsErrorsFalsePretty.ts, 3, 9)) +>kind : Symbol(kind, Decl(treatWarningsAsErrorsFalsePretty.ts, 3, 14)) +>kind : Symbol(kind, Decl(treatWarningsAsErrorsFalsePretty.ts, 3, 30)) +>kind : Symbol(kind, Decl(treatWarningsAsErrorsFalsePretty.ts, 3, 46)) + + const y = "any"; // unused variable +>y : Symbol(y, Decl(treatWarningsAsErrorsFalsePretty.ts, 4, 9)) + + switch (x.kind) { +>x.kind : Symbol(kind, Decl(treatWarningsAsErrorsFalsePretty.ts, 3, 14)) +>x : Symbol(x, Decl(treatWarningsAsErrorsFalsePretty.ts, 3, 9)) +>kind : Symbol(kind, Decl(treatWarningsAsErrorsFalsePretty.ts, 3, 14)) + + case "a": + void x; // implicit fallthrough +>x : Symbol(x, Decl(treatWarningsAsErrorsFalsePretty.ts, 3, 9)) + + case "b": + return; // implicit return + if (x === x) { // unreachable code +>x : Symbol(x, Decl(treatWarningsAsErrorsFalsePretty.ts, 3, 9)) +>x : Symbol(x, Decl(treatWarningsAsErrorsFalsePretty.ts, 3, 9)) + + void x; +>x : Symbol(x, Decl(treatWarningsAsErrorsFalsePretty.ts, 3, 9)) + } + } +} diff --git a/tests/baselines/reference/treatWarningsAsErrorsFalsePretty.types b/tests/baselines/reference/treatWarningsAsErrorsFalsePretty.types new file mode 100644 index 0000000000000..cf05dc83d6916 --- /dev/null +++ b/tests/baselines/reference/treatWarningsAsErrorsFalsePretty.types @@ -0,0 +1,48 @@ +=== tests/cases/compiler/treatWarningsAsErrorsFalsePretty.ts === +function foo(p: any): string { // unused parameter +>foo : (p: any) => string +>p : any + + foo: while (false) {} // unused label +>foo : any +>false : false + + const x: { kind: "a" } | { kind: "b" } = { kind: "a" }; +>x : { kind: "a"; } | { kind: "b"; } +>kind : "a" +>kind : "b" +>{ kind: "a" } : { kind: "a"; } +>kind : string +>"a" : "a" + + const y = "any"; // unused variable +>y : "any" +>"any" : "any" + + switch (x.kind) { +>x.kind : "a" +>x : { kind: "a"; } +>kind : "a" + + case "a": +>"a" : "a" + + void x; // implicit fallthrough +>void x : undefined +>x : { kind: "a"; } + + case "b": +>"b" : "b" + + return; // implicit return + if (x === x) { // unreachable code +>x === x : boolean +>x : { kind: "a"; } | { kind: "b"; } +>x : { kind: "a"; } | { kind: "b"; } + + void x; +>void x : undefined +>x : { kind: "a"; } | { kind: "b"; } + } + } +} diff --git a/tests/cases/compiler/treatWarningsAsErrorsFalse.ts b/tests/cases/compiler/treatWarningsAsErrorsFalse.ts new file mode 100644 index 0000000000000..930b65540358c --- /dev/null +++ b/tests/cases/compiler/treatWarningsAsErrorsFalse.ts @@ -0,0 +1,22 @@ +// @treatWarningsAsErrors: false +// @noUnusedLocals: true +// @noUnusedParameters: true +// @noImplicitReturns: true +// @noFallthroughCasesInSwitch: true +// @allowUnusedLabels: false +// @allowUnreachableCode: false +function foo(p: any): string { // unused parameter + foo: while (false) {} // unused label + + const x: { kind: "a" } | { kind: "b" } = { kind: "a" }; + const y = "any"; // unused variable + switch (x.kind) { + case "a": + void x; // implicit fallthrough + case "b": + return; // implicit return + if (x === x) { // unreachable code + void x; + } + } +} \ No newline at end of file diff --git a/tests/cases/compiler/treatWarningsAsErrorsFalseNoEmitOnError.ts b/tests/cases/compiler/treatWarningsAsErrorsFalseNoEmitOnError.ts new file mode 100644 index 0000000000000..ad8b7e0e17c77 --- /dev/null +++ b/tests/cases/compiler/treatWarningsAsErrorsFalseNoEmitOnError.ts @@ -0,0 +1,23 @@ +// @treatWarningsAsErrors: false +// @noUnusedLocals: true +// @noUnusedParameters: true +// @noImplicitReturns: true +// @noFallthroughCasesInSwitch: true +// @allowUnusedLabels: false +// @allowUnreachableCode: false +// @noEmitOnError: true +function foo(p: any): string { // unused parameter + foo: while (false) {} // unused label + + const x: { kind: "a" } | { kind: "b" } = { kind: "a" }; + const y = "any"; // unused variable + switch (x.kind) { + case "a": + void x; // implicit fallthrough + case "a": + return; // implicit return + if (x === x) { // unreachable code + void x; + } + } +} \ No newline at end of file diff --git a/tests/cases/compiler/treatWarningsAsErrorsFalsePretty.ts b/tests/cases/compiler/treatWarningsAsErrorsFalsePretty.ts new file mode 100644 index 0000000000000..202bf241fab37 --- /dev/null +++ b/tests/cases/compiler/treatWarningsAsErrorsFalsePretty.ts @@ -0,0 +1,23 @@ +// @treatWarningsAsErrors: false +// @noUnusedLocals: true +// @noUnusedParameters: true +// @noImplicitReturns: true +// @noFallthroughCasesInSwitch: true +// @allowUnusedLabels: false +// @allowUnreachableCode: false +// @pretty: true +function foo(p: any): string { // unused parameter + foo: while (false) {} // unused label + + const x: { kind: "a" } | { kind: "b" } = { kind: "a" }; + const y = "any"; // unused variable + switch (x.kind) { + case "a": + void x; // implicit fallthrough + case "b": + return; // implicit return + if (x === x) { // unreachable code + void x; + } + } +} \ No newline at end of file