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

Commit a58a6ce

Browse files
authored
Re-enable AST_COMPARE (#17)
* Re-enable AST_COMPARE * Get AST_COMPARE passing * Always use source text for numbers * Serialize Python numbers to JSON strings, not numbers
1 parent a2ef27e commit a58a6ce

File tree

9 files changed

+98
-49
lines changed

9 files changed

+98
-49
lines changed

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,4 @@ before_install:
2121
- pyenv install -s 3.6.3
2222
- pyenv global 2.7.11 3.6.3
2323
script:
24-
- yarn lint
25-
- yarn test -- --runInBand
24+
- AST_COMPARE=1 yarn test -- --runInBand

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"node": ">=6"
1515
},
1616
"dependencies": {
17-
"prettier": "prettier/prettier#b63c669ed9821a0e0d6918243ef4f4a091b222a9"
17+
"prettier": "prettier/prettier#dbe0758b487be2da74a3f8cf4036472685d75fb0"
1818
},
1919
"devDependencies": {
2020
"eslint": "^4.14.0",
@@ -26,8 +26,9 @@
2626
"jest-runner-eslint": "^0.3.0"
2727
},
2828
"scripts": {
29-
"lint": "prettier src/**/*.js --list-different",
30-
"test": "jest"
29+
"lint": "jest -c jest.eslint.config.js",
30+
"test": "jest",
31+
"prettier": "prettier --plugin=. --parser=python"
3132
},
3233
"jest": {
3334
"projects": ["<rootDir>/jest.*.config.js"]

src/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,18 @@ function printComment(commentPath) {
3939
}
4040
}
4141

42+
function clean(ast, newObj) {
43+
delete newObj.lineno;
44+
delete newObj.col_offset;
45+
}
46+
4247
const printers = {
4348
python: {
4449
print,
4550
hasPrettierIgnore: util.hasIgnoreComment,
4651
printComment,
47-
canAttachComment
52+
canAttachComment,
53+
massageAstNode: clean
4854
}
4955
};
5056

src/printer/index.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -297,12 +297,9 @@ function genericPrint(path, options, print) {
297297
}
298298

299299
case "Num": {
300-
return path.call(print, "n");
301-
}
302-
303-
case "float":
304-
case "int": {
305-
return `${n.n}`;
300+
// This is overly cautious, we may want to revisit and normalize more
301+
// cases here.
302+
return n.source;
306303
}
307304

