Browse Source

Don't do ad-hoc serialization, update exclusions

tags/v0.2.2
Dylan Baker 4 years ago
parent
commit
a05e6979af
5 changed files with 43 additions and 62 deletions
  1. 6
    7
      README.md
  2. 10
    31
      index.js
  3. 7
    12
      package-lock.json
  4. 4
    4
      package.json
  5. 16
    8
      test.js

+ 6
- 7
README.md View File

28
 ## Exclusions
28
 ## Exclusions
29
 
29
 
30
 The config file formats that Prettier uses are not able to represent all of
30
 The config file formats that Prettier uses are not able to represent all of
31
-Prettier's default values. For instance, TOML has no concept of `null`, neither
32
-TOML nor JSON can represent `Infinity`, and no format except actual JavaScript
33
-can represent `undefined`. As a result, depending on the chosen format, some
34
-configuration options may be excluded. The current list of exclusions is as
35
-follows:
31
+Prettier's default values. For instance, JSON can't represent `undefined` or
32
+`Infinity`, and no format except actual JavaScript can represent `undefined`.
33
+As a result, depending on the chosen format, some configuration options may be
34
+excluded. The current list of exclusions is as follows:
36
 
35
 
37
 
36
 
38
 | Format | Excluded Options                  |
37
 | Format | Excluded Options                  |
39
 |--------|-----------------------------------|
38
 |--------|-----------------------------------|
40
 | JSON   | `rangeEnd` , `filepath`, `parser` |
39
 | JSON   | `rangeEnd` , `filepath`, `parser` |
41
-| TOML   | `rangeEnd`, `filepath`, `parser`  |
42
-| YAML   | `filepath`, `parser`  |
40
+| TOML   | `rangeEnd`                        |
41
+| YAML   | `filepath`                        |
43
 
42
 
44
 ## License
43
 ## License
45
 
44
 

+ 10
- 31
index.js View File

1
 #!/usr/bin/env node
1
 #!/usr/bin/env node
2
 
2
 
3
 const fs = require('fs');
3
 const fs = require('fs');
4
-const minimist = require('minimist');
5
 const path = require('path');
4
 const path = require('path');
5
+const { inspect } = require('util');
6
+
7
+const minimist = require('minimist');
6
 const prettier = require('prettier');
8
 const prettier = require('prettier');
9
+const TOML = require('@iarna/toml');
10
+const YAML = require('yaml');
7
 
11
 
