Skip to content

Filter out plainJSErrors #930

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 4 commits into from
May 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions internal/ast/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -2695,6 +2695,10 @@ func IsCheckJSEnabledForFile(sourceFile *SourceFile, compilerOptions *core.Compi
return compilerOptions.CheckJs == core.TSTrue
}

func IsPlainJSFile(file *SourceFile, checkJs core.Tristate) bool {
return file != nil && (file.ScriptKind == core.ScriptKindJS || file.ScriptKind == core.ScriptKindJSX) && file.CheckJsDirective == nil && checkJs == core.TSUnknown
}

func GetLeftmostAccessExpression(expr *Node) *Node {
for IsAccessExpression(expr) {
expr = expr.Expression()
Expand Down
6 changes: 1 addition & 5 deletions internal/checker/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -1586,7 +1586,7 @@ func canIncludeBindAndCheckDiagnostics(sourceFile *ast.SourceFile, options *core

isJS := sourceFile.ScriptKind == core.ScriptKindJS || sourceFile.ScriptKind == core.ScriptKindJSX
isCheckJS := isJS && ast.IsCheckJSEnabledForFile(sourceFile, options)
isPlainJS := isPlainJSFile(sourceFile, options.CheckJs)
isPlainJS := ast.IsPlainJSFile(sourceFile, options.CheckJs)

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

func isPlainJSFile(file *ast.SourceFile, checkJs core.Tristate) bool {
return file != nil && (file.ScriptKind == core.ScriptKindJS || file.ScriptKind == core.ScriptKindJSX) && file.CheckJsDirective == nil && checkJs == core.TSUnknown
}

func getEnclosingContainer(node *ast.Node) *ast.Node {
return ast.FindAncestor(node.Parent, func(n *ast.Node) bool {
return binder.GetContainerFlags(n)&binder.ContainerFlagsIsContainer != 0
Expand Down
105 changes: 105 additions & 0 deletions internal/compiler/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,14 @@ func (p *Program) getSemanticDiagnosticsForFile(ctx context.Context, sourceFile
if ctx.Err() != nil {
return nil
}

isPlainJS := ast.IsPlainJSFile(sourceFile, p.compilerOptions.CheckJs)
if isPlainJS {
diags = core.Filter(diags, func(d *ast.Diagnostic) bool {
return plainJSErrors.Has(d.Code())
})
}

if len(sourceFile.CommentDirectives) == 0 {
return diags
}
Expand Down Expand Up @@ -797,3 +805,100 @@ func (p *Program) GetJSXRuntimeImportSpecifier(path tspath.Path) (moduleReferenc
func (p *Program) GetImportHelpersImportSpecifier(path tspath.Path) *ast.Node {
return p.importHelpersImportSpecifiers[path]
}

var plainJSErrors = core.NewSetFromItems(
// binder errors
diagnostics.Cannot_redeclare_block_scoped_variable_0.Code(),
diagnostics.A_module_cannot_have_multiple_default_exports.Code(),
diagnostics.Another_export_default_is_here.Code(),
diagnostics.The_first_export_default_is_here.Code(),
diagnostics.Identifier_expected_0_is_a_reserved_word_at_the_top_level_of_a_module.Code(),
diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode_Modules_are_automatically_in_strict_mode.Code(),
diagnostics.Identifier_expected_0_is_a_reserved_word_that_cannot_be_used_here.Code(),
diagnostics.X_constructor_is_a_reserved_word.Code(),
diagnostics.X_delete_cannot_be_called_on_an_identifier_in_strict_mode.Code(),
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(),
diagnostics.Invalid_use_of_0_Modules_are_automatically_in_strict_mode.Code(),
diagnostics.Invalid_use_of_0_in_strict_mode.Code(),
diagnostics.A_label_is_not_allowed_here.Code(),
diagnostics.X_with_statements_are_not_allowed_in_strict_mode.Code(),
// grammar errors
diagnostics.A_break_statement_can_only_be_used_within_an_enclosing_iteration_or_switch_statement.Code(),
diagnostics.A_break_statement_can_only_jump_to_a_label_of_an_enclosing_statement.Code(),
diagnostics.A_class_declaration_without_the_default_modifier_must_have_a_name.Code(),
diagnostics.A_class_member_cannot_have_the_0_keyword.Code(),
diagnostics.A_comma_expression_is_not_allowed_in_a_computed_property_name.Code(),
diagnostics.A_continue_statement_can_only_be_used_within_an_enclosing_iteration_statement.Code(),
diagnostics.A_continue_statement_can_only_jump_to_a_label_of_an_enclosing_iteration_statement.Code(),
diagnostics.A_default_clause_cannot_appear_more_than_once_in_a_switch_statement.Code(),
diagnostics.A_default_export_must_be_at_the_top_level_of_a_file_or_module_declaration.Code(),
diagnostics.A_definite_assignment_assertion_is_not_permitted_in_this_context.Code(),
diagnostics.A_destructuring_declaration_must_have_an_initializer.Code(),
diagnostics.A_get_accessor_cannot_have_parameters.Code(),
diagnostics.A_rest_element_cannot_contain_a_binding_pattern.Code(),
diagnostics.A_rest_element_cannot_have_a_property_name.Code(),
diagnostics.A_rest_element_cannot_have_an_initializer.Code(),
diagnostics.A_rest_element_must_be_last_in_a_destructuring_pattern.Code(),
diagnostics.A_rest_parameter_cannot_have_an_initializer.Code(),
diagnostics.A_rest_parameter_must_be_last_in_a_parameter_list.Code(),
diagnostics.A_rest_parameter_or_binding_pattern_may_not_have_a_trailing_comma.Code(),
diagnostics.A_return_statement_cannot_be_used_inside_a_class_static_block.Code(),
diagnostics.A_set_accessor_cannot_have_rest_parameter.Code(),
diagnostics.A_set_accessor_must_have_exactly_one_parameter.Code(),
diagnostics.An_export_declaration_can_only_be_used_at_the_top_level_of_a_module.Code(),
diagnostics.An_export_declaration_cannot_have_modifiers.Code(),
diagnostics.An_import_declaration_can_only_be_used_at_the_top_level_of_a_module.Code(),
diagnostics.An_import_declaration_cannot_have_modifiers.Code(),
diagnostics.An_object_member_cannot_be_declared_optional.Code(),
diagnostics.Argument_of_dynamic_import_cannot_be_spread_element.Code(),
diagnostics.Cannot_assign_to_private_method_0_Private_methods_are_not_writable.Code(),
diagnostics.Cannot_redeclare_identifier_0_in_catch_clause.Code(),
diagnostics.Catch_clause_variable_cannot_have_an_initializer.Code(),
diagnostics.Class_decorators_can_t_be_used_with_static_private_identifier_Consider_removing_the_experimental_decorator.Code(),
diagnostics.Classes_can_only_extend_a_single_class.Code(),
diagnostics.Classes_may_not_have_a_field_named_constructor.Code(),
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(),
diagnostics.Duplicate_label_0.Code(),
diagnostics.Dynamic_imports_can_only_accept_a_module_specifier_and_an_optional_set_of_attributes_as_arguments.Code(),
diagnostics.X_for_await_loops_cannot_be_used_inside_a_class_static_block.Code(),
diagnostics.JSX_attributes_must_only_be_assigned_a_non_empty_expression.Code(),
diagnostics.JSX_elements_cannot_have_multiple_attributes_with_the_same_name.Code(),
diagnostics.JSX_expressions_may_not_use_the_comma_operator_Did_you_mean_to_write_an_array.Code(),
diagnostics.JSX_property_access_expressions_cannot_include_JSX_namespace_names.Code(),
diagnostics.Jump_target_cannot_cross_function_boundary.Code(),
diagnostics.Line_terminator_not_permitted_before_arrow.Code(),
diagnostics.Modifiers_cannot_appear_here.Code(),
diagnostics.Only_a_single_variable_declaration_is_allowed_in_a_for_in_statement.Code(),
diagnostics.Only_a_single_variable_declaration_is_allowed_in_a_for_of_statement.Code(),
diagnostics.Private_identifiers_are_not_allowed_outside_class_bodies.Code(),
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(),
diagnostics.Property_0_is_not_accessible_outside_class_1_because_it_has_a_private_identifier.Code(),
diagnostics.Tagged_template_expressions_are_not_permitted_in_an_optional_chain.Code(),
diagnostics.The_left_hand_side_of_a_for_of_statement_may_not_be_async.Code(),
diagnostics.The_variable_declaration_of_a_for_in_statement_cannot_have_an_initializer.Code(),
diagnostics.The_variable_declaration_of_a_for_of_statement_cannot_have_an_initializer.Code(),
diagnostics.Trailing_comma_not_allowed.Code(),
diagnostics.Variable_declaration_list_cannot_be_empty.Code(),
diagnostics.X_0_and_1_operations_cannot_be_mixed_without_parentheses.Code(),
diagnostics.X_0_expected.Code(),
diagnostics.X_0_is_not_a_valid_meta_property_for_keyword_1_Did_you_mean_2.Code(),
diagnostics.X_0_list_cannot_be_empty.Code(),
diagnostics.X_0_modifier_already_seen.Code(),
diagnostics.X_0_modifier_cannot_appear_on_a_constructor_declaration.Code(),
diagnostics.X_0_modifier_cannot_appear_on_a_module_or_namespace_element.Code(),
diagnostics.X_0_modifier_cannot_appear_on_a_parameter.Code(),
diagnostics.X_0_modifier_cannot_appear_on_class_elements_of_this_kind.Code(),
diagnostics.X_0_modifier_cannot_be_used_here.Code(),
diagnostics.X_0_modifier_must_precede_1_modifier.Code(),
diagnostics.X_0_declarations_can_only_be_declared_inside_a_block.Code(),
diagnostics.X_0_declarations_must_be_initialized.Code(),
diagnostics.X_extends_clause_already_seen.Code(),
diagnostics.X_let_is_not_allowed_to_be_used_as_a_name_in_let_or_const_declarations.Code(),
diagnostics.Class_constructor_may_not_be_a_generator.Code(),
diagnostics.Class_constructor_may_not_be_an_accessor.Code(),
diagnostics.X_await_expressions_are_only_allowed_within_async_functions_and_at_the_top_levels_of_modules.Code(),
diagnostics.X_await_using_statements_are_only_allowed_within_async_functions_and_at_the_top_levels_of_modules.Code(),
diagnostics.Private_field_0_must_be_declared_in_an_enclosing_class.Code(),
// Type errors
diagnostics.This_condition_will_always_return_0_since_JavaScript_compares_objects_by_reference_not_value.Code(),
)

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/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.
/a.js(23,24): error TS2339: Property 'bar' does not exist on type 'object'.


==== /a.js (2 errors) ====
==== /a.js (1 errors) ====
class A {
/**
* Constructor
Expand All @@ -28,8 +27,6 @@
* @type object
*/
this.bar = arguments.bar;
~~~
!!! error TS2339: Property 'bar' does not exist on type 'object'.

/**
* @type object
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/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.
/a.js(21,24): error TS2339: Property 'bar' does not exist on type 'object'.


==== /a.js (2 errors) ====
==== /a.js (1 errors) ====
class A {
/**
* @param {object} [foo={}]
Expand All @@ -26,8 +25,6 @@
* @type object
*/
this.bar = arguments.bar;
~~~
!!! error TS2339: Property 'bar' does not exist on type 'object'.

/**
* @type object
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
bar.ts(1,1): error TS2322: Type 'string' is not assignable to type 'number'.
foo.js(3,13): error TS2304: Cannot find name 'cond'.


==== foo.js (1 errors) ====
==== foo.js (0 errors) ====
class C {
constructor() {
if (cond) {
~~~~
!!! error TS2304: Cannot find name 'cond'.
this.p = null;
}
else {
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
a.ts(1,10): error TS2393: Duplicate function implementation.
b.js(1,10): error TS2393: Duplicate function implementation.


==== b.js (1 errors) ====
==== b.js (0 errors) ====
function foo() {
~~~
!!! error TS2393: Duplicate function implementation.
return 10;
}
==== a.ts (1 errors) ====
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
a.ts(1,10): error TS2393: Duplicate function implementation.
b.js(1,10): error TS2393: Duplicate function implementation.


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

==== b.js (1 errors) ====
==== b.js (0 errors) ====
function foo() {
~~~
!!! error TS2393: Duplicate function implementation.
return 10;
}

Expand Down
Loading