Skip to content

Commit f4022d3

Browse files
jakebaileyCopilot
andauthored
Filter out plainJSErrors (#930)
Co-authored-by: Copilot <[email protected]>
1 parent 7137727 commit f4022d3

File tree

117 files changed

+289
-3265
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

117 files changed

+289
-3265
lines changed

internal/ast/utilities.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2691,6 +2691,10 @@ func IsCheckJSEnabledForFile(sourceFile *SourceFile, compilerOptions *core.Compi
26912691
return compilerOptions.CheckJs == core.TSTrue
26922692
}
26932693

2694+
func IsPlainJSFile(file *SourceFile, checkJs core.Tristate) bool {
2695+
return file != nil && (file.ScriptKind == core.ScriptKindJS || file.ScriptKind == core.ScriptKindJSX) && file.CheckJsDirective == nil && checkJs == core.TSUnknown
2696+
}
2697+
26942698
func GetLeftmostAccessExpression(expr *Node) *Node {
26952699
for IsAccessExpression(expr) {
26962700
expr = expr.Expression()

internal/checker/utilities.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,7 +1591,7 @@ func canIncludeBindAndCheckDiagnostics(sourceFile *ast.SourceFile, options *core
15911591

15921592
isJS := sourceFile.ScriptKind == core.ScriptKindJS || sourceFile.ScriptKind == core.ScriptKindJSX
15931593
isCheckJS := isJS && ast.IsCheckJSEnabledForFile(sourceFile, options)
1594-
isPlainJS := isPlainJSFile(sourceFile, options.CheckJs)
1594+
isPlainJS := ast.IsPlainJSFile(sourceFile, options.CheckJs)
15951595

15961596
// By default, only type-check .ts, .tsx, Deferred, plain JS, checked JS and External
15971597
// - plain JS: .js files with no // ts-check and checkJs: undefined
@@ -1600,10 +1600,6 @@ func canIncludeBindAndCheckDiagnostics(sourceFile *ast.SourceFile, options *core
16001600
return isPlainJS || isCheckJS || sourceFile.ScriptKind == core.ScriptKindDeferred
16011601
}
16021602

1603-
func isPlainJSFile(file *ast.SourceFile, checkJs core.Tristate) bool {
1604-
return file != nil && (file.ScriptKind == core.ScriptKindJS || file.ScriptKind == core.ScriptKindJSX) && file.CheckJsDirective == nil && checkJs == core.TSUnknown
1605-
}
1606-
16071603
func getEnclosingContainer(node *ast.Node) *ast.Node {
16081604
return ast.FindAncestor(node.Parent, func(n *ast.Node) bool {
16091605
return binder.GetContainerFlags(n)&binder.ContainerFlagsIsContainer != 0

internal/compiler/program.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,14 @@ func (p *Program) getSemanticDiagnosticsForFile(ctx context.Context, sourceFile
415415
if ctx.Err() != nil {
416416
return nil
417417
}
418+
419+
isPlainJS := ast.IsPlainJSFile(sourceFile, p.compilerOptions.CheckJs)
420+
if isPlainJS {
421+
diags = core.Filter(diags, func(d *ast.Diagnostic) bool {
422+
return plainJSErrors.Has(d.Code())
423+
})
424+
}
425+
418426
if len(sourceFile.CommentDirectives) == 0 {
419427
return diags
420428
}
@@ -834,3 +842,100 @@ func (p *Program) GetJSXRuntimeImportSpecifier(path tspath.Path) (moduleReferenc
834842
func (p *Program) GetImportHelpersImportSpecifier(path tspath.Path) *ast.Node {
835843
return p.importHelpersImportSpecifiers[path]
836844
}
845+
846+
var plainJSErrors = core.NewSetFromItems(
847+
// binder errors
848+
diagnostics.Cannot_redeclare_block_scoped_variable_0.Code(),
849+
diagnostics.A_module_cannot_have_multiple_default_exports.Code(),
850+
diagnostics.Another_export_default_is_here.Code(),
851+
diagnostics.The_first_export_default_is_here.Code(),
852+
diagnostics.Identifier_expected_0_is_a_reserved_word_at_the_top_level_of_a_module.Code(),
853+
diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode_Modules_are_automatically_in_strict_mode.Code(),
854+
diagnostics.Identifier_expected_0_is_a_reserved_word_that_cannot_be_used_here.Code(),
855+
diagnostics.X_constructor_is_a_reserved_word.Code(),
856+
diagnostics.X_delete_cannot_be_called_on_an_identifier_in_strict_mode.Code(),
857+
diagnostics.Code_contained_in_a_class_is_evaluated_in_JavaScript_s_strict_mode_which_does_not_allow_this_use_of_0_For_more_information_see_https_Colon_Slash_Slashdeveloper_mozilla_org_Slashen_US_Slashdocs_SlashWeb_SlashJavaScript_SlashReference_SlashStrict_mode.Code(),
858+
diagnostics.Invalid_use_of_0_Modules_are_automatically_in_strict_mode.Code(),
859+
diagnostics.Invalid_use_of_0_in_strict_mode.Code(),
860+
diagnostics.A_label_is_not_allowed_here.Code(),
861+
diagnostics.X_with_statements_are_not_allowed_in_strict_mode.Code(),
862+
// grammar errors
863+
diagnostics.A_break_statement_can_only_be_used_within_an_enclosing_iteration_or_switch_statement.Code(),
864+
diagnostics.A_break_statement_can_only_jump_to_a_label_of_an_enclosing_statement.Code(),
865+
diagnostics.A_class_declaration_without_the_default_modifier_must_have_a_name.Code(),
866+
diagnostics.A_class_member_cannot_have_the_0_keyword.Code(),
867+
diagnostics.A_comma_expression_is_not_allowed_in_a_computed_property_name.Code(),
868+
diagnostics.A_continue_statement_can_only_be_used_within_an_enclosing_iteration_statement.Code(),
869+
diagnostics.A_continue_statement_can_only_jump_to_a_label_of_an_enclosing_iteration_statement.Code(),
870+
diagnostics.A_default_clause_cannot_appear_more_than_once_in_a_switch_statement.Code(),
871+
diagnostics.A_default_export_must_be_at_the_top_level_of_a_file_or_module_declaration.Code(),
872+
diagnostics.A_definite_assignment_assertion_is_not_permitted_in_this_context.Code(),
873+
diagnostics.A_destructuring_declaration_must_have_an_initializer.Code(),
874+
diagnostics.A_get_accessor_cannot_have_parameters.Code(),
875+
diagnostics.A_rest_element_cannot_contain_a_binding_pattern.Code(),
876+
diagnostics.A_rest_element_cannot_have_a_property_name.Code(),
877+
diagnostics.A_rest_element_cannot_have_an_initializer.Code(),
878+
diagnostics.A_rest_element_must_be_last_in_a_destructuring_pattern.Code(),
879+
diagnostics.A_rest_parameter_cannot_have_an_initializer.Code(),
880+
diagnostics.A_rest_parameter_must_be_last_in_a_parameter_list.Code(),
881+
diagnostics.A_rest_parameter_or_binding_pattern_may_not_have_a_trailing_comma.Code(),
882+
diagnostics.A_return_statement_cannot_be_used_inside_a_class_static_block.Code(),
883+
diagnostics.A_set_accessor_cannot_have_rest_parameter.Code(),
884+
diagnostics.A_set_accessor_must_have_exactly_one_parameter.Code(),
885+
diagnostics.An_export_declaration_can_only_be_used_at_the_top_level_of_a_module.Code(),
886+
diagnostics.An_export_declaration_cannot_have_modifiers.Code(),
887+
diagnostics.An_import_declaration_can_only_be_used_at_the_top_level_of_a_module.Code(),
888+
diagnostics.An_import_declaration_cannot_have_modifiers.Code(),
889+
diagnostics.An_object_member_cannot_be_declared_optional.Code(),
890+
diagnostics.Argument_of_dynamic_import_cannot_be_spread_element.Code(),
891+
diagnostics.Cannot_assign_to_private_method_0_Private_methods_are_not_writable.Code(),
892+
diagnostics.Cannot_redeclare_identifier_0_in_catch_clause.Code(),
893+
diagnostics.Catch_clause_variable_cannot_have_an_initializer.Code(),
894+
diagnostics.Class_decorators_can_t_be_used_with_static_private_identifier_Consider_removing_the_experimental_decorator.Code(),
895+
diagnostics.Classes_can_only_extend_a_single_class.Code(),
896+
diagnostics.Classes_may_not_have_a_field_named_constructor.Code(),
897+
diagnostics.Did_you_mean_to_use_a_Colon_An_can_only_follow_a_property_name_when_the_containing_object_literal_is_part_of_a_destructuring_pattern.Code(),
898+
diagnostics.Duplicate_label_0.Code(),
899+
diagnostics.Dynamic_imports_can_only_accept_a_module_specifier_and_an_optional_set_of_attributes_as_arguments.Code(),
900+
diagnostics.X_for_await_loops_cannot_be_used_inside_a_class_static_block.Code(),
901+
diagnostics.JSX_attributes_must_only_be_assigned_a_non_empty_expression.Code(),
902+
diagnostics.JSX_elements_cannot_have_multiple_attributes_with_the_same_name.Code(),
903+
diagnostics.JSX_expressions_may_not_use_the_comma_operator_Did_you_mean_to_write_an_array.Code(),
904+
diagnostics.JSX_property_access_expressions_cannot_include_JSX_namespace_names.Code(),
905+
diagnostics.Jump_target_cannot_cross_function_boundary.Code(),
906+
diagnostics.Line_terminator_not_permitted_before_arrow.Code(),
907+
diagnostics.Modifiers_cannot_appear_here.Code(),
908+
diagnostics.Only_a_single_variable_declaration_is_allowed_in_a_for_in_statement.Code(),
909+
diagnostics.Only_a_single_variable_declaration_is_allowed_in_a_for_of_statement.Code(),
910+
diagnostics.Private_identifiers_are_not_allowed_outside_class_bodies.Code(),
911+
diagnostics.Private_identifiers_are_only_allowed_in_class_bodies_and_may_only_be_used_as_part_of_a_class_member_declaration_property_access_or_on_the_left_hand_side_of_an_in_expression.Code(),
912+
diagnostics.Property_0_is_not_accessible_outside_class_1_because_it_has_a_private_identifier.Code(),
913+
diagnostics.Tagged_template_expressions_are_not_permitted_in_an_optional_chain.Code(),
914+
diagnostics.The_left_hand_side_of_a_for_of_statement_may_not_be_async.Code(),
915+
diagnostics.The_variable_declaration_of_a_for_in_statement_cannot_have_an_initializer.Code(),
916+
diagnostics.The_variable_declaration_of_a_for_of_statement_cannot_have_an_initializer.Code(),
917+
diagnostics.Trailing_comma_not_allowed.Code(),
918+
diagnostics.Variable_declaration_list_cannot_be_empty.Code(),
919+
diagnostics.X_0_and_1_operations_cannot_be_mixed_without_parentheses.Code(),
920+
diagnostics.X_0_expected.Code(),
921+
diagnostics.X_0_is_not_a_valid_meta_property_for_keyword_1_Did_you_mean_2.Code(),
922+
diagnostics.X_0_list_cannot_be_empty.Code(),
923+
diagnostics.X_0_modifier_already_seen.Code(),
924+
diagnostics.X_0_modifier_cannot_appear_on_a_constructor_declaration.Code(),
925+
diagnostics.X_0_modifier_cannot_appear_on_a_module_or_namespace_element.Code(),
926+
diagnostics.X_0_modifier_cannot_appear_on_a_parameter.Code(),
927+
diagnostics.X_0_modifier_cannot_appear_on_class_elements_of_this_kind.Code(),
928+
diagnostics.X_0_modifier_cannot_be_used_here.Code(),
929+
diagnostics.X_0_modifier_must_precede_1_modifier.Code(),
930+
diagnostics.X_0_declarations_can_only_be_declared_inside_a_block.Code(),
931+
diagnostics.X_0_declarations_must_be_initialized.Code(),
932+
diagnostics.X_extends_clause_already_seen.Code(),
933+
diagnostics.X_let_is_not_allowed_to_be_used_as_a_name_in_let_or_const_declarations.Code(),
934+
diagnostics.Class_constructor_may_not_be_a_generator.Code(),
935+
diagnostics.Class_constructor_may_not_be_an_accessor.Code(),
936+
diagnostics.X_await_expressions_are_only_allowed_within_async_functions_and_at_the_top_levels_of_modules.Code(),
937+
diagnostics.X_await_using_statements_are_only_allowed_within_async_functions_and_at_the_top_levels_of_modules.Code(),
938+
diagnostics.Private_field_0_must_be_declared_in_an_enclosing_class.Code(),
939+
// Type errors
940+
diagnostics.This_condition_will_always_return_0_since_JavaScript_compares_objects_by_reference_not_value.Code(),
941+
)

testdata/baselines/reference/submodule/compiler/argumentsReferenceInConstructor3_Js.errors.txt

Lines changed: 0 additions & 36 deletions
This file was deleted.

testdata/baselines/reference/submodule/compiler/argumentsReferenceInConstructor4_Js.errors.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
/a.js(18,9): error TS1210: Code contained in a class is evaluated in JavaScript's strict mode which does not allow this use of 'arguments'. For more information, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode.
2-
/a.js(23,24): error TS2339: Property 'bar' does not exist on type 'object'.
32

43

5-
==== /a.js (2 errors) ====
4+
==== /a.js (1 errors) ====
65
class A {
76
/**
87
* Constructor
@@ -28,8 +27,6 @@
2827
* @type object
2928
*/
3029
this.bar = arguments.bar;
31-
~~~
32-
!!! error TS2339: Property 'bar' does not exist on type 'object'.
3330

3431
/**
3532
* @type object

testdata/baselines/reference/submodule/compiler/argumentsReferenceInMethod3_Js.errors.txt

Lines changed: 0 additions & 29 deletions
This file was deleted.

testdata/baselines/reference/submodule/compiler/argumentsReferenceInMethod4_Js.errors.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
/a.js(16,9): error TS1210: Code contained in a class is evaluated in JavaScript's strict mode which does not allow this use of 'arguments'. For more information, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode.
2-
/a.js(21,24): error TS2339: Property 'bar' does not exist on type 'object'.
32

43

5-
==== /a.js (2 errors) ====
4+
==== /a.js (1 errors) ====
65
class A {
76
/**
87
* @param {object} [foo={}]
@@ -26,8 +25,6 @@
2625
* @type object
2726
*/
2827
this.bar = arguments.bar;
29-
~~~
30-
!!! error TS2339: Property 'bar' does not exist on type 'object'.
3128

3229
/**
3330
* @type object

testdata/baselines/reference/submodule/compiler/decoratorInJsFile.errors.txt

Lines changed: 0 additions & 12 deletions
This file was deleted.

testdata/baselines/reference/submodule/compiler/decoratorInJsFile1.errors.txt

Lines changed: 0 additions & 12 deletions
This file was deleted.

testdata/baselines/reference/submodule/compiler/dynamicRequire.errors.txt

Lines changed: 0 additions & 10 deletions
This file was deleted.

testdata/baselines/reference/submodule/compiler/exportDefaultMarksIdentifierAsUsed.errors.txt

Lines changed: 0 additions & 12 deletions
This file was deleted.

testdata/baselines/reference/submodule/compiler/jsFileClassPropertyInitalizationInObjectLiteral.errors.txt

Lines changed: 0 additions & 13 deletions
This file was deleted.

testdata/baselines/reference/submodule/compiler/jsFileClassPropertyType3.errors.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
bar.ts(1,1): error TS2322: Type 'string' is not assignable to type 'number'.
2-
foo.js(3,13): error TS2304: Cannot find name 'cond'.
32

43

5-
==== foo.js (1 errors) ====
4+
==== foo.js (0 errors) ====
65
class C {
76
constructor() {
87
if (cond) {
9-
~~~~
10-
!!! error TS2304: Cannot find name 'cond'.
118
this.p = null;
129
}
1310
else {

testdata/baselines/reference/submodule/compiler/jsFileCompilationConstructorOverloadSyntax.errors.txt

Lines changed: 0 additions & 10 deletions
This file was deleted.

testdata/baselines/reference/submodule/compiler/jsFileCompilationDecoratorSyntax.errors.txt

Lines changed: 0 additions & 8 deletions
This file was deleted.

testdata/baselines/reference/submodule/compiler/jsFileCompilationDuplicateFunctionImplementation.errors.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
a.ts(1,10): error TS2393: Duplicate function implementation.
2-
b.js(1,10): error TS2393: Duplicate function implementation.
32

43

5-
==== b.js (1 errors) ====
4+
==== b.js (0 errors) ====
65
function foo() {
7-
~~~
8-
!!! error TS2393: Duplicate function implementation.
96
return 10;
107
}
118
==== a.ts (1 errors) ====

testdata/baselines/reference/submodule/compiler/jsFileCompilationDuplicateFunctionImplementationFileOrderReversed.errors.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
a.ts(1,10): error TS2393: Duplicate function implementation.
2-
b.js(1,10): error TS2393: Duplicate function implementation.
32

43

54
==== a.ts (1 errors) ====
@@ -9,10 +8,8 @@ b.js(1,10): error TS2393: Duplicate function implementation.
98
return 30;
109
}
1110

12-
==== b.js (1 errors) ====
11+
==== b.js (0 errors) ====
1312
function foo() {
14-
~~~
15-
!!! error TS2393: Duplicate function implementation.
1613
return 10;
1714
}
1815

0 commit comments

Comments
 (0)