3 Commits

Author SHA1 Message Date
  Dylan Baker af7ff4fb49 0.2.2 4 years ago
  Dylan Baker a05e6979af Don't do ad-hoc serialization, update exclusions 4 years ago
  Dylan Baker c03e251dc5 Restore prettierrc 4 years ago
6 changed files with 66 additions and 64 deletions
  1. 21
    0
      .prettierrc
  2. 6
    7
      README.md
  3. 10
    31
      index.js
  4. 8
    13
      package-lock.json
  5. 5
    5
      package.json
  6. 16
    8
      test.js

+ 21
- 0
.prettierrc View File

@@ -0,0 +1,21 @@
1
+{
2
+  "arrowParens": "avoid",
3
+  "bracketSpacing": true,
4
+  "endOfLine": "auto",
5
+  "filepath": "",
6
+  "htmlWhitespaceSensitivity": "css",
7
+  "insertPragma": false,
8
+  "jsxBracketSameLine": false,
9
+  "jsxSingleQuote": false,
10
+  "parser": "",
11
+  "printWidth": 80,
12
+  "proseWrap": "preserve",
13
+  "quoteProps": "as-needed",
14
+  "requirePragma": false,
15
+  "semi": true,
16
+  "singleQuote": true,
17
+  "tabWidth": 2,
18
+  "trailingComma": "es5",
19
+  "useTabs": false,
20
+  "vueIndentScriptAndStyle": false
21
+}

+ 6
- 7
README.md View File

@@ -28,18 +28,17 @@ OPTIONS:
28 28
 ## Exclusions
29 29
 
30 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 37
 | Format | Excluded Options                  |
39 38
 |--------|-----------------------------------|
40 39
 | JSON   | `rangeEnd` , `filepath`, `parser` |
41
-| TOML   | `rangeEnd`, `filepath`, `parser`  |
42
-| YAML   | `filepath`, `parser`  |
40
+| TOML   | `rangeEnd`                        |
41
+| YAML   | `filepath`                        |
43 42
 
44 43
 ## License
45 44
 

+ 10
- 31
index.js View File

@@ -1,17 +1,19 @@
1 1
 #!/usr/bin/env node
2 2
 
3 3
 const fs = require('fs');
4
-const minimist = require('minimist');
5 4
 const path = require('path');
5
+const { inspect } = require('util');
6
+
7
+const minimist = require('minimist');
6 8
 const prettier = require('prettier');
9
+const TOML = require('@iarna/toml');
10
+const YAML = require('yaml');
7 11
 
8 12
 const defaultConfigFor = function(format) {
9 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 19
   if (format != 'js') {
@@ -33,42 +35,19 @@ const formats = {
33 35
   yaml: {
34 36
     filename: '.prettierrc.yaml',
35 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 41
   toml: {
48 42
     filename: '.prettierrc.toml',
49 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 47
   js: {
60 48
     filename: '.prettierrc.js',
61 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 53
   'package.json': {

+ 8
- 13
package-lock.json View File

@@ -1,6 +1,6 @@
1 1
 {
2 2
   "name": "prettier-default-config",
3
-  "version": "0.2.1",
3
+  "version": "0.2.2",
4 4
   "lockfileVersion": 1,
5 5
   "requires": true,
6 6
   "dependencies": {
@@ -8,11 +8,15 @@
8 8
       "version": "7.8.4",
9 9
       "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.8.4.tgz",
10 10
       "integrity": "sha512-neAp3zt80trRVBI1x0azq6c57aNBqYZH8KhMm3TaB7wEI5Q4A2SHfBHE8w9gOhI/lrqxtEbXZgQIrHP+wvSGwQ==",
11
-      "dev": true,
12 11
       "requires": {
13 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 20
     "balanced-match": {
17 21
       "version": "1.0.0",
18 22
       "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz",
@@ -346,8 +350,7 @@
346 350
     "prettier": {
347 351
       "version": "1.19.1",
348 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 355
     "readable-stream": {
353 356
       "version": "1.1.14",
@@ -364,8 +367,7 @@
364 367
     "regenerator-runtime": {
365 368
       "version": "0.13.3",
366 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 372
     "regexp.prototype.flags": {
371 373
       "version": "1.3.0",
@@ -487,12 +489,6 @@
487 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 492
     "wrappy": {
497 493
       "version": "1.0.2",
498 494
       "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
@@ -520,7 +516,6 @@
520 516
       "version": "1.7.2",
521 517
       "resolved": "https://registry.npmjs.org/yaml/-/yaml-1.7.2.tgz",
522 518
       "integrity": "sha512-qXROVp90sb83XtAoqE8bP9RwAkTTZbugRUTm5YeFCBfNRPEp2YzTeqWiz7m5OORHzEvrA/qcGS8hp/E+MMROYw==",
523
-      "dev": true,
524 519
       "requires": {
525 520
         "@babel/runtime": "^7.6.3"
526 521
       }

+ 5
- 5
package.json View File

@@ -1,6 +1,6 @@
1 1
 {
2 2
   "name": "prettier-default-config",
3
-  "version": "0.2.1",
3
+  "version": "0.2.2",
4 4
   "description": "Generate a prettier config file with all the possible options and their default values",
5 5
   "bin": "index.js",
6 6
   "scripts": {
@@ -17,13 +17,13 @@
17 17
     "index.js"
18 18
   ],
19 19
   "dependencies": {
20
+    "@iarna/toml": "^2.2.3",
20 21
     "minimist": "^1.2.0",
21
-    "prettier": "^1.19.1"
22
+    "prettier": "^1.19.1",
23
+    "yaml": "^1.7.2"
22 24
   },
23 25
   "devDependencies": {
24 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,6 +1,6 @@
1 1
 const fs = require('fs');
2 2
 const test = require('tape');
3
-const TOML = require('toml');
3
+const TOML = require('@iarna/toml');
4 4
 const YAML = require('yaml');
5 5
 
6 6
 const { defaultConfigFor, formats } = require('.');
@@ -43,20 +43,28 @@ test('YAML supports infinity, leave rangeEnd in', t => {
43 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 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 56
   t.false(Object.keys(result).includes('parser'));
50 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 68
   t.plan(4);
61 69
   const result = eval(formats.js.generate());
62 70
   t.equal(result.parser, undefined);

Loading…
Cancel
Save