@putout/printer 6.2.0 → 6.4.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,14 @@
1
+ 2023.11.15, v6.4.0
2
+
3
+ feature:
4
+ - a5a01f0 @putout/printer: use @putout/operator-json instead of processors
5
+
6
+ 2023.11.12, v6.3.0
7
+
8
+ feature:
9
+ - d4795dc @putout/printer: putout v33.1.1
10
+ - 68439dd @putout/printer: StringLiteral: encode single quote (coderaiser/putout#192)
11
+
1
12
  2023.11.09, v6.2.0
2
13
 
3
14
  feature:
package/README.md CHANGED
@@ -98,6 +98,7 @@ print(ast, {
98
98
  maxVariablesInOneLine: 4,
99
99
  maxPropertiesInOneLine: 2,
100
100
  trailingComma: true,
101
+ encodeSingleQuote: true,
101
102
  },
102
103
  visitors: {
103
104
  AssignmentPattern(path, {print}) {
package/lib/json.js CHANGED
@@ -5,11 +5,10 @@ const {
5
5
  isIdentifier,
6
6
  } = require('@putout/babel').types;
7
7
 
8
- const {isJSON} = require('@putout/processor-json/is-json');
9
- const {isFilesystem} = require('@putout/processor-filesystem/is-filesystem');
8
+ const {isJSON} = require('@putout/operator-json');
10
9
 
11
10
  module.exports.maybeJSON = (ast, overrides) => {
12
- if (isASTJSON(ast)) {
11
+ if (isASTJSON(ast))
13
12
  return {
14
13
  ...overrides,
15
14
  format: {
@@ -19,9 +18,9 @@ module.exports.maybeJSON = (ast, overrides) => {
19
18
  semantics: {
20
19
  ...overrides?.semantics,
21
20
  trailingComma: false,
21
+ encodeSingleQuote: false,
22
22
  },
23
23
  };
24
- }
25
24
 
26
25
  return overrides;
27
26
  };
@@ -47,11 +46,5 @@ function isASTJSON(ast) {
47
46
  if (!isIdentifier(callee))
48
47
  return false;
49
48
 
50
- const {name} = callee;
51
-
52
- if (isJSON(name))
53
- return true;
54
-
55
- if (isFilesystem(name))
56
- return true;
49
+ return isJSON(callee.name);
57
50
  }
@@ -56,9 +56,8 @@ module.exports.createLog = ({newline = '\n', store = createStore()} = {}) => ({t
56
56
  store(value);
57
57
  }
58
58
 
59
- if (LOG_TERM) {
59
+ if (LOG_TERM)
60
60
  process.stdout.write(value);
61
- }
62
61
  };
63
62
 
64
63
  function createStore() {
@@ -65,9 +65,8 @@ module.exports.ArrayExpression = {
65
65
 
66
66
  const indented = !isTwoLongStrings(elements) || !isInsideArray(path) && isIndented(elements[0]);
67
67
 
68
- if (indented) {
68
+ if (indented)
69
69
  maybe.indent.inc(shouldIncreaseIndent);
70
- }
71
70
 
72
71
  const isNewLine = isNewlineBetweenElements(path, {
73
72
  elements,
@@ -47,7 +47,6 @@ module.exports.concatanate = (path, {print, indent}) => {
47
47
  print.breakline();
48
48
  print('__right');
49
49
 
50
- if (!path.parentPath.isBinaryExpression()) {
50
+ if (!path.parentPath.isBinaryExpression())
51
51
  indent.dec();
52
- }
53
52
  };
@@ -36,9 +36,8 @@ function CallExpression(path, {indent, print, maybe, traverse}) {
36
36
  for (const [i, arg] of args.entries()) {
37
37
  const isObject = arg.isObjectExpression();
38
38
 
39
- if (isParentCall && !isObject && n) {
39
+ if (isParentCall && !isObject && n)
40
40
  print.breakline();
41
- }
42
41
 
43
42
  print(arg);
44
43
 
@@ -53,9 +53,8 @@ const classVisitor = maybeDecorators((path, printer, semantics) => {
53
53
  traverse(item);
54
54
  }
55
55
 
56
- if (!body.length) {
56
+ if (!body.length)
57
57
  parseComments(classBody, printer, semantics);
58
- }
59
58
 
60
59
  indent.dec();
61
60
  maybe.indent(body.length);
@@ -64,12 +64,11 @@ function build(path) {
64
64
  type: path.type,
65
65
  };
66
66
 
67
- if (path.isCallExpression()) {
67
+ if (path.isCallExpression())
68
68
  assign(prop, {
69
69
  args: path.node.arguments.length,
70
70
  name: path.node.callee.property?.name || '',
71
71
  });
72
- }
73
72
 
74
73
  return prop;
75
74
  }
@@ -33,9 +33,8 @@ module.exports.MemberExpression = (path, {print, maybe, traverse}) => {
33
33
 
34
34
  maybe.indent.inc(isChain);
35
35
 
36
- if (isChain) {
36
+ if (isChain)
37
37
  print.breakline();
38
- }
39
38
 
40
39
  print('.');
41
40
  print('__property');
@@ -1,9 +1,15 @@
1
1
  'use strict';
2
2
 
3
3
  const {TemplateLiteral} = require('./template-literal');
4
-
5
4
  const {Identifier} = require('./identifier');
6
5
 
6
+ const maybeEncode = (value, {encodeSingleQuote}) => {
7
+ if (encodeSingleQuote && !value.includes('\\'))
8
+ return value.replaceAll(`'`, `\\'`);
9
+
10
+ return value;
11
+ };
12
+
7
13
  module.exports = {
8
14
  Identifier,
9
15
  TemplateLiteral,
@@ -36,7 +42,7 @@ module.exports = {
36
42
  BooleanLiteral(path, {write}) {
37
43
  write(path.node.value);
38
44
  },
39
- StringLiteral(path, {write}) {
45
+ StringLiteral(path, {write}, semantics) {
40
46
  const {raw, value} = path.node;
41
47
 
42
48
  if (raw && path.parentPath.isJSXAttribute()) {
@@ -47,7 +53,7 @@ module.exports = {
47
53
  if (raw) {
48
54
  const value = raw.slice(1, -1);
49
55
  write.quote();
50
- write(value);
56
+ write(maybeEncode(value, semantics));
51
57
  write.quote();
52
58
 
53
59
  return;
@@ -50,13 +50,11 @@ function objectPlugin(plugin, path, printer, options) {
50
50
  afterIf = condition,
51
51
  } = maybeSatisfy(plugin);
52
52
 
53
- if (beforeIf?.(path, printer)) {
53
+ if (beforeIf?.(path, printer))
54
54
  before(path, printer);
55
- }
56
55
 
57
56
  print(path, printer, options);
58
57
 
59
- if (afterIf?.(path, printer)) {
58
+ if (afterIf?.(path, printer))
60
59
  after(path, printer);
61
- }
62
60
  }
@@ -18,13 +18,12 @@ module.exports.maybeDecorators = (visitor) => (path, printer, semantics, options
18
18
 
19
19
  const {decorators} = path.node;
20
20
 
21
- if (decorators) {
21
+ if (decorators)
22
22
  for (const decorator of path.get('decorators')) {
23
23
  maybe.write.breakline(isPrevClassProperty(path));
24
24
  traverse(decorator);
25
25
  write.breakline();
26
26
  }
27
- }
28
27
 
29
28
  visitor(path, printer, semantics, options);
30
29
  };
@@ -36,6 +36,7 @@ function initSemantics(semantics = {}) {
36
36
  maxElementsInOneLine: 5,
37
37
  maxVariablesInOneLine: 4,
38
38
  trailingComma: true,
39
+ encodeSingleQuote: true,
39
40
  ...semantics,
40
41
  };
41
42
  }
@@ -65,9 +65,8 @@ module.exports.BlockStatement = {
65
65
  maybe.indent(body.length);
66
66
  write('}');
67
67
 
68
- if (path.parentPath.isObjectMethod()) {
68
+ if (path.parentPath.isObjectMethod())
69
69
  write(',');
70
- }
71
70
  },
72
71
  afterIf(path) {
73
72
  return shouldAddNewlineAfter(path);
@@ -75,9 +75,8 @@ module.exports.ExportNamedDeclaration = {
75
75
  maybe.write(isType, 'type ');
76
76
  traverse(spec);
77
77
 
78
- if (i < lastIndex && !isNewline) {
78
+ if (i < lastIndex && !isNewline)
79
79
  write(', ');
80
- }
81
80
 
82
81
  maybe.write(isNewline, ',');
83
82
  maybe.write.newline(isNewline);
@@ -61,13 +61,11 @@ module.exports.ExpressionStatement = {
61
61
  return isBeforeElse(path);
62
62
  },
63
63
  after(path, {print, maybe, store}) {
64
- if (hasTrailingComment(path) && isLast(path) && isCoupleLines(path)) {
64
+ if (hasTrailingComment(path) && isLast(path) && isCoupleLines(path))
65
65
  print.breakline();
66
- }
67
66
 
68
- if (hasTrailingComment(path) && !isCoupleLines(path)) {
67
+ if (hasTrailingComment(path) && !isCoupleLines(path))
69
68
  return;
70
- }
71
69
 
72
70
  print.newline();
73
71
  maybe.markAfter(store(), path);
@@ -38,9 +38,8 @@ module.exports.IfStatement = {
38
38
  print.space();
39
39
  print(consequent);
40
40
 
41
- if (isInsideIf(path) || isInsideNestedBody(path)) {
41
+ if (isInsideIf(path) || isInsideNestedBody(path))
42
42
  maybe.print.newline(isEmptyBody(consequent));
43
- }
44
43
  } else {
45
44
  const is = !isEmptyConsequent(path);
46
45
  maybe.print.newline(is);
@@ -268,9 +268,8 @@ const createPrint = (path, {traverse, write}) => (maybeLine) => {
268
268
  };
269
269
 
270
270
  const computePath = (path, maybeLine) => {
271
- if (isString(maybeLine) && maybeLine.startsWith(GET)) {
271
+ if (isString(maybeLine) && maybeLine.startsWith(GET))
272
272
  return get(path, maybeLine);
273
- }
274
273
 
275
274
  if (isObject(maybeLine))
276
275
  return maybeLine;
@@ -17,11 +17,10 @@ module.exports.TSTypeParameter = (path, {write, traverse}) => {
17
17
  if (!exists(constraint))
18
18
  return;
19
19
 
20
- if (constraint.isTSTypeOperator() || path.parentPath.isTSMappedType()) {
20
+ if (constraint.isTSTypeOperator() || path.parentPath.isTSMappedType())
21
21
  write(' in ');
22
- } else {
22
+ else
23
23
  write(' extends ');
24
- }
25
24
 
26
25
  traverse(constraint);
27
26
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "6.2.0",
3
+ "version": "6.4.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",
@@ -30,8 +30,7 @@
30
30
  "@putout/babel": "^1.1.1",
31
31
  "@putout/compare": "^13.0.0",
32
32
  "@putout/operate": "^11.0.0",
33
- "@putout/processor-filesystem": "^1.1.0",
34
- "@putout/processor-json": "^7.0.0",
33
+ "@putout/operator-json": "^1.2.0",
35
34
  "fullstore": "^3.0.0",
36
35
  "just-snake-case": "^3.2.0",
37
36
  "parse-import-specifiers": "^1.0.1",
@@ -66,7 +65,7 @@
66
65
  "mock-require": "^3.0.3",
67
66
  "montag": "^1.0.0",
68
67
  "nodemon": "^3.0.1",
69
- "putout": "^32.0.4",
68
+ "putout": "^33.1.1",
70
69
  "supertape": "^8.0.0",
71
70
  "try-catch": "^3.0.0"
72
71
  },