Skip to content

Commit fa2f585

Browse files
committed
Remove class name from ast
1 parent b3230e1 commit fa2f585

File tree

9 files changed

+150
-85
lines changed

9 files changed

+150
-85
lines changed

parser/src/ast.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,12 @@ pub struct Name {
334334
pub parenthesized: bool,
335335
}
336336

337+
impl Name {
338+
pub fn get_value<'a>(&self, source: &'a str) -> &'a str {
339+
&source[(self.node.start) as usize..(self.node.end) as usize]
340+
}
341+
}
342+
337343
impl fmt::Debug for Name {
338344
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
339345
f.debug_struct("Name")
@@ -956,14 +962,20 @@ impl AsyncFunctionDef {
956962
#[derive(Debug, Clone)]
957963
pub struct ClassDef {
958964
pub node: Node,
959-
pub name: String,
965+
pub name: TextRange,
960966
pub bases: Vec<Expression>,
961967
pub keywords: Vec<Keyword>,
962968
pub body: Vec<Statement>,
963969
pub decorator_list: Vec<Expression>,
964970
pub type_params: Vec<TypeParam>,
965971
}
966972

973+
impl ClassDef {
974+
pub fn name<'a>(&self, source: &'a str) -> &'a str {
975+
&source[(self.name.start) as usize..(self.name.end) as usize]
976+
}
977+
}
978+
967979
// https://docs.python.org/3/library/ast.html#ast.Match
968980
#[derive(Debug, Clone)]
969981
pub struct Match {

parser/src/parser/compat.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ impl AsPythonCompat for Expression {
276276
impl AsPythonCompat for Name {
277277
fn as_python_compat(&self, parser: &Parser) -> Value {
278278
json_python_compat_node!("Name", self, parser, {
279-
"id": self.id,
279+
"id": "TODO",
280280
})
281281
}
282282
}
@@ -732,7 +732,7 @@ impl AsPythonCompat for ExceptHandler {
732732
impl AsPythonCompat for FunctionDef {
733733
fn as_python_compat(&self, parser: &Parser) -> Value {
734734
json_python_compat_node!("FunctionDef", self, parser, {
735-
"name": self.name,
735+
// "name": self.name,
736736
"args": self.args.as_python_compat(parser),
737737
"body": self.body.iter().map(|stmt| stmt.as_python_compat(parser)).collect::<Vec<_>>(),
738738
"decorator_list": self.decorator_list.iter().map(|expr| expr.as_python_compat(parser)).collect::<Vec<_>>(),
@@ -760,7 +760,7 @@ impl AsPythonCompat for AsyncFunctionDef {
760760
impl AsPythonCompat for ClassDef {
761761
fn as_python_compat(&self, parser: &Parser) -> Value {
762762
json_python_compat_node!("ClassDef", self, parser, {
763-
"name": self.name,
763+
// "name": self.name,
764764
"bases": self.bases.iter().map(|expr| expr.as_python_compat(parser)).collect::<Vec<_>>(),
765765
"keywords": self.keywords.iter().map(|kw| kw.as_python_compat(parser)).collect::<Vec<_>>(),
766766
"body": self.body.iter().map(|stmt| stmt.as_python_compat(parser)).collect::<Vec<_>>(),

parser/src/parser/parser.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,10 @@ impl<'a> Parser<'a> {
656656
self.start_node()
657657
};
658658
self.expect(Kind::Class)?;
659-
let name = self.cur_token().to_string(self.source);
659+
let name_range = TextRange {
660+
start: self.cur_token().start,
661+
end: self.cur_token().end,
662+
};
660663
self.expect(Kind::Identifier)?;
661664
let type_params = if self.at(Kind::LeftBrace) {
662665
self.parse_type_parameters()?
@@ -675,7 +678,7 @@ impl<'a> Parser<'a> {
675678

676679
Ok(Statement::ClassDef(Arc::new(ClassDef {
677680
node: self.finish_node(node),
678-
name,
681+
name: name_range,
679682
bases,
680683
keywords,
681684
body,

parser/test_data/output/enderpy_python_parser__parser__parser__tests__class.snap

Lines changed: 80 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ Module {
1414
start: 0,
1515
end: 13,
1616
},
17-
name: "a",
17+
name: TextRange {
18+
start: 6,
19+
end: 7,
20+
},
1821
bases: [],
1922
keywords: [],
2023
body: [
@@ -37,7 +40,10 @@ Module {
3740
start: 15,
3841
end: 36,
3942
},
40-
name: "a",
43+
name: TextRange {
44+
start: 21,
45+
end: 22,
46+
},
4147
bases: [],
4248
keywords: [],
4349
body: [
@@ -60,7 +66,10 @@ Module {
6066
start: 36,
6167
end: 55,
6268
},
63-
name: "a",
69+
name: TextRange {
70+
start: 42,
71+
end: 43,
72+
},
6473
bases: [
6574
Name(
6675
Name {
@@ -102,7 +111,10 @@ Module {
102111
start: 57,
103112
end: 82,
104113
},
105-
name: "a",
114+
name: TextRange {
115+
start: 63,
116+
end: 64,
117+
},
106118
bases: [
107119
Name(
108120
Name {
@@ -169,7 +181,10 @@ Module {
169181
start: 84,
170182
end: 106,
171183
},
172-
name: "a",
184+
name: TextRange {
185+
start: 90,
186+
end: 91,
187+
},
173188
bases: [
174189
Name(
175190
Name {
@@ -220,7 +235,10 @@ Module {
220235
start: 108,
221236
end: 128,
222237
},
223-
name: "a",
238+
name: TextRange {
239+
start: 121,
240+
end: 122,
241+
},
224242
bases: [],
225243
keywords: [],
226244
body: [
@@ -253,7 +271,10 @@ Module {
253271
start: 130,
254272
end: 146,
255273
},
256-
name: "a",
274+
name: TextRange {
275+
start: 136,
276+
end: 137,
277+
},
257278
bases: [],
258279
keywords: [],
259280
body: [
@@ -287,7 +308,10 @@ Module {
287308
start: 148,
288309
end: 167,
289310
},
290-
name: "a",
311+
name: TextRange {
312+
start: 154,
313+
end: 155,
314+
},
291315
bases: [],
292316
keywords: [],
293317
body: [
@@ -331,7 +355,10 @@ Module {
331355
start: 169,
332356
end: 188,
333357
},
334-
name: "a",
358+
name: TextRange {
359+
start: 175,
360+
end: 176,
361+
},
335362
bases: [],
336363
keywords: [],
337364
body: [
@@ -375,7 +402,10 @@ Module {
375402
start: 190,
376403
end: 215,
377404
},
378-
name: "a",
405+
name: TextRange {
406+
start: 196,
407+
end: 197,
408+
},
379409
bases: [],
380410
keywords: [],
381411
body: [
@@ -439,7 +469,10 @@ Module {
439469
start: 217,
440470
end: 234,
441471
},
442-
name: "a",
472+
name: TextRange {
473+
start: 223,
474+
end: 224,
475+
},
443476
bases: [],
444477
keywords: [],
445478
body: [
@@ -472,7 +505,10 @@ Module {
472505
start: 236,
473506
end: 256,
474507
},
475-
name: "a",
508+
name: TextRange {
509+
start: 242,
510+
end: 243,
511+
},
476512
bases: [],
477513
keywords: [],
478514
body: [
@@ -515,7 +551,10 @@ Module {
515551
start: 258,
516552
end: 281,
517553
},
518-
name: "a",
554+
name: TextRange {
555+
start: 264,
556+
end: 265,
557+
},
519558
bases: [],
520559
keywords: [],
521560
body: [
@@ -568,7 +607,10 @@ Module {
568607
start: 283,
569608
end: 312,
570609
},
571-
name: "a",
610+
name: TextRange {
611+
start: 289,
612+
end: 290,
613+
},
572614
bases: [],
573615
keywords: [],
574616
body: [
@@ -641,7 +683,10 @@ Module {
641683
start: 314,
642684
end: 332,
643685
},
644-
name: "a",
686+
name: TextRange {
687+
start: 320,
688+
end: 321,
689+
},
645690
bases: [],
646691
keywords: [],
647692
body: [
@@ -674,7 +719,10 @@ Module {
674719
start: 334,
675720
end: 355,
676721
},
677-
name: "a",
722+
name: TextRange {
723+
start: 340,
724+
end: 341,
725+
},
678726
bases: [],
679727
keywords: [],
680728
body: [
@@ -717,7 +765,10 @@ Module {
717765
start: 357,
718766
end: 381,
719767
},
720-
name: "a",
768+
name: TextRange {
769+
start: 363,
770+
end: 364,
771+
},
721772
bases: [],
722773
keywords: [],
723774
body: [
@@ -770,7 +821,10 @@ Module {
770821
start: 383,
771822
end: 413,
772823
},
773-
name: "a",
824+
name: TextRange {
825+
start: 389,
826+
end: 390,
827+
},
774828
bases: [],
775829
keywords: [],
776830
body: [
@@ -843,7 +897,10 @@ Module {
843897
start: 415,
844898
end: 440,
845899
},
846-
name: "a",
900+
name: TextRange {
901+
start: 421,
902+
end: 422,
903+
},
847904
bases: [],
848905
keywords: [],
849906
body: [
@@ -895,7 +952,10 @@ Module {
895952
start: 442,
896953
end: 470,
897954
},
898-
name: "a",
955+
name: TextRange {
956+
start: 448,
957+
end: 449,
958+
},
899959
bases: [],
900960
keywords: [],
901961
body: [

parser/test_data/output/enderpy_python_parser__parser__parser__tests__newlines.snap

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,10 @@ Module {
211211
start: 102,
212212
end: 135,
213213
},
214-
name: "A",
214+
name: TextRange {
215+
start: 108,
216+
end: 109,
217+
},
215218
bases: [
216219
Name(
217220
Name {

typechecker/src/checker.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -347,11 +347,9 @@ impl<'a> TraversalVisitor for TypeChecker<'a> {
347347
}
348348

349349
fn visit_class_def(&mut self, c: &Arc<parser::ast::ClassDef>) {
350-
self.infer_name_type(
351-
&c.name,
352-
c.node.start + 6,
353-
c.node.start + 6 + c.name.len() as u32,
354-
);
350+
let source = &self.build_manager.files.get(&self.id).unwrap().source;
351+
let name = c.name(source);
352+
self.infer_name_type(name, c.name.start, c.name.end);
355353

356354
self.enter_scope(c.node.start);
357355
for base in &c.bases {

0 commit comments

Comments
 (0)