Browse Source

Read config options from Prettier rather than hardcoding

tags/v0.2.0
Dylan Baker 4 years ago
parent
commit
4a2a367657
5 changed files with 84 additions and 57 deletions
  1. 0
    21
      .prettierrc
  2. 18
    2
      README.md
  3. 30
    26
      index.js
  4. 2
    2
      package.json
  5. 34
    6
      test.js

+ 0
- 21
.prettierrc View File

@@ -1,21 +0,0 @@
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
-}

+ 18
- 2
README.md View File

@@ -1,6 +1,6 @@
1 1
 # prettier-default-config
2 2
 
3
-This is a small CLI tool that dumps all of prettier's default configuration
3
+This is a small CLI tool that dumps all of Prettier's default configuration
4 4
 options to a file in the format of your choice.
5 5
 
6 6
 ## Installation
@@ -25,6 +25,22 @@ OPTIONS:
25 25
     --help            Prints help information
26 26
 ```
27 27
 
28
+## Exclusions
29
+
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:
36
+
37
+
38
+| Format | Excluded Options                  |
39
+|--------|-----------------------------------|
40
+| JSON   | `rangeEnd` , `filepath`, `parser` |
41
+| TOML   | `rangeEnd`, `filepath`, `parser`  |
42
+| YAML   | `rangeEnd`                        |
43
+
28 44
 ## License
29 45
 
30
-MIT
46
+MIT

+ 30
- 26
index.js View File

@@ -3,42 +3,42 @@
3 3
 const fs = require('fs');
4 4
 const minimist = require('minimist');
5 5
 const path = require('path');
6
+const prettier = require('prettier');
6 7
 
7
-const defaultConfig = {
8
-  arrowParens: 'avoid',
9
-  bracketSpacing: true,
10
-  endOfLine: 'auto',
11
-  filepath: '',
12
-  htmlWhitespaceSensitivity: 'css',
13
-  insertPragma: false,
14
-  jsxBracketSameLine: false,
15
-  jsxSingleQuote: false,
16
-  parser: '',
17
-  printWidth: 80,
18
-  proseWrap: 'preserve',
19
-  quoteProps: 'as-needed',
20
-  requirePragma: false,
21
-  semi: true,
22
-  singleQuote: false,
23
-  tabWidth: 2,
24
-  trailingComma: 'none',
25
-  useTabs: false,
26
-  vueIndentScriptAndStyle: false,
8
+const defaultConfigFor = function(format) {
9
+  let config = prettier.getSupportInfo().options;
10
+
11
+  if (!['js', 'yaml'].includes(format)) {
12
+    config = config.filter(
13
+      option => ![null, Infinity].includes(option.default)
14
+    );
15
+  }
16
+
17
+  if (format != 'js') {
18
+    config = config.filter(option => option.default !== undefined);
19
+  }
20
+
21
+  return Object.fromEntries(
22
+    config.map(option => [option.name, option.default])
23
+  );
27 24
 };
28 25
 
29 26
 const formats = {
30 27
   json: {
31 28
     filename: '.prettierrc',
32 29
     generate: function() {
33
-      return JSON.stringify(defaultConfig, null, 2);
30
+      return JSON.stringify(defaultConfigFor('json'), null, 2);
34 31
     },
35 32
   },
36 33
   yaml: {
37 34
     filename: '.prettierrc.yaml',
38 35
     generate: function() {
39
-      return Object.entries(defaultConfig)
36
+      return Object.entries(defaultConfigFor('yaml'))
40 37
         .map(([key, value]) => {
41 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 42
           return `${key}: ${value}`;
43 43
         })
44 44
         .join('\n');
@@ -47,9 +47,10 @@ const formats = {
47 47
   toml: {
48 48
     filename: '.prettierrc.toml',
49 49
     generate: function() {
50
-      return Object.entries(defaultConfig)
50
+      return Object.entries(defaultConfigFor('toml'))
51 51
         .map(([key, value]) => {
52 52
           if (typeof value === 'string') return `${key} = "${value}"`;
53
+          if (!!value.forEach && value.length === 0) return `${key} = []`;
53 54
           return `${key} = ${value}`;
54 55
         })
55 56
         .join('\n');
@@ -58,9 +59,12 @@ const formats = {
58 59
   js: {
59 60
     filename: '.prettierrc.js',
60 61
     generate: function() {
61
-      const contents = Object.entries(defaultConfig)
62
+      const contents = Object.entries(defaultConfigFor('js'))
62 63
         .map(([key, value]) => {
63 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}: [],`;
64 68
           return `  ${key}: ${value},`;