8
 const defaultConfigFor = function(format) {
12
 const defaultConfigFor = function(format) {
9
   let config = prettier.getSupportInfo().options;
13
   let config = prettier.getSupportInfo().options;
10
 
14
 
11
-  if (!['js', 'yaml'].includes(format)) {
12
-    config = config.filter(
13
-      option => ![null, Infinity].includes(option.default)
14
-    );
15
+  if (format.match(/json/)) {
16
+    config = config.filter(option => option.default !== Infinity);
15
   }
17
   }
16
 
18
 
17
   if (format != 'js') {
19
   if (format != 'js') {
33
   yaml: {
35
   yaml: {
34
     filename: '.prettierrc.yaml',
36
     filename: '.prettierrc.yaml',
35
     generate: function() {
37
     generate: function() {
36
-      return Object.entries(defaultConfigFor('yaml'))
37
-        .map(([key, value]) => {
38
-          if (typeof value === 'string' && value === '') return `${key}: ''`;
39
-          if (value === null) return `${key}: ~`;
40
-          if (value === Infinity) return `${key}: .inf`;
41
-          if (!!value.forEach && value.length === 0) return `${key}: []`;
42
-          return `${key}: ${value}`;
43
-        })
44
-        .join('\n');
38
+      return YAML.stringify(defaultConfigFor('yaml'));
45
     },
39
     },
46
   },
40
   },
47
   toml: {
41
   toml: {
48
     filename: '.prettierrc.toml',
42
     filename: '.prettierrc.toml',
49
     generate: function() {
43
     generate: function() {
50
-      return Object.entries(defaultConfigFor('toml'))
51
-        .map(([key, value]) => {
52
-          if (typeof value === 'string') return `${key} = "${value}"`;
53
-          if (!!value.forEach && value.length === 0) return `${key} = []`;
54
-          return `${key} = ${value}`;
55
-        })
56
-        .join('\n');
44
+      return TOML.stringify(defaultConfigFor('toml'));
57
     },
45
     },
58
   },
46
   },
59
   js: {
47
   js: {
60
     filename: '.prettierrc.js',
48
     filename: '.prettierrc.js',
61
     generate: function() {
49
     generate: function() {
62
-      const contents = Object.entries(defaultConfigFor('js'))
63
-        .map(([key, value]) => {
64
-          if (typeof value === 'string') return `  ${key}: '${value}',`;
65
-          if (typeof value === 'undefined') return `  ${key}: undefined,`;
66
-          if (value === null) return `  ${key}: null,`;
67
-          if (!!value.forEach && value.length === 0) return `${key}: [],`;
68
-          return `  ${key}: ${value},`;
69
-        })
70
-        .join('\n');
71
-      return `module.exports = {\n${contents}\n};`;
50
+      return `module.exports = ${inspect(defaultConfigFor('js'))};`;
72
     },
51
     },
73
   },
52
   },
74
   'package.json': {
53
   'package.json': {

+ 7
- 12
package-lock.json View File

8
       "version": "7.8.4",
8
       "version": "7.8.4",
9
       "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.8.4.tgz",
9
       "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.8.4.tgz",
10
       "integrity": "sha512-neAp3zt80trRVBI1x0azq6c57aNBqYZH8KhMm3TaB7wEI5Q4A2SHfBHE8w9gOhI/lrqxtEbXZgQIrHP+wvSGwQ==",
10
       "integrity": "sha512-neAp3zt80trRVBI1x0azq6c57aNBqYZH8KhMm3TaB7wEI5Q4A2SHfBHE8w9gOhI/lrqxtEbXZgQIrHP+wvSGwQ==",
11
-      "dev": true,
12
       "requires": {
11
       "requires": {
13
         "regenerator-runtime": "^0.13.2"
12
         "regenerator-runtime": "^0.13.2"
14
       }
13
       }
15
     },
14
     },
15
+    "@iarna/toml": {
16
+      "version": "2.2.3",
17
+      "resolved": "https://registry.npmjs.org/@iarna/toml/-/toml-2.2.3.tgz",
18
+      "integrity": "sha512-FmuxfCuolpLl0AnQ2NHSzoUKWEJDFl63qXjzdoWBVyFCXzMGm1spBzk7LeHNoVCiWCF7mRVms9e6jEV9+MoPbg=="
19
+    },
16
     "balanced-match": {
20
     "balanced-match": {
17
       "version": "1.0.0",
21
       "version": "1.0.0",
18
       "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz",
22
       "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz",
346
     "prettier": {
350
     "prettier": {
347
       "version": "1.19.1",
351
       "version": "1.19.1",
348
       "resolved": "https://registry.npmjs.org/prettier/-/prettier-1.19.1.tgz",
352
       "resolved": "https://registry.npmjs.org/prettier/-/prettier-1.19.1.tgz",
349
-      "integrity": "sha512-s7PoyDv/II1ObgQunCbB9PdLmUcBZcnWOcxDh7O0N/UwDEsHyqkW+Qh28jW+mVuCdx7gLB0BotYI1Y6uI9iyew==",
350
-      "dev": true
353
+      "integrity": "sha512-s7PoyDv/II1ObgQunCbB9PdLmUcBZcnWOcxDh7O0N/UwDEsHyqkW+Qh28jW+mVuCdx7gLB0BotYI1Y6uI9iyew=="
351
     },
354
     },
352
     "readable-stream": {
355
     "readable-stream": {
353
       "version": "1.1.14",
356
       "version": "1.1.14",
364
     "regenerator-runtime": {
367
     "regenerator-runtime": {
365
       "version": "0.13.3",
368
       "version": "0.13.3",
366
       "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.13.3.tgz",
369
       "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.13.3.tgz",
367
-      "integrity": "sha512-naKIZz2GQ8JWh///G7L3X6LaQUAMp2lvb1rvwwsURe/VXwD6VMfr+/1NuNw3ag8v2kY1aQ/go5SNn79O9JU7yw==",
368
-      "dev": true
370
+      "integrity": "sha512-naKIZz2GQ8JWh///G7L3X6LaQUAMp2lvb1rvwwsURe/VXwD6VMfr+/1NuNw3ag8v2kY1aQ/go5SNn79O9JU7yw=="
369
     },
371
     },
370
     "regexp.prototype.flags": {
372
     "regexp.prototype.flags": {
371
       "version": "1.3.0",
373
       "version": "1.3.0",
487
         "xtend": "~2.1.1"
489
         "xtend": "~2.1.1"
488
       }
490
       }
489
     },
491
     },
490
-    "toml": {
491
-      "version": "3.0.0",
492
-      "resolved": "https://registry.npmjs.org/toml/-/toml-3.0.0.tgz",
493
-      "integrity": "sha512-y/mWCZinnvxjTKYhJ+pYxwD0mRLVvOtdS2Awbgxln6iEnt4rk0yBxeSBHkGJcPucRiG0e55mwWp+g/05rsrd6w==",
494
-      "dev": true
495
-    },
496
     "wrappy": {
492
     "wrappy": {
497
       "version": "1.0.2",
493
       "version": "1.0.2",
498
       "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
494
       "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
520
       "version": "1.7.2",
516
       "version": "1.7.2",
521
       "resolved": "https://registry.npmjs.org/yaml/-/yaml-1.7.2.tgz",
517
       "resolved": "https://registry.npmjs.org/yaml/-/yaml-1.7.2.tgz",
522
       "integrity": "sha512-qXROVp90sb83XtAoqE8bP9RwAkTTZbugRUTm5YeFCBfNRPEp2YzTeqWiz7m5OORHzEvrA/qcGS8hp/E+MMROYw==",
518
       "integrity": "sha512-qXROVp90sb83XtAoqE8bP9RwAkTTZbugRUTm5YeFCBfNRPEp2YzTeqWiz7m5OORHzEvrA/qcGS8hp/E+MMROYw==",
523
-      "dev": true,
524
       "requires": {
519
       "requires": {
525
         "@babel/runtime": "^7.6.3"
520
         "@babel/runtime": "^7.6.3"
526
       }
521
       }

+ 4
- 4
package.json View File

17
     "index.js"
17
     "index.js"
18
   ],
18
   ],
19
   "dependencies": {
19
   "dependencies": {
20
+    "@iarna/toml": "^2.2.3",
20
     "minimist": "^1.2.0",
21
     "minimist": "^1.2.0",
21
-    "prettier": "^1.19.1"
22
+    "prettier": "^1.19.1",
23
+    "yaml": "^1.7.2"
22
   },
24
   },
23
   "devDependencies": {
25
   "devDependencies": {
24
     "faucet": "0.0.1",
26
     "faucet": "0.0.1",
25
-    "tape": "^4.13.0",
26
-    "toml": "^3.0.0",
27
-    "yaml": "^1.7.2"
27
+    "tape": "^4.13.0"
28
   }
28
   }
29
 }
29
 }

+ 16
- 8
test.js View File

1
 const fs = require('fs');
1
 const fs = require('fs');
2
 const test = require('tape');
2
 const test = require('tape');
3
-const TOML = require('toml');
3
+const TOML = require('@iarna/toml');
4
 const YAML = require('yaml');
4
 const YAML = require('yaml');
5
 
5
 
6
 const { defaultConfigFor, formats } = require('.');
6
 const { defaultConfigFor, formats } = require('.');
43
   t.equal(YAML.parse(result).rangeEnd, Infinity);
43
   t.equal(YAML.parse(result).rangeEnd, Infinity);
44
 });
44
 });
