@putout/printer 12.32.0 → 13.0.0

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,16 @@
1
+ 2025.02.17, v13.0.0
2
+
3
+ fix:
4
+ - 64bec90 @putout/printer: "\\n'" will be escaped to '\\n\'' instead of '\\n''
5
+
6
+ feature:
7
+ - 8fd3042 @putout/printer: add ability to set escapeSingleQuotes/escapeDoubleQuotes according to format (coderaiser/putout#230)
8
+
9
+ 2025.02.15, v12.32.1
10
+
11
+ fix:
12
+ - 1ae65ad @putout/printer: DEBUG -> LOG_DEBUG
13
+
1
14
  2025.02.14, v12.32.0
2
15
 
3
16
  feature:
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,
@@ -11,11 +11,11 @@ const {
11
11
  LOG_ALL,
12
12
  LOG_TOKENS,
13
13
  LOG_TERM,
14
- DEBUG,
14
+ LOG_DEBUG,
15
15
  } = process.env;
16
16
 
17
17
  module.exports.createDebug = (tokens) => (a) => {
18
- if (!DEBUG)
18
+ if (!LOG_DEBUG)
19
19
  return;
20
20
 
21
21
  tokens.push({
@@ -200,4 +200,3 @@ function isSimpleAfterObject(path) {
200
200
 
201
201
  return prev.isObjectExpression();
202
202
  }
203
-
@@ -25,4 +25,3 @@ module.exports.isNextSimple = (a) => {
25
25
 
26
26
  return SIMPLE_TYPES.includes(type);
27
27
  };
28
-
@@ -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,40 @@ 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
+ };
60
+
@@ -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
+ };
@@ -124,7 +124,7 @@ function isNotLastBody(path) {
124
124
  }
125
125
 
126
126
  function isNotLastOrParentLast(path) {
127
- return !(isLast(path) || isParentLast(path));
127
+ return !isLast(path) && !isParentLast(path);
128
128
  }
129
129
 
130
130
  function isNextUp(path) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "12.32.0",
3
+ "version": "13.0.0",
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",
@@ -66,6 +66,7 @@
66
66
  },
67
67
  "devDependencies": {
68
68
  "@putout/eslint": "^3.5.0",
69
+ "@putout/eslint-flat": "^2.0.0",
69
70
  "@putout/plugin-minify": "^9.0.0",
70
71
  "@putout/plugin-printer": "^4.0.0",
71
72
  "@putout/plugin-promises": "^16.0.0",
package/tsconfig.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es2015",
4
+ "skipLibCheck": true
5
+ }
6
+ }