65 69
         })
66 70
         .join('\n');
@@ -78,7 +82,7 @@ const formats = {
78 82
         process.exit(0);
79 83
       }
80 84
       const data = JSON.parse(fs.readFileSync(file));
81
-      data.prettier = defaultConfig;
85
+      data.prettier = defaultConfigFor('json');
82 86
       return JSON.stringify(data, null, 2);
83 87
     },
84 88
   },
@@ -137,7 +141,7 @@ function run() {
137 141
 }
138 142
 
139 143
 module.exports = {
140
-  defaultConfig,
144
+  defaultConfigFor,
141 145
   formats,
142 146
 };
143 147
 

+ 2
- 2
package.json View File

@@ -13,11 +13,11 @@
13 13
     "index.js"
14 14
   ],
15 15
   "dependencies": {
16
-    "minimist": "^1.2.0"
16
+    "minimist": "^1.2.0",
17
+    "prettier": "^1.19.1"
17 18
   },
18 19
   "devDependencies": {
19 20
     "faucet": "0.0.1",
20
-    "prettier": "^1.19.1",
21 21
     "tape": "^4.13.0",
22 22
     "toml": "^3.0.0",
23 23
     "yaml": "^1.7.2"

+ 34
- 6
test.js View File

@@ -3,36 +3,64 @@ const test = require('tape');
3 3
 const TOML = require('toml');
4 4
 const YAML = require('yaml');
5 5
 
6
-const { defaultConfig, formats } = require('.');
6
+const { defaultConfigFor, formats } = require('.');
7 7
 
8 8
 test('generate JSON', t => {
9 9
   t.plan(1);
10 10
   const result = formats.json.generate();
11
-  t.deepEqual(JSON.parse(result), defaultConfig);
11
+  t.deepEqual(JSON.parse(result), defaultConfigFor('json'));
12 12
 });
13 13
 
14 14
 test('generate TOML', t => {
15 15
   t.plan(1);
16 16
   const result = formats.toml.generate();
17
-  t.deepEqual(TOML.parse(result), defaultConfig);
17
+  t.deepEqual(TOML.parse(result), defaultConfigFor('toml'));
18 18
 });
19 19
 
20 20
 test('generate YAML', t => {
21 21
   t.plan(1);
22 22
   const result = formats.yaml.generate();
23
-  t.deepEqual(YAML.parse(result), defaultConfig);
23
+  t.deepEqual(YAML.parse(result), defaultConfigFor('yaml'));
24 24
 });
25 25
 
26 26
 test('generate JS', t => {
27 27
   t.plan(1);
28 28
   const result = formats.js.generate();
29
-  t.deepEqual(eval(result), defaultConfig);
29
+  t.deepEqual(eval(result), defaultConfigFor('js'));
30 30
 });
31 31
 
32 32
 test('generate package.json', t => {
33 33
   t.plan(1);
34 34
   const before = JSON.parse(fs.readFileSync('package.json'));
35 35
   const result = formats['package.json'].generate();
36
-  before.prettier = defaultConfig;
36
+  before.prettier = defaultConfigFor('package.json');
37 37
   t.deepEqual(JSON.parse(result), before);
38 38
 });
39
+
40
+test('YAML supports infinity, leave rangeEnd in', t => {
41
+  t.plan(1);
42
+  const result = formats.yaml.generate();
43
+  t.equal(YAML.parse(result).rangeEnd, Infinity);
44
+});
45
+
46
+test("YAML doesn't support undefined so leave out filepath and parser", t => {
47
+  t.plan(2);
48
+  const result = formats.yaml.generate();
49
+  t.false(Object.keys(result).includes('parser'));
50
+  t.false(Object.keys(result).includes('filepath'));
51
+});
52
+
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'));
57
+});
58
+
59
+test('JS supports undefined, so leave in parser and filepath', t => {
60
+  t.plan(4);
61
+  const result = eval(formats.js.generate());
62
+  t.equal(result.parser, undefined);
63
+  t.equal(result.filepath, undefined);
64
+  t.true(Object.keys(result).includes('parser'));
65
+  t.true(Object.keys(result).includes('filepath'));
66
+});

Loading…
Cancel
Save