45
 
45
 
46
-test("YAML doesn't support undefined so leave out filepath and parser", t => {
46
+test('TOML supports infinity, leave rangeEnd in', t => {
47
   t.plan(2);
47
   t.plan(2);
48
-  const result = formats.yaml.generate();
48
+  const result = TOML.parse(formats.toml.generate());
49
+  t.true(Object.keys(result).includes('rangeEnd'));
50
+  t.equal(result.rangeEnd, Infinity);
51
+});
52
+
53
+test("JSON doesn't support undefined, leave out filepath and parser", t => {
54
+  t.plan(2);
55
+  const result = JSON.parse(formats.json.generate());
49
   t.false(Object.keys(result).includes('parser'));
56
   t.false(Object.keys(result).includes('parser'));
50
   t.false(Object.keys(result).includes('filepath'));
57
   t.false(Object.keys(result).includes('filepath'));
51
 });
58
 });
52
 
59
 
53
-test("TOML doesn't support infinity, so leave out rangeEnd", t => {
54
-  t.plan(1);
55
-  const result = formats.toml.generate();
56
-  t.false(Object.keys(result).includes('rangeEnd'));
60
+test("YAML doesn't support undefined, leave out filepath and parser", t => {
61
+  t.plan(2);
62
+  const result = YAML.parse(formats.yaml.generate());
63
+  t.false(Object.keys(result).includes('parser'));
64
+  t.false(Object.keys(result).includes('filepath'));
57
 });
65
 });
58
 
66
 
59
-test('JS supports undefined, so leave in parser and filepath', t => {
67
+test('JS supports undefined, leave in parser and filepath', t => {
60
   t.plan(4);
68
   t.plan(4);
61
   const result = eval(formats.js.generate());
69
   const result = eval(formats.js.generate());
62
   t.equal(result.parser, undefined);
70
   t.equal(result.parser, undefined);

Loading…
Cancel
Save