308305
case "Name": {
@@ -332,10 +329,15 @@ function genericPrint(path, options, print) {
332329
}
333330

334331
case "Tuple": {
332+
const parent = path.getParentNode();
335333
const needsParens =
336-
["List", "Tuple"].indexOf(path.getParentNode().ast_type) !== -1;
334+
parent.ast_type === "List" || parent.ast_type === "Tuple";
335+
const trailingComma = n.elts.length <= 1;
337336

338-
const elts = join(", ", path.map(print, "elts"));
337+
const elts = concat([
338+
join(", ", path.map(print, "elts")),
339+
trailingComma ? "," : ""
340+
]);
339341

340342
if (needsParens) {
341343
return concat(["(", elts, ")"]);

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

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ exports[`expressions.py 1`] = `
1515
1.8e19
1616
90000000000000
1717
90000000000000.
18-
# 3j
18+
3j
1919
2020
x = a + 1
2121
x = 1 - a
@@ -85,7 +85,8 @@ l = [i for j in range(10) for i in range(j) if j%2 == 0]
8585
l = [i for j in range(10) for i in range(j) if j%2 == 0 and i%2 == 0]
8686
# l = [(a, b) for (a,b,c) in l2]
8787
# l = [{a:b} for (a,b,c) in l2]
88-
l = [i for j in k if j%2 == 0 if j*2 < 20 for i in j if i%2==0]
88+
# https://github.com/prettier/prettier-python/issues/18
89+
# l = [i for j in k if j%2 == 0 if j*2 < 20 for i in j if i%2==0]
8990
9091
# l = (i for i in j)
9192
# l = (i for i in j if i%2 == 0)
@@ -341,19 +342,21 @@ a=1;a|=2
341342
342343
-3
343344
344-
24
345+
0x18
345346
346-
1
347+
1.0
347348
348349
3.9
349350
350351
-3.6
351352
352-
18000000000000000000
353+
1.8e19
353354
354355
90000000000000
355356
356-
90000000000000
357+
90000000000000.
358+
359+
3j
357360
358361
x = a + 1
359362
@@ -383,7 +386,7 @@ x, y, z = 1, 2, 3
383386
384387
l[0]
385388
386-
k[v]
389+
k[v,]
387390
388391
m[a, b]
389392
@@ -453,8 +456,6 @@ l = [i for j in range(10) for i in range(j) if j % 2 == 0]
453456
454457
l = [i for j in range(10) for i in range(j) if j % 2 == 0 and i % 2 == 0]
455458
456-
l = [i for j in k if j % 2 == 0 j * 2 < 20 for i in j if i % 2 == 0]
457-
458459
l = {a: b,"c": 0}
459460
460461
l = {}
@@ -719,7 +720,7 @@ exports[`expressions.py 2`] = `
719720
1.8e19
720721
90000000000000
721722
90000000000000.
722-
# 3j
723+
3j
723724
724725
x = a + 1
725726
x = 1 - a
@@ -789,7 +790,8 @@ l = [i for j in range(10) for i in range(j) if j%2 == 0]
789790
l = [i for j in range(10) for i in range(j) if j%2 == 0 and i%2 == 0]
790791
# l = [(a, b) for (a,b,c) in l2]
791792
# l = [{a:b} for (a,b,c) in l2]
792-
l = [i for j in k if j%2 == 0 if j*2 < 20 for i in j if i%2==0]
793+
# https://github.com/prettier/prettier-python/issues/18
794+
# l = [i for j in k if j%2 == 0 if j*2 < 20 for i in j if i%2==0]
793795
794796
# l = (i for i in j)
795797
# l = (i for i in j if i%2 == 0)
@@ -1048,22 +1050,22 @@ a=1;a|=2
10481050
-3
10491051
10501052
# 053
1051-
24
1053+
0x18
10521054
10531055
# 14L
1054-
1
1056+
1.0
10551057
10561058
3.9
10571059
10581060
-3.6
10591061
1060-
18000000000000000000
1062+
1.8e19
10611063
10621064
90000000000000
10631065
1064-
90000000000000
1066+
90000000000000.
10651067
1066-
# 3j
1068+
3j
10671069
10681070
x = a + 1
10691071
@@ -1098,7 +1100,7 @@ x, y, z = 1, 2, 3
10981100
# del foo.bar
10991101
l[0]
11001102
1101-
k[v]
1103+
k[v,]
11021104
11031105
m[a, b]
11041106
@@ -1179,7 +1181,8 @@ l = [i for j in range(10) for i in range(j) if j % 2 == 0 and i % 2 == 0]
11791181
11801182
# l = [(a, b) for (a,b,c) in l2]
11811183
# l = [{a:b} for (a,b,c) in l2]
1182-
l = [i for j in k if j % 2 == 0 j * 2 < 20 for i in j if i % 2 == 0]
1184+
# https://github.com/prettier/prettier-python/issues/18
1185+
# l = [i for j in k if j%2 == 0 if j*2 < 20 for i in j if i%2==0]
11831186
11841187
# l = (i for i in j)
11851188
# l = (i for i in j if i%2 == 0)

tests/python_expressions/expressions.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
1.8e19
1313
90000000000000
1414
90000000000000.
15-
# 3j
15+
3j
1616

1717
x = a + 1
1818
x = 1 - a
@@ -82,7 +82,8 @@
8282
l = [i for j in range(10) for i in range(j) if j%2 == 0 and i%2 == 0]
8383
# l = [(a, b) for (a,b,c) in l2]
8484
# l = [{a:b} for (a,b,c) in l2]
85-
l = [i for j in k if j%2 == 0 if j*2 < 20 for i in j if i%2==0]
85+
# https://github.com/prettier/prettier-python/issues/18
86+
# l = [i for j in k if j%2 == 0 if j*2 < 20 for i in j if i%2==0]
8687

8788
# l = (i for i in j)
8889
# l = (i for i in j if i%2 == 0)

tests_config/run_spec.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const fs = require("fs");
44
const extname = require("path").extname;
55
const prettier = require("prettier");
66
const massageAST = require("prettier/src/common/clean-ast").massageAST;
7+
const normalizeOptions = require("prettier/src/main/options").normalize;
78

89
const AST_COMPARE = process.env["AST_COMPARE"];
910

@@ -53,15 +54,16 @@ function run_spec(dirname, parsers, options) {
5354

5455
if (AST_COMPARE) {
5556
const ast = parse(source, mergedOptions);
56-
const astMassaged = massageAST(ast);
57+
const normalizedOptions = normalizeOptions(mergedOptions);
58+
const astMassaged = massageAST(ast, normalizedOptions);
5759
let ppastMassaged;
5860
let pperr = null;
5961
try {
6062
const ppast = parse(
6163
prettyprint(source, path, mergedOptions),
6264
mergedOptions
6365
);
64-
ppastMassaged = massageAST(ppast);
66+
ppastMassaged = massageAST(ppast, normalizedOptions);
6567
} catch (e) {
6668
pperr = e.stack;
6769
}

vendor/python/astexport.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,18 @@ def visit_field_Num_n(self, val):
8787
if isinstance(val, int):
8888
return {
8989
self.ast_type_field: "int",
90-
"n": val
90+
"n": str(val)
9191
}
9292
elif isinstance(val, float):
9393
return {
9494
self.ast_type_field: "float",
95-
"n": val
95+
"n": str(val)
9696
}
9797
elif isinstance(val, complex):
9898
return {
9999
self.ast_type_field: "complex",
100-
"n": val.real,
101-
"i": val.imag
100+
"n": str(val.real),
101+
"i": str(val.imag)
102102
}
103103

104104

yarn.lock

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,31 @@
1010
esutils "^2.0.2"
1111
js-tokens "^3.0.0"
1212

13+
"@glimmer/interfaces@^0.30.3":
14+
version "0.30.3"
15+
resolved "https://registry.yarnpkg.com/@glimmer/interfaces/-/interfaces-0.30.3.tgz#ea53e6b945ae3cc14588e655626ad9c6ed90a9f9"
16+
dependencies:
17+
"@glimmer/wire-format" "^0.30.3"
18+
19+
"@glimmer/[email protected]":
20+
version "0.30.3"
21+
resolved "https://registry.yarnpkg.com/@glimmer/syntax/-/syntax-0.30.3.tgz#31ca56d00b4f7c63af13aeb70b5d58a9b250f02e"
22+
dependencies:
23+
"@glimmer/interfaces" "^0.30.3"
24+
"@glimmer/util" "^0.30.3"
25+
handlebars "^4.0.6"
26+
simple-html-tokenizer "^0.4.1"
27+
28+
"@glimmer/util@^0.30.3":
29+
version "0.30.3"
30+
resolved "https://registry.yarnpkg.com/@glimmer/util/-/util-0.30.3.tgz#c771fce2d766c380ae50d3ad8045eff224637e8c"
31+
32+
"@glimmer/wire-format@^0.30.3":
33+
version "0.30.3"
34+
resolved "https://registry.yarnpkg.com/@glimmer/wire-format/-/wire-format-0.30.3.tgz#32629d99f3107b4c940cf84607e417686e55b94d"
35+
dependencies:
36+
"@glimmer/util" "^0.30.3"
37+
1338
"@types/node@*":
1439
version "8.5.2"
1540
resolved "https://registry.yarnpkg.com/@types/node/-/node-8.5.2.tgz#83b8103fa9a2c2e83d78f701a9aa7c9539739aa5"
@@ -671,6 +696,10 @@ decamelize@^1.0.0, decamelize@^1.1.1:
671696
version "1.2.0"
672697
resolved "https://registry.yarnpkg.com/decamelize/-/decamelize-1.2.0.tgz#f6534d15148269b20352e7bee26f501f9a191290"
673698

699+
700+
version "0.7.0"
701+
resolved "https://registry.yarnpkg.com/dedent/-/dedent-0.7.0.tgz#2495ddbaf6eb874abb0e1be9df22d2e5a544326c"
702+
674703
deep-extend@~0.4.0:
675704
version "0.4.2"
676705
resolved "https://registry.yarnpkg.com/deep-extend/-/deep-extend-0.4.2.tgz#48b699c27e334bf89f10892be432f6e4c7d34a7f"
@@ -1289,17 +1318,17 @@ graceful-fs@^4.1.11, graceful-fs@^4.1.2:
12891318
version "4.1.11"
12901319
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.11.tgz#0e8bdfe4d1ddb8854d64e04ea7c00e2a026e5658"
12911320

1292-
graphql@0.10.5:
1293-
version "0.10.5"
1294-
resolved "https://registry.yarnpkg.com/graphql/-/graphql-0.10.5.tgz#c9be17ca2bdfdbd134077ffd9bbaa48b8becd298"
1321+
graphql@0.12.3:
1322+
version "0.12.3"
1323+
resolved "https://registry.yarnpkg.com/graphql/-/graphql-0.12.3.tgz#11668458bbe28261c0dcb6e265f515ba79f6ce07"
12951324
dependencies:
1296-
iterall "^1.1.0"
1325+
iterall "1.1.3"
12971326

12981327
growly@^1.3.0:
12991328
version "1.3.0"
13001329
resolved "https://registry.yarnpkg.com/growly/-/growly-1.3.0.tgz#f10748cbe76af964b7c96c93c6bcc28af120c081"
13011330

1302-
handlebars@^4.0.3:
1331+
handlebars@^4.0.3, handlebars@^4.0.6:
13031332
version "4.0.11"
13041333
resolved "https://registry.yarnpkg.com/handlebars/-/handlebars-4.0.11.tgz#630a35dfe0294bc281edae6ffc5d329fc7982dcc"
13051334
dependencies:
@@ -1706,7 +1735,7 @@ istanbul-reports@^1.1.3:
17061735
dependencies:
17071736
handlebars "^4.0.3"
17081737

1709-
iterall@^1.1.0:
1738+
17101739
version "1.1.3"
17111740
resolved "https://registry.yarnpkg.com/iterall/-/iterall-1.1.3.tgz#1cbbff96204056dde6656e2ed2e2226d0e6d72c9"
17121741

@@ -2572,17 +2601,19 @@ preserve@^0.2.0:
25722601
version "0.2.0"
25732602
resolved "https://registry.yarnpkg.com/preserve/-/preserve-0.2.0.tgz#815ed1f6ebc65926f865b310c0713bcb3315ce4b"
25742603

2575-
prettier@prettier/prettier#b63c669ed9821a0e0d6918243ef4f4a091b222a9:
2604+
prettier@prettier/prettier#dbe0758b487be2da74a3f8cf4036472685d75fb0:
25762605
version "1.9.2"
2577-
resolved "https://codeload.github.com/prettier/prettier/tar.gz/b63c669ed9821a0e0d6918243ef4f4a091b222a9"
2606+
resolved "https://codeload.github.com/prettier/prettier/tar.gz/dbe0758b487be2da74a3f8cf4036472685d75fb0"
25782607
dependencies:
25792608
"@babel/code-frame" "7.0.0-beta.35"
2609+
"@glimmer/syntax" "0.30.3"
25802610
babylon "7.0.0-beta.34"
25812611
camelcase "4.1.0"
25822612
chalk "2.1.0"
25832613
cjk-regex "1.0.2"
25842614
cosmiconfig "3.1.0"
25852615
dashify "0.2.2"
2616+
dedent "0.7.0"
25862617
diff "3.2.0"
25872618
editorconfig "0.14.2"
25882619
editorconfig-to-prettier "0.0.6"
@@ -2593,7 +2624,7 @@ prettier@prettier/prettier#b63c669ed9821a0e0d6918243ef4f4a091b222a9:
25932624
flow-parser "0.59.0"
25942625
get-stream "3.0.0"
25952626
globby "6.1.0"
2596-
graphql "0.10.5"
2627+
graphql "0.12.3"
25972628
ignore "3.3.7"
25982629
jest-docblock "21.3.0-beta.11"
25992630
jest-validate "21.1.0"
@@ -2947,6 +2978,10 @@ signal-exit@^3.0.0, signal-exit@^3.0.2:
29472978
version "3.0.2"
29482979
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.2.tgz#b5fdc08f1287ea1178628e415e25132b73646c6d"
29492980

2981+
simple-html-tokenizer@^0.4.1:
2982+
version "0.4.3"
2983+
resolved "https://registry.yarnpkg.com/simple-html-tokenizer/-/simple-html-tokenizer-0.4.3.tgz#9b00b766e30058b4bb377c0d4f97566a13ab1be1"
2984+
29502985
slash@^1.0.0:
29512986
version "1.0.0"
29522987
resolved "https://registry.yarnpkg.com/slash/-/slash-1.0.0.tgz#c41f2f6c39fc16d1cd17ad4b5d896114ae470d55"

0 commit comments

Comments
 (0)