@putout/printer 12.32.1 → 13.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/ChangeLog CHANGED
@@ -1,3 +1,18 @@
1
+ 2025.02.22, v13.0.1
2
+
3
+ feature:
4
+ - ee27918 @putout/printer: @putout/eslint v4.1.0
5
+ - 7384b14 @putout/printer: eslint-plugin-putout v25.0.1
6
+ - fc42fa2 @putout/printer: ObjectExpression inside ArrayExpression
7
+
8
+ 2025.02.17, v13.0.0
9
+
10
+ fix:
11
+ - 64bec90 @putout/printer: "\\n'" will be escaped to '\\n\'' instead of '\\n''
12
+
13
+ feature:
14
+ - 8fd3042 @putout/printer: add ability to set escapeSingleQuotes/escapeDoubleQuotes according to format (coderaiser/putout#230)
15
+
1
16
  2025.02.15, v12.32.1
2
17
 
3
18
  fix:
package/README.md CHANGED
@@ -102,8 +102,8 @@ print(ast, {
102
102
  maxPropertiesInOneLine: 2,
103
103
  maxPropertiesLengthInOneLine: 15,
104
104
  trailingComma: true,
105
- encodeSingleQuote: true,
106
- encodeDoubleQuote: false,
105
+ escapeSingleQuote: true,
106
+ escapeDoubleQuote: false,
107
107
  roundBraces: {
108
108
  arrow: true,
109
109
  sequence: true,
@@ -34,6 +34,7 @@ const {
34
34
  isIdentifier,
35
35
  isFunction,
36
36
  isCallExpression,
37
+ isObjectProperty,
37
38
  } = types;
38
39
 
39
40
  const isNextString = (path) => isStringLiteral(path.getNextSibling());
@@ -133,7 +134,9 @@ module.exports.ArrayExpression = {
133
134
  if (index < n || trailingComma)
134
135
  maybe.print(is, ',');
135
136
 
136
- maybe.print.newline((is || isSpreadBeforeObject(element)) && !isNextObject(element));
137
+ if (!(isObjectExpression(element) && isObjectProperty(path.parentPath)))
138
+ maybe.print.newline((is || isSpreadBeforeObject(element)) && !isNextObject(element));
139
+
137
140
  maybe.print.space(is && isObjectAfterSimple(element));
138
141
 
139
142
  if (!is && index < n) {
@@ -37,6 +37,7 @@ const isLast = (path) => isParentProgram(path) && !isNext(path);
37
37
  module.exports.isNextObject = (a) => a
38
38
  .getNextSibling()
39
39
  .isObjectExpression();
40
+
40
41
  module.exports.isPrevObject = (a) => a
41
42
  .getPrevSibling()
42
43
  .isObjectExpression();
@@ -1,15 +1,5 @@
1
1
  'use strict';
2
2
 
3
- const maybeEncode = (value, {encodeDoubleQuote, encodeSingleQuote}) => {
4
- if (encodeSingleQuote && !value.includes('\\'))
5
- return value.replaceAll(`'`, `\\'`);
6
-
7
- if (encodeDoubleQuote && !value.includes('\\"'))
8
- return value.replaceAll(`"`, '\\"');
9
-
10
- return value;
11
- };
12
-
13
3
  module.exports.StringLiteral = (path, {write}, semantics) => {
14
4
  const {raw, value} = path.node;
15
5
 
@@ -21,7 +11,7 @@ module.exports.StringLiteral = (path, {write}, semantics) => {
21
11
  if (raw) {
22
12
  const value = raw.slice(1, -1);
23
13
  write.quote();
24
- write(maybeEncode(value, semantics));
14
+ write(maybeEscape(value, semantics));
25
15
  write.quote();
26
16
 
27
17
  return;
@@ -31,3 +21,39 @@ module.exports.StringLiteral = (path, {write}, semantics) => {
31
21
  write(value);
32
22
  write.quote();
33
23
  };
24
+
25
+ const maybeEscape = (value, {escapeDoubleQuote, escapeSingleQuote}) => {
26
+ const list = value.split('');
27
+ const slash = '\\';
28
+
29
+ if (escapeSingleQuote)
30
+ return escape(list, {
31
+ slash,
32
+ quote: `'`,
33
+ });
34
+
35
+ if (escapeDoubleQuote)
36
+ return escape(list, {
37
+ slash,
38
+ quote: `"`,
39
+ });
40
+
41
+ return value;
42
+ };
43
+
44
+ const escape = (list, {slash, quote}) => {
45
+ const res = [];
46
+
47
+ for (const [index, char] of list.entries()) {
48
+ const prev = list[index - 1];
49
+
50
+ if (char === quote && prev !== slash) {
51
+ res.push(`${slash}${char}`);
52
+ continue;
53
+ }
54
+
55
+ res.push(char);
56
+ }
57
+
58
+ return res.join('');
59
+ };
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const {parseRoundBraces} = require('./parse-round-braces');
4
+ const {parseQuotes} = require('./parse-quotes');
4
5
 
5
6
  module.exports.parseOverrides = (overrides = {}) => {
6
7
  const {
@@ -9,9 +10,11 @@ module.exports.parseOverrides = (overrides = {}) => {
9
10
  visitors,
10
11
  } = overrides;
11
12
 
13
+ const initiatedFormat = initFormat(format);
14
+
12
15
  return {
13
- format: initFormat(format),
14
- semantics: initSemantics(semantics),
16
+ format: initiatedFormat,
17
+ semantics: initSemantics(initiatedFormat, semantics),
15
18
  visitors,
16
19
  };
17
20
  };
@@ -26,7 +29,7 @@ const initFormat = (format) => ({
26
29
  ...format,
27
30
  });
28
31
 
29
- const initSemantics = (semantics = {}) => ({
32
+ const initSemantics = (format, semantics = {}) => ({
30
33
  comments: true,
31
34
  maxPropertiesInOneLine: 2,
32
35
  maxPropertiesLengthInOneLine: 15,
@@ -36,8 +39,7 @@ const initSemantics = (semantics = {}) => ({
36
39
  maxVariablesInOneLine: 4,
37
40
  maxTypesInOneLine: 3,
38
41
  trailingComma: true,
39
- encodeSingleQuote: true,
40
- encodeDoubleQuote: false,
42
+ ...parseQuotes(format),
41
43
  ...semantics,
42
44
  roundBraces: parseRoundBraces(semantics),
43
45
  });
@@ -0,0 +1,14 @@
1
+ 'use strict';
2
+
3
+ module.exports.parseQuotes = ({quote}) => {
4
+ if (quote === '"')
5
+ return {
6
+ escapeSingleQuote: false,
7
+ escapeDoubleQuote: true,
8
+ };
9
+
10
+ return {
11
+ escapeSingleQuote: true,
12
+ escapeDoubleQuote: false,
13
+ };
14
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "12.32.1",
3
+ "version": "13.0.1",
4
4
  "type": "commonjs",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "Simplest possible opinionated Babel AST printer for 🐊Putout",
@@ -65,7 +65,7 @@
65
65
  }
66
66
  },
67
67
  "devDependencies": {
68
- "@putout/eslint": "^3.5.0",
68
+ "@putout/eslint": "^4.1.0",
69
69
  "@putout/eslint-flat": "^2.0.0",
70
70
  "@putout/plugin-minify": "^9.0.0",
71
71
  "@putout/plugin-printer": "^4.0.0",
@@ -77,7 +77,7 @@
77
77
  "check-dts": "^0.8.0",
78
78
  "escover": "^4.0.1",
79
79
  "eslint": "^9.0.0",
80
- "eslint-plugin-putout": "^24.0.0",
80
+ "eslint-plugin-putout": "^25.0.1",
81
81
  "estree-to-babel": "^10.0.0",
82
82
  "goldstein": "^5.14.0",
83
83
  "just-kebab-case": "^4.2.0",