Skip to content
This repository was archived by the owner on Oct 1, 2019. It is now read-only.

Commit a83b4d8

Browse files
author
Danny Weinberg
committed
Filter out the text key from AST nodes when comparing them.
1 parent a314b98 commit a83b4d8

3 files changed

Lines changed: 27 additions & 24 deletions

File tree

tests/python_expressions/__snapshots__/jsfmt.spec.js.snap

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -392,12 +392,10 @@ a ** 2 ** 2
392392
a.b[0] ** 2
393393
a.b[0].read()[1][2].foo().spam()[0].bar ** 2
394394
l[start:end] = l2
395-
396395
# l[::] = l2
397396
# a = \`s\`
398397
# a = \`1 + 2 + f(3, 4)\`
399398
[a, b] = c
400-
401399
# (a, b) = c
402400
[a, (b, c), d] = e
403401
a, (b, c), d = e
@@ -407,7 +405,6 @@ a, (b, c), d = e
407405
408406
l = func()
409407
l = func(10)
410-
411408
# l = func(10, 12, a, b=c, *args)
412409
# l = func(10, 12, a, b=c, **kwargs)
413410
# l = func(10, 12, a, b=c, *args, **kwargs)
@@ -461,9 +458,9 @@ a is not b
461458
a in b
462459
a is b
463460
not a
461+
# We don't wrap this because it doesn't make sense with a 4-space indent.
464462
not my_long_variable_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
465463
3 < x < 5
466-
467464
# (3 < x) < 5
468465
a < b < c < d
469466
@@ -477,30 +474,23 @@ c = d
477474
478475
a.b = 2
479476
x = a.b
480-
481477
# l[:]
482478
# l[::]
483479
l[1:2]
484-
485480
# l[1:]
486481
l[:2]
487-
488482
# l[1::]
489483
# l[:1:]
490484
l[::1]
491-
492485
# l[1:2:]
493486
l[:1:2]
494487
l[1::2]
495488
l[0:1:2]
496-
497489
# a.b.l[:]
498490
a.b.l[1:2]
499-
500491
# a.b.l[1:]
501492
# a.b.l[:2]
502493
a.b.l[0:1:2]
503-
504494
# a[1:2:3, 100]
505495
# a[:2:3, 100]
506496
# a[1::3, 100,]
@@ -532,7 +522,6 @@ from os import path, system
532522
533523
from os import path, system
534524
from os import path as P, system as S
535-
536525
# from os import (path as P, system as S,)
537526
from os import *
538527
@@ -590,6 +579,7 @@ try:
590579
except NameError:
591580
pass
592581
582+
593583
# try:
594584
# a
595585
# b
@@ -674,6 +664,7 @@ def f(*args):
674664
def f(**kwargs):
675665
return 1
676666
667+
677668
# def f(t=()): pass
678669
# def f(a, b, (c, d), e): pass
679670
# def f(a, b, (c, (d, e), f, (g, h))): pass
@@ -711,6 +702,7 @@ def foo():
711702
"""bar"""
712703
return a
713704
705+
714706
# def foo():
715707
# """doc"""; print 1
716708
# a=1

tests/python_strings/__snapshots__/jsfmt.spec.js.snap

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,22 @@ a = "both 'single' and \\"double\\""
8181
a = u"unicode snowman: \\u26C4"
8282
a = r"this does not have a tab: \\t"
8383
84+
# Bytes crash the astexport.py script
85+
# a = b"here's some bytes"
86+
8487
a = """escaped triple quote \\''' in middle"""
8588
a = '''escaped triple quote """ in middle'''
8689
8790
a = """other triple quote ''' in the middle"""
8891
a = '''other triple quote """ in the middle'''
8992
93+
# The interior triple quotes don't get escaped properly
94+
# a = '''both triple quotes """ and \\''' in the middle'''
95+
# a = """both triple quotes ''' and \\""" in the middle"""
96+
97+
# In the future these might be re-flowed, if that's a feature we want to
98+
# implement.
99+
90100
foo('this should NOT be '
91101
'a multi-line string')
92102
@@ -96,6 +106,10 @@ foo("this should NOT be "
96106
foo(r"this should NOT be \\t "
97107
r'a multi-line string \\n')
98108
109+
# Bytes crash the astexport.py script
110+
#foo(bR"this should NOT be \\t "
111+
# Br'a multi-line string \\n')
112+
99113
foo("""this should remain as is """
100114
'''because it is really weird''')
101115

tests_config/run_spec.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,32 +81,29 @@ function run_spec(dirname, parsers, options) {
8181
}
8282
global.run_spec = run_spec;
8383

84-
function stripLocation(ast) {
84+
/**
85+
* This gets rid of extra keys not removed by the massageAST function which are not suitable or relevant when
86+
* comparing two different ASTs.
87+
*/
88+
function stripExtraNonComparableKeys(ast) {
8589
if (Array.isArray(ast)) {
86-
return ast.map(e => stripLocation(e));
90+
return ast.map(e => stripExtraNonComparableKeys(e));
8791
}
8892
if (typeof ast === "object") {
8993
const newObj = {};
9094
for (const key in ast) {
91-
if (
92-
key === "loc" ||
93-
key === "range" ||
94-
key === "raw" ||
95-
key === "comments" ||
96-
key === "parent" ||
97-
key === "prev"
98-
) {
95+
if (key === "text") {
9996
continue;
10097
}
101-
newObj[key] = stripLocation(ast[key]);
98+
newObj[key] = stripExtraNonComparableKeys(ast[key]);
10299
}
103100
return newObj;
104101
}
105102
return ast;
106103
}
107104

108105
function parse(string, opts) {
109-
return stripLocation(prettier.__debug.parse(string, opts));
106+
return stripExtraNonComparableKeys(prettier.__debug.parse(string, opts));
110107
}
111108

112109
function prettyprint(src, filename, options) {

0 commit comments

Comments
 (0)