prettier-plugin-java 1.3.1 → 1.6.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.
Files changed (46) hide show
  1. package/dist/base-cst-printer.js +77 -0
  2. package/dist/cst-printer.js +37 -0
  3. package/dist/index.js +67 -0
  4. package/dist/options.js +256 -0
  5. package/dist/parser.js +7 -0
  6. package/dist/printer.js +28 -0
  7. package/dist/printers/arrays.js +49 -0
  8. package/dist/printers/blocks-and-statements.js +493 -0
  9. package/dist/printers/classes.js +724 -0
  10. package/dist/printers/comments/comments-utils.js +29 -0
  11. package/dist/printers/comments/format-comments.js +179 -0
  12. package/dist/printers/comments/handle-comments.js +38 -0
  13. package/dist/printers/expressions.js +549 -0
  14. package/dist/printers/interfaces.js +251 -0
  15. package/dist/printers/lexical-structure.js +43 -0
  16. package/dist/printers/names.js +53 -0
  17. package/dist/printers/packages-and-modules.js +185 -0
  18. package/dist/printers/prettier-builder.js +44 -0
  19. package/dist/printers/printer-utils.js +565 -0
  20. package/dist/printers/types-values-and-variables.js +183 -0
  21. package/dist/types/utils.js +29 -0
  22. package/dist/utils/constants.js +4 -0
  23. package/dist/utils/expressions-utils.js +29 -0
  24. package/dist/utils/index.js +10 -0
  25. package/dist/utils/printArgumentListWithBraces.js +21 -0
  26. package/dist/utils/printSingleLambdaInvocation.js +20 -0
  27. package/package.json +32 -10
  28. package/src/cst-printer.js +0 -145
  29. package/src/index.js +0 -79
  30. package/src/options.js +0 -256
  31. package/src/parser.js +0 -10
  32. package/src/printer.js +0 -31
  33. package/src/printers/arrays.js +0 -38
  34. package/src/printers/blocks-and-statements.js +0 -588
  35. package/src/printers/classes.js +0 -940
  36. package/src/printers/comments/comments-utils.js +0 -38
  37. package/src/printers/comments/format-comments.js +0 -223
  38. package/src/printers/comments/handle-comments.js +0 -50
  39. package/src/printers/expressions.js +0 -703
  40. package/src/printers/interfaces.js +0 -324
  41. package/src/printers/lexical-structure.js +0 -27
  42. package/src/printers/names.js +0 -42
  43. package/src/printers/packages-and-modules.js +0 -231
  44. package/src/printers/prettier-builder.js +0 -60
  45. package/src/printers/printer-utils.js +0 -715
  46. package/src/printers/types-values-and-variables.js +0 -202
