Skip to content
Closed
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
3 changes: 1 addition & 2 deletions crates/oxc_ast/src/ast/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -928,8 +928,7 @@ pub struct TSInterfaceDeclaration<'a> {
pub id: BindingIdentifier<'a>,
/// Other interfaces/types this interface extends.
#[scope(enter_before)]
#[estree(via = TSInterfaceDeclarationExtends)]
pub extends: Option<Vec<'a, TSInterfaceHeritage<'a>>>,
pub extends: Vec<'a, TSInterfaceHeritage<'a>>,
/// Type parameters that get bound to the interface.
pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
pub body: Box<'a, TSInterfaceBody<'a>>,
Expand Down
16 changes: 8 additions & 8 deletions crates/oxc_ast/src/generated/ast_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4137,7 +4137,7 @@ impl<'a> AstBuilder<'a> {
self,
span: Span,
id: BindingIdentifier<'a>,
extends: Option<Vec<'a, TSInterfaceHeritage<'a>>>,
extends: Vec<'a, TSInterfaceHeritage<'a>>,
type_parameters: T1,
body: T2,
declare: bool,
Expand Down Expand Up @@ -4173,7 +4173,7 @@ impl<'a> AstBuilder<'a> {
self,
span: Span,
id: BindingIdentifier<'a>,
extends: Option<Vec<'a, TSInterfaceHeritage<'a>>>,
extends: Vec<'a, TSInterfaceHeritage<'a>>,
type_parameters: T1,
body: T2,
declare: bool,
Expand Down Expand Up @@ -8141,7 +8141,7 @@ impl<'a> AstBuilder<'a> {
self,
span: Span,
id: BindingIdentifier<'a>,
extends: Option<Vec<'a, TSInterfaceHeritage<'a>>>,
extends: Vec<'a, TSInterfaceHeritage<'a>>,
type_parameters: T1,
body: T2,
declare: bool,
Expand Down Expand Up @@ -8177,7 +8177,7 @@ impl<'a> AstBuilder<'a> {
self,
span: Span,
id: BindingIdentifier<'a>,
extends: Option<Vec<'a, TSInterfaceHeritage<'a>>>,
extends: Vec<'a, TSInterfaceHeritage<'a>>,
type_parameters: T1,
body: T2,
declare: bool,
Expand Down Expand Up @@ -12079,7 +12079,7 @@ impl<'a> AstBuilder<'a> {
self,
span: Span,
id: BindingIdentifier<'a>,
extends: Option<Vec<'a, TSInterfaceHeritage<'a>>>,
extends: Vec<'a, TSInterfaceHeritage<'a>>,
type_parameters: T1,
body: T2,
declare: bool,
Expand Down Expand Up @@ -12116,7 +12116,7 @@ impl<'a> AstBuilder<'a> {
self,
span: Span,
id: BindingIdentifier<'a>,
extends: Option<Vec<'a, TSInterfaceHeritage<'a>>>,
extends: Vec<'a, TSInterfaceHeritage<'a>>,
type_parameters: T1,
body: T2,
declare: bool,
Expand Down Expand Up @@ -12149,7 +12149,7 @@ impl<'a> AstBuilder<'a> {
self,
span: Span,
id: BindingIdentifier<'a>,
extends: Option<Vec<'a, TSInterfaceHeritage<'a>>>,
extends: Vec<'a, TSInterfaceHeritage<'a>>,
type_parameters: T1,
body: T2,
declare: bool,
Expand Down Expand Up @@ -12188,7 +12188,7 @@ impl<'a> AstBuilder<'a> {
self,
span: Span,
id: BindingIdentifier<'a>,
extends: Option<Vec<'a, TSInterfaceHeritage<'a>>>,
extends: Vec<'a, TSInterfaceHeritage<'a>>,
type_parameters: T1,
body: T2,
declare: bool,
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_ast/src/generated/derive_estree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2860,7 +2860,7 @@ impl ESTree for TSInterfaceDeclaration<'_> {
state.serialize_field("start", &self.span.start);
state.serialize_field("end", &self.span.end);
state.serialize_field("id", &self.id);
state.serialize_field("extends", &crate::serialize::TSInterfaceDeclarationExtends(self));
state.serialize_field("extends", &self.extends);
state.serialize_field("typeParameters", &self.type_parameters);
state.serialize_field("body", &self.body);
state.serialize_field("declare", &self.declare);
Expand Down
23 changes: 0 additions & 23 deletions crates/oxc_ast/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -719,29 +719,6 @@ impl ESTree for MethodDefinitionKey<'_, '_> {
}
}

/// Serializer for `extends` field of `TSInterfaceDeclaration`.
///
/// Serialize `extends` as an empty array if it's `None`.
#[ast_meta]
#[estree(
ts_type = "Array<TSInterfaceHeritage>",
raw_deser = "
const extendsArr = DESER[Option<Vec<TSInterfaceHeritage>>](POS_OFFSET.extends);
extendsArr === null ? [] : extendsArr
"
)]
pub struct TSInterfaceDeclarationExtends<'a, 'b>(pub &'b TSInterfaceDeclaration<'a>);

impl ESTree for TSInterfaceDeclarationExtends<'_, '_> {
fn serialize<S: Serializer>(&self, serializer: S) {
if let Some(extends) = &self.0.extends {
extends.serialize(serializer);
} else {
[(); 0].serialize(serializer);
}
}
}

/// Serializer for `specifiers` field of `ImportDeclaration`.
///
/// Serialize `specifiers` as an empty array if it's `None`.
Expand Down
4 changes: 1 addition & 3 deletions crates/oxc_ast_visit/src/generated/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3622,9 +3622,7 @@ pub mod walk {
visitor.visit_span(&it.span);
visitor.visit_binding_identifier(&it.id);
visitor.enter_scope(ScopeFlags::empty(), &it.scope_id);
if let Some(extends) = &it.extends {
visitor.visit_ts_interface_heritages(extends);
}
visitor.visit_ts_interface_heritages(&it.extends);
if let Some(type_parameters) = &it.type_parameters {
visitor.visit_ts_type_parameter_declaration(type_parameters);
}
Expand Down
4 changes: 1 addition & 3 deletions crates/oxc_ast_visit/src/generated/visit_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3819,9 +3819,7 @@ pub mod walk_mut {
visitor.visit_span(&mut it.span);
visitor.visit_binding_identifier(&mut it.id);
visitor.enter_scope(ScopeFlags::empty(), &it.scope_id);
if let Some(extends) = &mut it.extends {
visitor.visit_ts_interface_heritages(extends);
}
visitor.visit_ts_interface_heritages(&mut it.extends);
if let Some(type_parameters) = &mut it.type_parameters {
visitor.visit_ts_type_parameter_declaration(type_parameters);
}
Expand Down
8 changes: 3 additions & 5 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3700,11 +3700,9 @@ impl Gen for TSInterfaceDeclaration<'_> {
if let Some(type_parameters) = &self.type_parameters {
type_parameters.print(p, ctx);
}
if let Some(extends) = &self.extends {
if !extends.is_empty() {
p.print_str(" extends ");
p.print_list(extends, ctx);
}
if !self.extends.is_empty() {
p.print_str(" extends ");
p.print_list(&self.extends, ctx);
}
p.print_soft_space();
p.print_curly_braces(self.body.span, self.body.body.is_empty(), |p| {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::Write;

use oxc_ast::{
AstKind,
ast::{ExportDefaultDeclarationKind, TSType},
Expand Down Expand Up @@ -172,27 +174,10 @@ impl Rule for ConsistentTypeDefinitions {
let body_span = &decl.body.span;
let body = &ctx.source_text()[body_span.start as usize..body_span.end as usize];

let extends_vec = {
let mut concatenated_names: Vec<&str> = Vec::new();

if let Some(extends) = decl.extends.as_deref() {
for exp in extends {
concatenated_names.push(
&ctx.source_text()
[exp.span.start as usize..exp.span.end as usize],
);
}
}

concatenated_names
};

let extends = if extends_vec.is_empty() {
String::new()
} else {
let joined_extends = extends_vec.join(" & ");
format!(" & {joined_extends}")
};
let mut extends = String::new();
for exp in &decl.extends {
write!(extends, " & {}", exp.span.source_text(ctx.source_text())).unwrap();
}

ctx.diagnostic_with_fix(
consistent_type_definitions_diagnostic(
Expand Down Expand Up @@ -235,25 +220,10 @@ impl Rule for ConsistentTypeDefinitions {
let body_span = &decl.body.span;
let body = &ctx.source_text()[body_span.start as usize..body_span.end as usize];

let extends_vec = {
let mut concatenated_names: Vec<&str> = Vec::new();

if let Some(extends) = decl.extends.as_deref() {
for exp in extends {
concatenated_names.push(
&ctx.source_text()[exp.span.start as usize..exp.span.end as usize],
);
}
}

concatenated_names
};
let extends = if extends_vec.is_empty() {
String::new()
} else {
let joined_extends = extends_vec.join(" & ");
format!(" & {joined_extends}")
};
let mut extends = String::new();
for exp in &decl.extends {
write!(extends, " & {}", exp.span.source_text(ctx.source_text())).unwrap();
}

ctx.diagnostic_with_fix(
consistent_type_definitions_diagnostic(
Expand Down
15 changes: 4 additions & 11 deletions crates/oxc_linter/src/rules/typescript/no_empty_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,10 @@ impl Rule for NoEmptyInterface {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
if let AstKind::TSInterfaceDeclaration(interface) = node.kind() {
if interface.body.body.is_empty() {
match &interface.extends {
None => {
ctx.diagnostic(no_empty_interface_diagnostic(interface.span));
}

Some(extends) if extends.len() == 1 => {
if !self.allow_single_extends {
ctx.diagnostic(no_empty_interface_extend_diagnostic(interface.span));
}
}
_ => {}
if interface.extends.is_empty() {
ctx.diagnostic(no_empty_interface_diagnostic(interface.span));
} else if interface.extends.len() == 1 && !self.allow_single_extends {
ctx.diagnostic(no_empty_interface_extend_diagnostic(interface.span));
}
}
}
Expand Down
26 changes: 7 additions & 19 deletions crates/oxc_linter/src/rules/typescript/no_empty_object_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,31 +153,19 @@ fn check_interface_declaration(
allow_interfaces: AllowInterfaces,
allow_with_name: &str,
) {
if matches!(allow_interfaces, AllowInterfaces::Always) {
if allow_interfaces == AllowInterfaces::Always {
return;
}
if interface.id.name.as_str() == allow_with_name {
return;
}
match interface.extends.as_ref() {
Some(extends) if extends.len() == 1 => match allow_interfaces {
AllowInterfaces::WithSingleExtends => (),
_ => ctx.diagnostic(no_empty_object_type_diagnostic(
interface.body.span,
"Do not use an empty interface declaration.",
)),
},
Some(extends) if extends.is_empty() => {
ctx.diagnostic(no_empty_object_type_diagnostic(
interface.body.span,
"Do not use an empty interface declaration.",
));
}
None => ctx.diagnostic(no_empty_object_type_diagnostic(
if interface.extends.is_empty()
|| (allow_interfaces == AllowInterfaces::Never && interface.extends.len() == 1)
{
ctx.diagnostic(no_empty_object_type_diagnostic(
interface.body.span,
"Do not use an empty interface declaration.",
)),
_ => (),
));
}
}

Expand Down Expand Up @@ -209,7 +197,7 @@ fn check_type_literal(
));
}

#[derive(Debug, Default, Clone, Copy)]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
enum AllowInterfaces {
#[default]
Never,
Expand Down
21 changes: 6 additions & 15 deletions crates/oxc_linter/src/rules/typescript/prefer_function_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,24 +89,15 @@ declare_oxc_lint!(
);

fn has_one_super_type(decl: &TSInterfaceDeclaration) -> bool {
if decl.extends.is_none() {
return false;
match decl.extends.len() {
0 => return false,
1 => {}
_ => return true,
}

let decl_extends_vec = decl.extends.as_deref().unwrap();

if decl_extends_vec.is_empty() {
return false;
}

if decl_extends_vec.len() != 1 {
return true;
}

let expr = &decl_extends_vec[0].expression;

let expr = &decl.extends[0].expression;
if let Expression::Identifier(identifier) = expr {
return !matches!(identifier.name.as_str(), "Function");
return &identifier.name != "Function";
}

true
Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_parser/src/ts/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ impl<'a> ParserImpl<'a> {
let type_parameters = self.parse_ts_type_parameters()?;
let (extends, _) = self.parse_heritage_clause()?;
let body = self.parse_ts_interface_body()?;
let extends = extends.map(|e| self.ast.ts_interface_heritages(e));
let extends =
extends.map_or_else(|| self.ast.vec(), |e| self.ast.ts_interface_heritages(e));

self.verify_modifiers(
modifiers,
Expand Down
14 changes: 6 additions & 8 deletions crates/oxc_semantic/src/checker/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,12 @@ pub fn check_ts_interface_declaration<'a>(
decl: &TSInterfaceDeclaration<'a>,
ctx: &SemanticBuilder<'a>,
) {
if let Some(extends) = &decl.extends {
for extend in extends {
if !matches!(
&extend.expression,
Expression::Identifier(_) | Expression::StaticMemberExpression(_),
) {
ctx.error(invalid_interface_extend(extend.span));
}
for extend in &decl.extends {
if !matches!(
&extend.expression,
Expression::Identifier(_) | Expression::StaticMemberExpression(_),
) {
ctx.error(invalid_interface_extend(extend.span));
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions crates/oxc_traverse/src/generated/ancestor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12525,10 +12525,10 @@ impl<'a, 't> TSInterfaceDeclarationWithoutId<'a, 't> {
}

#[inline]
pub fn extends(self) -> &'t Option<Vec<'a, TSInterfaceHeritage<'a>>> {
pub fn extends(self) -> &'t Vec<'a, TSInterfaceHeritage<'a>> {
unsafe {
&*((self.0 as *const u8).add(OFFSET_TS_INTERFACE_DECLARATION_EXTENDS)
as *const Option<Vec<'a, TSInterfaceHeritage<'a>>>)
as *const Vec<'a, TSInterfaceHeritage<'a>>)
}
}

Expand Down Expand Up @@ -12657,10 +12657,10 @@ impl<'a, 't> TSInterfaceDeclarationWithoutTypeParameters<'a, 't> {
}

#[inline]
pub fn extends(self) -> &'t Option<Vec<'a, TSInterfaceHeritage<'a>>> {
pub fn extends(self) -> &'t Vec<'a, TSInterfaceHeritage<'a>> {
unsafe {
&*((self.0 as *const u8).add(OFFSET_TS_INTERFACE_DECLARATION_EXTENDS)
as *const Option<Vec<'a, TSInterfaceHeritage<'a>>>)
as *const Vec<'a, TSInterfaceHeritage<'a>>)
}
}

Expand Down Expand Up @@ -12719,10 +12719,10 @@ impl<'a, 't> TSInterfaceDeclarationWithoutBody<'a, 't> {
}

#[inline]
pub fn extends(self) -> &'t Option<Vec<'a, TSInterfaceHeritage<'a>>> {
pub fn extends(self) -> &'t Vec<'a, TSInterfaceHeritage<'a>> {
unsafe {
&*((self.0 as *const u8).add(OFFSET_TS_INTERFACE_DECLARATION_EXTENDS)
as *const Option<Vec<'a, TSInterfaceHeritage<'a>>>)
as *const Vec<'a, TSInterfaceHeritage<'a>>)
}
}

Expand Down
Loading
Loading