@@ -0,0 +1,183 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ var __spreadArray = (this && this.__spreadArray) || function (to, from) {
18
+ for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
19
+ to[j] = from[i];
20
+ return to;
21
+ };
22
+ var __importDefault = (this && this.__importDefault) || function (mod) {
23
+ return (mod && mod.__esModule) ? mod : { "default": mod };
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.TypesValuesAndVariablesPrettierVisitor = void 0;
27
+ var forEach_1 = __importDefault(require("lodash/forEach"));
28
+ var prettier_builder_1 = require("./prettier-builder");
29
+ var format_comments_1 = require("./comments/format-comments");
30
+ var printer_utils_1 = require("./printer-utils");
31
+ var base_cst_printer_1 = require("../base-cst-printer");
32
+ var utils_1 = require("../types/utils");
33
+ var TypesValuesAndVariablesPrettierVisitor = /** @class */ (function (_super) {
34
+ __extends(TypesValuesAndVariablesPrettierVisitor, _super);
35
+ function TypesValuesAndVariablesPrettierVisitor() {
36
+ return _super !== null && _super.apply(this, arguments) || this;
37
+ }
38
+ TypesValuesAndVariablesPrettierVisitor.prototype.primitiveType = function (ctx) {
39
+ var annotations = this.mapVisit(ctx.annotation);
40
+ var type = ctx.numericType
41
+ ? this.visit(ctx.numericType)
42
+ : this.getSingle(ctx);
43
+ return printer_utils_1.rejectAndJoin(" ", [prettier_builder_1.join(" ", annotations), type]);
44
+ };
45
+ TypesValuesAndVariablesPrettierVisitor.prototype.numericType = function (ctx) {
46
+ return this.visitSingle(ctx);
47
+ };
48
+ TypesValuesAndVariablesPrettierVisitor.prototype.integralType = function (ctx) {
49
+ return format_comments_1.printTokenWithComments(this.getSingle(ctx));
50
+ };
51
+ TypesValuesAndVariablesPrettierVisitor.prototype.floatingPointType = function (ctx) {
52
+ return format_comments_1.printTokenWithComments(this.getSingle(ctx));
53
+ };
54
+ TypesValuesAndVariablesPrettierVisitor.prototype.referenceType = function (ctx) {
55
+ var annotations = this.mapVisit(ctx.annotation);
56
+ var type = ctx.primitiveType
57
+ ? this.visit(ctx.primitiveType)
58
+ : this.visit(ctx.classOrInterfaceType);
59
+ var dims = this.visit(ctx.dims);
60
+ return printer_utils_1.rejectAndJoin(" ", [prettier_builder_1.join(" ", annotations), prettier_builder_1.concat([type, dims])]);
61
+ };
62
+ TypesValuesAndVariablesPrettierVisitor.prototype.classOrInterfaceType = function (ctx) {
63
+ return this.visitSingle(ctx);
64
+ };
65
+ TypesValuesAndVariablesPrettierVisitor.prototype.classType = function (ctx) {
66
+ var _this = this;
67
+ var tokens = printer_utils_1.sortClassTypeChildren(ctx.annotation, ctx.typeArguments, ctx.Identifier);
68
+ var segments = [];
69
+ var currentSegment = [];
70
+ forEach_1.default(tokens, function (token, i) {
71
+ if (utils_1.isTypeArgumentsCstNode(token)) {
72
+ currentSegment.push(_this.visit([token]));
73
+ segments.push(printer_utils_1.rejectAndConcat(currentSegment));
74
+ currentSegment = [];
75
+ }
76
+ else if (utils_1.isAnnotationCstNode(token)) {
77
+ currentSegment.push(_this.visit([token]));
78
+ }
79
+ else {
80
+ currentSegment.push(token);
81
+ if ((i + 1 < tokens.length && !utils_1.isTypeArgumentsCstNode(tokens[i + 1])) ||
82
+ i + 1 === tokens.length) {
83
+ segments.push(printer_utils_1.rejectAndConcat(currentSegment));
84
+ currentSegment = [];
85
+ }
86
+ }
87
+ });
88
+ return printer_utils_1.rejectAndJoinSeps(ctx.Dot, segments);
89
+ };
90
+ TypesValuesAndVariablesPrettierVisitor.prototype.interfaceType = function (ctx) {
91
+ return this.visitSingle(ctx);
92
+ };
93
+ TypesValuesAndVariablesPrettierVisitor.prototype.typeVariable = function (ctx) {
94
+ var annotations = this.mapVisit(ctx.annotation);
95
+ var identifier = this.getSingle(ctx);
96
+ return printer_utils_1.rejectAndJoin(" ", [prettier_builder_1.join(" ", annotations), identifier]);
97
+ };
98
+ TypesValuesAndVariablesPrettierVisitor.prototype.dims = function (ctx) {
99
+ var _this = this;
100
+ var tokens = __spreadArray([], ctx.LSquare);
101
+ if (ctx.annotation) {
102
+ tokens = __spreadArray(__spreadArray([], tokens), ctx.annotation);
103
+ }
104
+ tokens = tokens.sort(function (a, b) {
105
+ var startOffset1 = utils_1.isCstNode(a)
106
+ ? a.children.At[0].startOffset
107
+ : a.startOffset;
108
+ var startOffset2 = utils_1.isCstNode(b)
109
+ ? b.children.At[0].startOffset
110
+ : b.startOffset;
111
+ return startOffset1 - startOffset2;
112
+ });
113
+ var segments = [];
114
+ var currentSegment = [];
115
+ forEach_1.default(tokens, function (token) {
116
+ if (utils_1.isCstNode(token)) {
117
+ currentSegment.push(_this.visit([token]));
118
+ }
119
+ else {
120
+ segments.push(printer_utils_1.rejectAndConcat([
121
+ printer_utils_1.rejectAndJoin(" ", currentSegment),
122
+ prettier_builder_1.concat([ctx.LSquare[0], ctx.RSquare[0]])
123
+ ]));
124
+ currentSegment = [];
125
+ }
126
+ });
127
+ return printer_utils_1.rejectAndConcat(segments);
128
+ };
129
+ TypesValuesAndVariablesPrettierVisitor.prototype.typeParameter = function (ctx) {
130
+ var typeParameterModifiers = this.mapVisit(ctx.typeParameterModifier);
131
+ var typeIdentifier = this.visit(ctx.typeIdentifier);
132
+ var typeBound = this.visit(ctx.typeBound);
133
+ return printer_utils_1.rejectAndJoin(" ", [
134
+ prettier_builder_1.join(" ", typeParameterModifiers),
135
+ typeIdentifier,
136
+ typeBound
137
+ ]);
138
+ };
139
+ TypesValuesAndVariablesPrettierVisitor.prototype.typeParameterModifier = function (ctx) {
140
+ return this.visitSingle(ctx);
141
+ };
142
+ TypesValuesAndVariablesPrettierVisitor.prototype.typeBound = function (ctx) {
143
+ var classOrInterfaceType = this.visit(ctx.classOrInterfaceType);
144
+ var additionalBound = this.mapVisit(ctx.additionalBound);
145
+ return printer_utils_1.rejectAndJoin(" ", [
146
+ ctx.Extends[0],
147
+ classOrInterfaceType,
148
+ prettier_builder_1.join(" ", additionalBound)
149
+ ]);
150
+ };
151
+ TypesValuesAndVariablesPrettierVisitor.prototype.additionalBound = function (ctx) {
152
+ var interfaceType = this.visit(ctx.interfaceType);
153
+ return prettier_builder_1.join(" ", [ctx.And[0], interfaceType]);
154
+ };
155
+ TypesValuesAndVariablesPrettierVisitor.prototype.typeArguments = function (ctx) {
156
+ var typeArgumentList = this.visit(ctx.typeArgumentList);
157
+ return printer_utils_1.rejectAndConcat([ctx.Less[0], typeArgumentList, ctx.Greater[0]]);
158
+ };
159
+ TypesValuesAndVariablesPrettierVisitor.prototype.typeArgumentList = function (ctx) {
160
+ var typeArguments = this.mapVisit(ctx.typeArgument);
161
+ var commas = ctx.Comma ? ctx.Comma.map(function (elt) { return prettier_builder_1.concat([elt, " "]); }) : [];
162
+ return printer_utils_1.rejectAndJoinSeps(commas, typeArguments);
163
+ };
164
+ TypesValuesAndVariablesPrettierVisitor.prototype.typeArgument = function (ctx) {
165
+ return this.visitSingle(ctx);
166
+ };
167
+ TypesValuesAndVariablesPrettierVisitor.prototype.wildcard = function (ctx) {
168
+ var annotations = this.mapVisit(ctx.annotation);
169
+ var wildcardBounds = this.visit(ctx.wildcardBounds);
170
+ return printer_utils_1.rejectAndJoin(" ", [
171
+ prettier_builder_1.join(" ", annotations),
172
+ ctx.QuestionMark[0],
173
+ wildcardBounds
174
+ ]);
175
+ };
176
+ TypesValuesAndVariablesPrettierVisitor.prototype.wildcardBounds = function (ctx) {
177
+ var keyWord = ctx.Extends ? ctx.Extends[0] : ctx.Super[0];
178
+ var referenceType = this.visit(ctx.referenceType);
179
+ return prettier_builder_1.join(" ", [keyWord, referenceType]);
180
+ };
181
+ return TypesValuesAndVariablesPrettierVisitor;
182
+ }(base_cst_printer_1.BaseCstPrettierPrinter));
183
+ exports.TypesValuesAndVariablesPrettierVisitor = TypesValuesAndVariablesPrettierVisitor;
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isOrdinaryCompilationUnitCtx = exports.isAnnotationCstNode = exports.isTypeArgumentsCstNode = exports.isCstElementOrUndefinedIToken = exports.isIToken = exports.isCstNode = void 0;
4
+ function isCstNode(tokenOrNode) {
5
+ return !isIToken(tokenOrNode);
6
+ }
7
+ exports.isCstNode = isCstNode;
8
+ function isIToken(tokenOrNode) {
9
+ return (tokenOrNode.tokenType !== undefined &&
10
+ tokenOrNode.image !== undefined);
11
+ }
12
+ exports.isIToken = isIToken;
13
+ function isCstElementOrUndefinedIToken(tokenOrNode) {
14
+ return tokenOrNode !== undefined && isIToken(tokenOrNode);
15
+ }
16
+ exports.isCstElementOrUndefinedIToken = isCstElementOrUndefinedIToken;
17
+ var isTypeArgumentsCstNode = function (cstElement) {
18
+ return cstElement.name === "typeArguments";
19
+ };
20
+ exports.isTypeArgumentsCstNode = isTypeArgumentsCstNode;
21
+ var isAnnotationCstNode = function (cstElement) {
22
+ return cstElement.name === "annotation";
23
+ };
24
+ exports.isAnnotationCstNode = isAnnotationCstNode;
25
+ var isOrdinaryCompilationUnitCtx = function (ctx) {
26
+ return (ctx.ordinaryCompilationUnit !==
27
+ undefined);
28
+ };
29
+ exports.isOrdinaryCompilationUnitCtx = isOrdinaryCompilationUnitCtx;
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.whitespace = void 0;
4
+ exports.whitespace = " ";
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isSingleArgumentLambdaExpressionWithBlock = exports.isArgumentListSingleLambda = void 0;
4
+ function isArgumentListSingleLambda(argumentList) {
5
+ if (argumentList === undefined) {
6
+ return false;
7
+ }
8
+ var args = argumentList[0].children.expression;
9
+ if (args.length !== 1) {
10
+ return false;
11
+ }
12
+ var argument = args[0];
13
+ return argument.children.lambdaExpression !== undefined;
14
+ }
15
+ exports.isArgumentListSingleLambda = isArgumentListSingleLambda;
16
+ var isSingleArgumentLambdaExpressionWithBlock = function (argumentList) {
17
+ if (argumentList === undefined) {
18
+ return false;
19
+ }
20
+ var args = argumentList[0].children.expression;
21
+ if (args.length !== 1) {
22
+ return false;
23
+ }
24
+ var argument = args[0];
25
+ return (argument.children.lambdaExpression !== undefined &&
26
+ argument.children.lambdaExpression[0].children.lambdaBody[0].children
27
+ .block !== undefined);
28
+ };
29
+ exports.isSingleArgumentLambdaExpressionWithBlock = isSingleArgumentLambdaExpressionWithBlock;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.printSingleLambdaInvocation = exports.printArgumentListWithBraces = void 0;
7
+ var printArgumentListWithBraces_1 = require("./printArgumentListWithBraces");
8
+ Object.defineProperty(exports, "printArgumentListWithBraces", { enumerable: true, get: function () { return __importDefault(printArgumentListWithBraces_1).default; } });
9
+ var printSingleLambdaInvocation_1 = require("./printSingleLambdaInvocation");
10
+ Object.defineProperty(exports, "printSingleLambdaInvocation", { enumerable: true, get: function () { return __importDefault(printSingleLambdaInvocation_1).default; } });
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ var doc_1 = require("prettier/doc");
7
+ var expressions_utils_1 = require("./expressions-utils");
8
+ var printer_utils_1 = require("../printers/printer-utils");
9
+ var printSingleLambdaInvocation_1 = __importDefault(require("./printSingleLambdaInvocation"));
10
+ var softline = doc_1.builders.softline;
11
+ function printArgumentListWithBraces(argumentListCtx, rBrace, lBrace) {
12
+ var isSingleLambda = expressions_utils_1.isArgumentListSingleLambda(argumentListCtx);
13
+ if (isSingleLambda) {
14
+ return printSingleLambdaInvocation_1.default.call(this, argumentListCtx, rBrace, lBrace);
15
+ }
16
+ var argumentList = this.visit(argumentListCtx, {
17
+ isInsideMethodInvocationSuffix: true
18
+ });
19
+ return printer_utils_1.putIntoBraces(argumentList, softline, lBrace, rBrace);
20
+ }
21
+ exports.default = printArgumentListWithBraces;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var doc_1 = require("prettier/doc");
4
+ var expressions_utils_1 = require("./expressions-utils");
5
+ var format_comments_1 = require("../printers/comments/format-comments");
6
+ var prettier_builder_1 = require("../printers/prettier-builder");
7
+ var printer_utils_1 = require("../printers/printer-utils");
8
+ var softline = doc_1.builders.softline, ifBreak = doc_1.builders.ifBreak;
9
+ function printSingleLambdaInvocation(argumentListCtx, rBrace, lBrace) {
10
+ var lambdaParametersGroupId = Symbol("lambdaParameters");
11
+ var argumentList = this.visit(argumentListCtx, {
12
+ lambdaParametersGroupId: lambdaParametersGroupId,
13
+ isInsideMethodInvocationSuffix: true
14
+ });
15
+ var formattedRBrace = expressions_utils_1.isSingleArgumentLambdaExpressionWithBlock(argumentListCtx)
16
+ ? ifBreak(prettier_builder_1.indent(prettier_builder_1.concat([softline, rBrace])), format_comments_1.printTokenWithComments(rBrace), { groupId: lambdaParametersGroupId })
17
+ : prettier_builder_1.indent(prettier_builder_1.concat([softline, rBrace]));
18
+ return prettier_builder_1.dedent(printer_utils_1.putIntoBraces(argumentList, "", lBrace, formattedRBrace));
19
+ }
20
+ exports.default = printSingleLambdaInvocation;
package/package.json CHANGED
@@ -1,24 +1,46 @@
1
1
  {
2
2
  "name": "prettier-plugin-java",
3
- "version": "1.3.1",
3
+ "version": "1.6.1",
4
4
  "description": "Prettier Java Plugin",
5
- "main": "src/index.js",
5
+ "main": "dist/index.js",
6
+ "files": [
7
+ "dist"
8
+ ],
6
9
  "repository": "https://github.com/jhipster/prettier-java",
7
10
  "license": "Apache-2.0",
8
11
  "dependencies": {
9
- "java-parser": "1.3.1",
12
+ "java-parser": "2.0.1",
10
13
  "lodash": "4.17.21",
11
14
  "prettier": "2.3.1"
12
15
  },
13
16
  "scripts": {
14
17
  "test": "npm-run-all test:unit test:e2e-core",
15
- "test:unit": "mocha \"test/unit-test/**/*.spec.js\" \"test/unit-test/**/*-spec.js\"",
16
- "test:unit:coverage": "nyc mocha \"test/unit-test/**/*.spec.js\" \"test/unit-test/**/*-spec.js\"",
17
- "test:e2e-core": "node scripts/clone-samples e2e-core && mocha \"test/repository-test/core-test.js\"",
18
- "test:e2e-jhipster1": "node scripts/clone-samples e2e-jhipster1 && mocha \"test/repository-test/jhipster-1-test.js\"",
19
- "test:e2e-jhipster2": "node scripts/clone-samples e2e-jhipster2 && mocha \"test/repository-test/jhipster-2-test.js\"",
18
+ "test:unit": "mocha \"test/unit-test/**/*.spec.ts\" \"test/unit-test/**/*-spec.ts\"",
19
+ "test:e2e-core": "node scripts/clone-samples e2e-core && mocha \"test/repository-test/core-test.ts\"",
20
+ "test:e2e-jhipster1": "node scripts/clone-samples e2e-jhipster1 && mocha \"test/repository-test/jhipster-1-test.ts\"",
21
+ "test:e2e-jhipster2": "node scripts/clone-samples e2e-jhipster2 && mocha \"test/repository-test/jhipster-2-test.ts\"",
20
22
  "test:all": "npm-run-all test test:e2e-jhipster1 test:e2e-jhipster2",
21
- "clone-samples": "node scripts/clone-samples.js"
23
+ "clone-samples": "node scripts/clone-samples.js",
24
+ "build": "tsc -p tsconfig.build.json --outDir dist",
25
+ "build:watch": "tsc -p tsconfig.build.json --outDir dist -w"
22
26
  },
23
- "gitHead": "f0c2e4073dfecd1f00badfad402a4dde1777bd8c"
27
+ "devDependencies": {
28
+ "@babel/cli": "7.14.8",
29
+ "@babel/core": "7.15.0",
30
+ "@babel/preset-env": "7.15.0",
31
+ "@babel/preset-typescript": "7.15.0",
32
+ "@babel/register": "7.15.3",
33
+ "@chevrotain/types": "9.0.2",
34
+ "@types/chai": "4.2.21",
35
+ "@types/fs-extra": "9.0.12",
36
+ "@types/jest": "27.0.1",
37
+ "@types/klaw-sync": "6.0.1",
38
+ "@types/lodash": "4.14.172",
39
+ "@types/node": "16.7.1",
40
+ "@types/prettier": "2.3.1",
41
+ "@types/sinon": "10.0.2",
42
+ "ts-node": "10.2.1",
43
+ "typescript": "4.3.5"
44
+ },
45
+ "gitHead": "a0d5b167e79bc167f3b83c2abed81bad2f5245ac"
24
46
  }
@@ -1,145 +0,0 @@
1
- "use strict";
2
-
3
- const { BaseJavaCstVisitor } = require("java-parser");
4
- const { ArraysPrettierVisitor } = require("./printers/arrays");
5
- const {
6
- BlocksAndStatementPrettierVisitor
7
- } = require("./printers/blocks-and-statements");
8
- const { ClassesPrettierVisitor } = require("./printers/classes");
9
- const { ExpressionsPrettierVisitor } = require("./printers/expressions");
10
- const { InterfacesPrettierVisitor } = require("./printers/interfaces");
11
- const {
12
- LexicalStructurePrettierVisitor
13
- } = require("./printers/lexical-structure");
14
- const { NamesPrettierVisitor } = require("./printers/names");
15
- const {
16
- TypesValuesAndVariablesPrettierVisitor
17
- } = require("./printers/types-values-and-variables");
18
- const {
19
- PackagesAndModulesPrettierVisitor
20
- } = require("./printers/packages-and-modules");
21
- const {
22
- printNodeWithComments
23
- } = require("./printers/comments/format-comments");
24
-
25
- class CstPrettierPrinter extends BaseJavaCstVisitor {
26
- constructor() {
27
- super();
28
- // TODO: can we ignore the optimized lookahead methods here?
29
- this.validateVisitor();
30
-
31
- // TODO: this methods should be defined on the prototype
32
- // defining as instance members **after** the validations to avoid
33
- // false positive errors on redundant methods
34
- this.mapVisit = (elements, params) => {
35
- if (elements === undefined) {
36
- // TODO: can optimize this by returning an immutable empty array singleton.
37
- return [];
38
- }
39
-
40
- return elements.map(element => this.visit(element, params), this);
41
- };
42
-
43
- this.getSingle = function (ctx) {
44
- const ctxKeys = Object.keys(ctx);
45
- if (ctxKeys.length !== 1) {
46
- throw Error(
47
- `Expecting single key CST ctx but found: <${ctxKeys.length}> keys`
48
- );
49
- }
50
- const singleElementKey = ctxKeys[0];
51
- const singleElementValues = ctx[singleElementKey];
52
-
53
- if (singleElementValues.length !== 1) {
54
- throw Error(
55
- `Expecting single item in CST ctx key but found: <${singleElementValues.length}> items`
56
- );
57
- }
58
-
59
- return singleElementValues[0];
60
- };
61
-
62
- this.visitSingle = function (ctx, params) {
63
- const singleElement = this.getSingle(ctx);
64
- return this.visit(singleElement, params);
65
- };
66
-
67
- // hack to get a reference to the inherited visit method from
68
- // the prototype because we cannot user "super.visit" inside the function
69
- // below
70
- const orgVisit = this.visit;
71
- this.visit = function (ctx, inParam) {
72
- if (ctx === undefined) {
73
- // empty Doc
74
- return "";
75
- }
76
-
77
- const node = Array.isArray(ctx) ? ctx[0] : ctx;
78
-
79
- if (node.ignore) {
80
- try {
81
- const startOffset =
82
- node.leadingComments !== undefined
83
- ? node.leadingComments[0].startOffset
84
- : node.location.startOffset;
85
- const endOffset =
86
- node.trailingComments !== undefined
87
- ? node.trailingComments[node.trailingComments.length - 1]
88
- .endOffset
89
- : node.location.endOffset;
90
-
91
- return this.prettierOptions.originalText.substring(
92
- startOffset,
93
- endOffset + 1
94
- );
95
- } catch (e) {
96
- throw Error(
97
- e +
98
- "\nThere might be a problem with prettier-ignore, please report an issue on https://github.com/jhipster/prettier-java/issues"
99
- );
100
- }
101
- }
102
-
103
- return printNodeWithComments(node, orgVisit.call(this, node, inParam));
104
- };
105
- }
106
- }
107
-
108
- // Mixins for the win
109
- mixInMethods(
110
- ArraysPrettierVisitor,
111
- BlocksAndStatementPrettierVisitor,
112
- ClassesPrettierVisitor,
113
- ExpressionsPrettierVisitor,
114
- InterfacesPrettierVisitor,
115
- LexicalStructurePrettierVisitor,
116
- NamesPrettierVisitor,
117
- TypesValuesAndVariablesPrettierVisitor,
118
- PackagesAndModulesPrettierVisitor
119
- );
120
-
121
- function mixInMethods(...classesToMix) {
122
- classesToMix.forEach(from => {
123
- const fromMethodsNames = Object.getOwnPropertyNames(from.prototype);
124
- const fromPureMethodsName = fromMethodsNames.filter(
125
- methodName => methodName !== "constructor"
126
- );
127
- fromPureMethodsName.forEach(methodName => {
128
- CstPrettierPrinter.prototype[methodName] = from.prototype[methodName];
129
- });
130
- });
131
- }
132
-
133
- const prettyPrinter = new CstPrettierPrinter();
134
-
135
- // TODO: do we need the "path" and "print" arguments passed by prettier
136
- // see https://github.com/prettier/prettier/issues/5747
137
- function createPrettierDoc(cstNode, options) {
138
- prettyPrinter.prettierOptions = options;
139
- return prettyPrinter.visit(cstNode);
140
- }
141
-
142
- module.exports = {
143
- CstPrettierPrinter,
144
- createPrettierDoc
145
- };
package/src/index.js DELETED
@@ -1,79 +0,0 @@
1
- "use strict";
2
-
3
- const parse = require("./parser");
4
- const print = require("./printer");
5
- const options = require("./options");
6
-
7
- const languages = [
8
- {
9
- name: "Java",
10
- parsers: ["java"],
11
- group: "Java",
12
- tmScope: "text.html.vue", // FIXME
13
- aceMode: "html", // FIXME
14
- codemirrorMode: "clike",
15
- codemirrorMimeType: "text/x-java",
16
- extensions: [".java"],
17
- linguistLanguageId: 181,
18
- vscodeLanguageIds: ["java"]
19
- }
20
- ];
21
-
22
- function locStart(/* node */) {
23
- return -1;
24
- }
25
-
26
- function locEnd(/* node */) {
27
- return -1;
28
- }
29
-
30
- function hasPragma(/* text */) {
31
- return false;
32
- }
33
-
34
- const parsers = {
35
- java: {
36
- parse,
37
- astFormat: "java",
38
- locStart,
39
- locEnd,
40
- hasPragma
41
- }
42
- };
43
-
44
- function canAttachComment(node) {
45
- return node.ast_type && node.ast_type !== "comment";
46
- }
47
-
48
- function printComment(commentPath) {
49
- const comment = commentPath.getValue();
50
-
51
- switch (comment.ast_type) {
52
- case "comment":
53
- return comment.value;
54
- default:
55
- throw new Error("Not a comment: " + JSON.stringify(comment));
56
- }
57
- }
58
-
59
- function clean(ast, newObj) {
60
- delete newObj.lineno;
61
- delete newObj.col_offset;
62
- }
63
-
64
- const printers = {
65
- java: {
66
- print,
67
- // hasPrettierIgnore,
68
- printComment,
69
- canAttachComment,
70
- massageAstNode: clean
71
- }
72
- };
73
-
74
- module.exports = {
75
- languages,
76
- printers,
77
- parsers,
78
- options
79
- };