flow-parser 0.320.0 → 0.322.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/README.md +14 -26
- package/{oxidized/oxidized-src → dist}/FlowParser.js +3 -53
- package/{oxidized/oxidized-src → dist}/FlowParserDeserializer.js +3 -41
- package/{oxidized → dist}/FlowParserNodeDeserializers.js +0 -7
- package/{oxidized → dist}/FlowParserWASM.js +1 -1
- package/{oxidized/oxidized-src → dist}/HermesParserDecodeUTF8String.js +0 -9
- package/dist/ParserOptions.js +7 -0
- package/{oxidized → dist}/babel/TransformESTreeToBabel.js +9 -157
- package/{oxidized → dist}/estree/StripFlowTypes.js +0 -26
- package/{oxidized/oxidized-src → dist}/estree/StripFlowTypesForBabel.js +0 -24
- package/{oxidized/oxidized-src → dist}/estree/TransformComponentSyntax.js +0 -87
- package/{oxidized/oxidized-src → dist}/estree/TransformEnumSyntax.js +0 -12
- package/{oxidized/oxidized-src → dist}/estree/TransformMatchSyntax.js +3 -99
- package/{oxidized → dist}/estree/TransformRecordSyntax.js +3 -31
- package/{oxidized → dist}/getModuleDocblock.js +0 -21
- package/{oxidized → dist}/index.js +25 -99
- package/{oxidized → dist}/transform/SimpleTransform.js +0 -21
- package/{oxidized → dist}/transform/astArrayMutationHelpers.js +0 -9
- package/{oxidized/oxidized-src → dist}/transform/astNodeMutationHelpers.js +4 -26
- package/{oxidized/oxidized-src → dist}/transform/print/comments/comments.js +2 -51
- package/{oxidized → dist}/transform/print/comments/prettier/common/util.js +0 -49
- package/{oxidized → dist}/transform/print/comments/prettier/language-js/comments.js +0 -111
- package/{oxidized/oxidized-src → dist}/transform/print/comments/prettier/language-js/loc.js +0 -6
- package/{oxidized/oxidized-src → dist}/transform/print/comments/prettier/language-js/printer-estree.js +0 -3
- package/{oxidized/oxidized-src → dist}/transform/print/comments/prettier/language-js/utils.js +0 -20
- package/{oxidized/oxidized-src → dist}/transform/print/comments/prettier/main/comments.js +2 -63
- package/{oxidized/oxidized-src → dist}/transform/print/comments/prettier/utils/get-last.js +0 -1
- package/{oxidized → dist}/transform/print/print.js +6 -22
- package/{oxidized/oxidized-src → dist}/traverse/SimpleTraverser.js +2 -22
- package/{oxidized/oxidized-src → dist}/traverse/getVisitorKeys.js +3 -9
- package/{oxidized/oxidized-src → dist}/utils/Builders.js +1 -17
- package/{oxidized → dist}/utils/GenID.js +0 -7
- package/{oxidized/oxidized-src → dist}/utils/createSyntaxError.js +0 -1
- package/{oxidized → dist}/utils/isReservedWord.js +0 -2
- package/{oxidized → dist}/utils/mutateESTreeASTForPrettier.js +6 -21
- package/package.json +6 -7
- package/flow_parser.js +0 -14585
- package/oxidized/FlowParser.js +0 -249
- package/oxidized/FlowParserDeserializer.js +0 -209
- package/oxidized/HermesParserDecodeUTF8String.js +0 -45
- package/oxidized/ParserOptions.js +0 -8
- package/oxidized/estree/StripFlowTypesForBabel.js +0 -191
- package/oxidized/estree/TransformComponentSyntax.js +0 -764
- package/oxidized/estree/TransformEnumSyntax.js +0 -92
- package/oxidized/estree/TransformMatchSyntax.js +0 -990
- package/oxidized/oxidized-src/FlowParserNodeDeserializers.js +0 -1781
- package/oxidized/oxidized-src/ParserOptions.js +0 -8
- package/oxidized/oxidized-src/babel/TransformESTreeToBabel.js +0 -1063
- package/oxidized/oxidized-src/estree/StripFlowTypes.js +0 -158
- package/oxidized/oxidized-src/estree/TransformRecordSyntax.js +0 -283
- package/oxidized/oxidized-src/generated/ESTreeVisitorKeys.js +0 -234
- package/oxidized/oxidized-src/getModuleDocblock.js +0 -87
- package/oxidized/oxidized-src/index.js +0 -220
- package/oxidized/oxidized-src/transform/SimpleTransform.js +0 -95
- package/oxidized/oxidized-src/transform/astArrayMutationHelpers.js +0 -52
- package/oxidized/oxidized-src/transform/print/comments/prettier/common/util.js +0 -235
- package/oxidized/oxidized-src/transform/print/comments/prettier/language-js/comments.js +0 -663
- package/oxidized/oxidized-src/transform/print/print.js +0 -68
- package/oxidized/oxidized-src/utils/GenID.js +0 -37
- package/oxidized/oxidized-src/utils/isReservedWord.js +0 -53
- package/oxidized/oxidized-src/utils/mutateESTreeASTForPrettier.js +0 -85
- package/oxidized/transform/astNodeMutationHelpers.js +0 -142
- package/oxidized/transform/print/comments/comments.js +0 -245
- package/oxidized/transform/print/comments/prettier/language-js/loc.js +0 -30
- package/oxidized/transform/print/comments/prettier/language-js/printer-estree.js +0 -23
- package/oxidized/transform/print/comments/prettier/language-js/utils.js +0 -89
- package/oxidized/transform/print/comments/prettier/main/comments.js +0 -374
- package/oxidized/transform/print/comments/prettier/utils/get-last.js +0 -5
- package/oxidized/transform/print/detachedNodeTypes.js +0 -1
- package/oxidized/traverse/SimpleTraverser.js +0 -90
- package/oxidized/traverse/getVisitorKeys.js +0 -29
- package/oxidized/utils/Builders.js +0 -181
- package/oxidized/utils/createSyntaxError.js +0 -15
- /package/{oxidized → dist}/FlowParser.js.flow +0 -0
- /package/{oxidized → dist}/FlowParserDeserializer.js.flow +0 -0
- /package/{oxidized → dist}/FlowParserNodeDeserializers.js.flow +0 -0
- /package/{oxidized → dist}/FlowParserWASM.js.flow +0 -0
- /package/{oxidized → dist}/HermesAST.js.flow +0 -0
- /package/{oxidized → dist}/HermesParserDecodeUTF8String.js.flow +0 -0
- /package/{oxidized → dist}/ParserOptions.js.flow +0 -0
- /package/{oxidized → dist}/babel/TransformESTreeToBabel.js.flow +0 -0
- /package/{oxidized → dist}/estree/StripFlowTypes.js.flow +0 -0
- /package/{oxidized → dist}/estree/StripFlowTypesForBabel.js.flow +0 -0
- /package/{oxidized → dist}/estree/TransformComponentSyntax.js.flow +0 -0
- /package/{oxidized → dist}/estree/TransformEnumSyntax.js.flow +0 -0
- /package/{oxidized → dist}/estree/TransformMatchSyntax.js.flow +0 -0
- /package/{oxidized → dist}/estree/TransformRecordSyntax.js.flow +0 -0
- /package/{oxidized → dist}/generated/ESTreeVisitorKeys.js +0 -0
- /package/{oxidized → dist}/generated/ESTreeVisitorKeys.js.flow +0 -0
- /package/{oxidized → dist}/getModuleDocblock.js.flow +0 -0
- /package/{oxidized → dist}/index.js.flow +0 -0
- /package/{oxidized → dist}/transform/SimpleTransform.js.flow +0 -0
- /package/{oxidized → dist}/transform/astArrayMutationHelpers.js.flow +0 -0
- /package/{oxidized → dist}/transform/astNodeMutationHelpers.js.flow +0 -0
- /package/{oxidized → dist}/transform/print/comments/comments.js.flow +0 -0
- /package/{oxidized/oxidized-src → dist}/transform/print/detachedNodeTypes.js +0 -0
- /package/{oxidized → dist}/transform/print/detachedNodeTypes.js.flow +0 -0
- /package/{oxidized → dist}/transform/print/print.js.flow +0 -0
- /package/{oxidized → dist}/traverse/SimpleTraverser.js.flow +0 -0
- /package/{oxidized → dist}/traverse/getVisitorKeys.js.flow +0 -0
- /package/{oxidized → dist}/utils/Builders.js.flow +0 -0
- /package/{oxidized → dist}/utils/GenID.js.flow +0 -0
- /package/{oxidized → dist}/utils/createSyntaxError.js.flow +0 -0
- /package/{oxidized → dist}/utils/isReservedWord.js.flow +0 -0
- /package/{oxidized → dist}/utils/mutateESTreeASTForPrettier.js.flow +0 -0
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.print = print;
|
|
7
|
-
|
|
8
|
-
var _mutateESTreeASTForPrettier = _interopRequireDefault(require("../../utils/mutateESTreeASTForPrettier"));
|
|
9
|
-
|
|
10
|
-
var prettier = _interopRequireWildcard(require("prettier"));
|
|
11
|
-
|
|
12
|
-
var _comments = require("./comments/comments");
|
|
13
|
-
|
|
14
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
15
|
-
|
|
16
|
-
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
17
|
-
|
|
18
|
-
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
19
|
-
|
|
20
|
-
async function print(ast, originalCode, prettierOptions = {}, visitorKeys) {
|
|
21
|
-
const program = ast;
|
|
22
|
-
|
|
23
|
-
if (program.body.length === 0) {
|
|
24
|
-
var _program$docblock;
|
|
25
|
-
|
|
26
|
-
const docblockComment = (_program$docblock = program.docblock) == null ? void 0 : _program$docblock.comment;
|
|
27
|
-
|
|
28
|
-
if (docblockComment != null) {
|
|
29
|
-
return '/*' + docblockComment.value + '*/\n';
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
return '';
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const codeForPrinting = (0, _comments.mutateESTreeASTCommentsForPrettier)(program, originalCode);
|
|
36
|
-
(0, _mutateESTreeASTForPrettier.default)(program, visitorKeys);
|
|
37
|
-
let pluginParserName = 'flow';
|
|
38
|
-
let pluginParser;
|
|
39
|
-
let pluginPrinter;
|
|
40
|
-
|
|
41
|
-
try {
|
|
42
|
-
const prettierHermesPlugin = await Promise.resolve().then(() => _interopRequireWildcard(require('prettier-plugin-hermes-parser')));
|
|
43
|
-
pluginParser = prettierHermesPlugin.parsers.hermes;
|
|
44
|
-
pluginPrinter = prettierHermesPlugin.printers;
|
|
45
|
-
pluginParserName = 'hermes';
|
|
46
|
-
} catch {
|
|
47
|
-
const prettierFlowPlugin = require('prettier/plugins/flow');
|
|
48
|
-
|
|
49
|
-
pluginParser = prettierFlowPlugin.parsers.flow;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return prettier.format(codeForPrinting, { ...prettierOptions,
|
|
53
|
-
parser: pluginParserName,
|
|
54
|
-
requirePragma: false,
|
|
55
|
-
plugins: [{
|
|
56
|
-
parsers: {
|
|
57
|
-
[pluginParserName]: { ...pluginParser,
|
|
58
|
-
|
|
59
|
-
parse() {
|
|
60
|
-
return program;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
}
|
|
64
|
-
},
|
|
65
|
-
printers: pluginPrinter
|
|
66
|
-
}]
|
|
67
|
-
});
|
|
68
|
-
}
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = void 0;
|
|
7
|
-
const genPrefix = '$$gen$';
|
|
8
|
-
|
|
9
|
-
class GenID {
|
|
10
|
-
constructor(uniqueTransformPrefix) {
|
|
11
|
-
this.genN = 0;
|
|
12
|
-
this.used = new Set();
|
|
13
|
-
this.prefix = void 0;
|
|
14
|
-
this.prefix = `${genPrefix}${uniqueTransformPrefix}`;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
id() {
|
|
18
|
-
let name;
|
|
19
|
-
|
|
20
|
-
do {
|
|
21
|
-
name = `${this.prefix}${this.genN}`;
|
|
22
|
-
this.genN++;
|
|
23
|
-
} while (this.used.has(name));
|
|
24
|
-
|
|
25
|
-
this.used.add(name);
|
|
26
|
-
return name;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
addUsage(name) {
|
|
30
|
-
if (name.startsWith(this.prefix)) {
|
|
31
|
-
this.used.add(name);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
exports.default = GenID;
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = isReservedWord;
|
|
7
|
-
|
|
8
|
-
function isReservedWord(name) {
|
|
9
|
-
switch (name) {
|
|
10
|
-
case 'await':
|
|
11
|
-
case 'break':
|
|
12
|
-
case 'case':
|
|
13
|
-
case 'catch':
|
|
14
|
-
case 'class':
|
|
15
|
-
case 'const':
|
|
16
|
-
case 'continue':
|
|
17
|
-
case 'debugger':
|
|
18
|
-
case 'default':
|
|
19
|
-
case 'delete':
|
|
20
|
-
case 'do':
|
|
21
|
-
case 'else':
|
|
22
|
-
case 'enum':
|
|
23
|
-
case 'export':
|
|
24
|
-
case 'extends':
|
|
25
|
-
case 'false':
|
|
26
|
-
case 'finally':
|
|
27
|
-
case 'for':
|
|
28
|
-
case 'function':
|
|
29
|
-
case 'if':
|
|
30
|
-
case 'import':
|
|
31
|
-
case 'in':
|
|
32
|
-
case 'instanceof':
|
|
33
|
-
case 'new':
|
|
34
|
-
case 'null':
|
|
35
|
-
case 'return':
|
|
36
|
-
case 'super':
|
|
37
|
-
case 'switch':
|
|
38
|
-
case 'this':
|
|
39
|
-
case 'throw':
|
|
40
|
-
case 'true':
|
|
41
|
-
case 'try':
|
|
42
|
-
case 'typeof':
|
|
43
|
-
case 'var':
|
|
44
|
-
case 'void':
|
|
45
|
-
case 'while':
|
|
46
|
-
case 'with':
|
|
47
|
-
case 'yield':
|
|
48
|
-
return true;
|
|
49
|
-
|
|
50
|
-
default:
|
|
51
|
-
return false;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = mutate;
|
|
7
|
-
|
|
8
|
-
var _SimpleTransform = require("../transform/SimpleTransform");
|
|
9
|
-
|
|
10
|
-
function transformChainExpression(node, comments) {
|
|
11
|
-
if (comments != null) {
|
|
12
|
-
var _node$comments;
|
|
13
|
-
|
|
14
|
-
const joinedComments = comments.concat((_node$comments = node.comments) != null ? _node$comments : []);
|
|
15
|
-
node.comments = joinedComments;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
switch (node.type) {
|
|
19
|
-
case 'CallExpression':
|
|
20
|
-
return { ...node,
|
|
21
|
-
type: 'OptionalCallExpression',
|
|
22
|
-
callee: transformChainExpression(node.callee)
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
case 'MemberExpression':
|
|
26
|
-
return { ...node,
|
|
27
|
-
type: 'OptionalMemberExpression',
|
|
28
|
-
object: transformChainExpression(node.object)
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
return node;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function mutate(rootNode, visitorKeys) {
|
|
36
|
-
_SimpleTransform.SimpleTransform.transform(rootNode, {
|
|
37
|
-
transform(node) {
|
|
38
|
-
if (node.parent) {
|
|
39
|
-
delete node.parent;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
if (node.type === 'ChainExpression') {
|
|
43
|
-
return transformChainExpression(node.expression, node == null ? void 0 : node.comments);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
if (node.type === 'ObjectTypeProperty') {
|
|
47
|
-
if (node.method === false && node.kind === 'init' && node.range[0] === 1 && node.value.range[0] === 1) {
|
|
48
|
-
node.value = { ...node.value,
|
|
49
|
-
range: [2, node.value.range[1]]
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
return node;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
if (node.type === 'ImportSpecifier') {
|
|
57
|
-
if (node.local.name === node.imported.name) {
|
|
58
|
-
if (node.local.range == null) {
|
|
59
|
-
node.local.range = [0, 0];
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
node.imported.range = [...node.local.range];
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
return node;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
if (node.type === 'ExportSpecifier') {
|
|
69
|
-
if (node.local.name === node.exported.name) {
|
|
70
|
-
if (node.local.range == null) {
|
|
71
|
-
node.local.range = [0, 0];
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
node.exported.range = [...node.local.range];
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
return node;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
return node;
|
|
81
|
-
},
|
|
82
|
-
|
|
83
|
-
visitorKeys
|
|
84
|
-
});
|
|
85
|
-
}
|
|
@@ -1,142 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.deepCloneNode = deepCloneNode;
|
|
7
|
-
exports.nodeWith = nodeWith;
|
|
8
|
-
exports.removeNodeOnParent = removeNodeOnParent;
|
|
9
|
-
exports.replaceNodeOnParent = replaceNodeOnParent;
|
|
10
|
-
exports.setParentPointersInDirectChildren = setParentPointersInDirectChildren;
|
|
11
|
-
exports.shallowCloneNode = shallowCloneNode;
|
|
12
|
-
exports.updateAllParentPointers = updateAllParentPointers;
|
|
13
|
-
|
|
14
|
-
var _astArrayMutationHelpers = require("./astArrayMutationHelpers");
|
|
15
|
-
|
|
16
|
-
var _getVisitorKeys = require("../traverse/getVisitorKeys");
|
|
17
|
-
|
|
18
|
-
var _SimpleTraverser = require("../traverse/SimpleTraverser");
|
|
19
|
-
|
|
20
|
-
function getParentKey(target, parent, visitorKeys) {
|
|
21
|
-
if (parent == null) {
|
|
22
|
-
throw new Error(`Expected parent node to be set on "${target.type}"`);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
for (const key of (0, _getVisitorKeys.getVisitorKeys)(parent, visitorKeys)) {
|
|
26
|
-
if ((0, _getVisitorKeys.isNode)(parent[key])) {
|
|
27
|
-
if (parent[key] === target) {
|
|
28
|
-
return {
|
|
29
|
-
type: 'single',
|
|
30
|
-
node: parent,
|
|
31
|
-
key
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
} else if (Array.isArray(parent[key])) {
|
|
35
|
-
for (let i = 0; i < parent[key].length; i += 1) {
|
|
36
|
-
const current = parent[key][i];
|
|
37
|
-
|
|
38
|
-
if (current === target) {
|
|
39
|
-
return {
|
|
40
|
-
type: 'array',
|
|
41
|
-
node: parent,
|
|
42
|
-
key,
|
|
43
|
-
targetIndex: i
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
throw new Error(`Expected to find the ${target.type} as a direct child of the ${parent.type}.`);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function replaceNodeOnParent(originalNode, originalNodeParent, nodeToReplaceWith, visitorKeys) {
|
|
54
|
-
const replacementParent = getParentKey(originalNode, originalNodeParent, visitorKeys);
|
|
55
|
-
const parent = replacementParent.node;
|
|
56
|
-
|
|
57
|
-
if (replacementParent.type === 'array') {
|
|
58
|
-
parent[replacementParent.key] = (0, _astArrayMutationHelpers.replaceInArray)(parent[replacementParent.key], replacementParent.targetIndex, Array.isArray(nodeToReplaceWith) ? nodeToReplaceWith : [nodeToReplaceWith]);
|
|
59
|
-
} else {
|
|
60
|
-
if (Array.isArray(nodeToReplaceWith)) {
|
|
61
|
-
throw new Error(`Cannot insert array into non-array parent type: ${parent.type}`);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
parent[replacementParent.key] = nodeToReplaceWith;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function removeNodeOnParent(originalNode, originalNodeParent, visitorKeys) {
|
|
69
|
-
const replacementParent = getParentKey(originalNode, originalNodeParent, visitorKeys);
|
|
70
|
-
const parent = replacementParent.node;
|
|
71
|
-
|
|
72
|
-
if (replacementParent.type === 'array') {
|
|
73
|
-
parent[replacementParent.key] = (0, _astArrayMutationHelpers.removeFromArray)(parent[replacementParent.key], replacementParent.targetIndex);
|
|
74
|
-
} else {
|
|
75
|
-
parent[replacementParent.key] = null;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
function setParentPointersInDirectChildren(node, visitorKeys) {
|
|
80
|
-
for (const key of (0, _getVisitorKeys.getVisitorKeys)(node, visitorKeys)) {
|
|
81
|
-
if ((0, _getVisitorKeys.isNode)(node[key])) {
|
|
82
|
-
node[key].parent = node;
|
|
83
|
-
} else if (Array.isArray(node[key])) {
|
|
84
|
-
for (const child of node[key]) {
|
|
85
|
-
child.parent = node;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
function updateAllParentPointers(node, visitorKeys) {
|
|
92
|
-
_SimpleTraverser.SimpleTraverser.traverse(node, {
|
|
93
|
-
enter(node, parent) {
|
|
94
|
-
node.parent = parent;
|
|
95
|
-
},
|
|
96
|
-
|
|
97
|
-
leave() {},
|
|
98
|
-
|
|
99
|
-
visitorKeys
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
function nodeWith(node, overrideProps, visitorKeys) {
|
|
104
|
-
const willBeUnchanged = Object.entries(overrideProps).every(([key, value]) => {
|
|
105
|
-
const node_ = node;
|
|
106
|
-
|
|
107
|
-
if (Array.isArray(value)) {
|
|
108
|
-
return Array.isArray(node_[key]) ? (0, _astArrayMutationHelpers.arrayIsEqual)(node_[key], value) : false;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
return node_[key] === value;
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
if (willBeUnchanged) {
|
|
115
|
-
return node;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
const newNode = { ...node,
|
|
119
|
-
...overrideProps
|
|
120
|
-
};
|
|
121
|
-
setParentPointersInDirectChildren(newNode, visitorKeys);
|
|
122
|
-
return newNode;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
function shallowCloneNode(node, visitorKeys) {
|
|
126
|
-
const newNode = { ...node
|
|
127
|
-
};
|
|
128
|
-
setParentPointersInDirectChildren(newNode, visitorKeys);
|
|
129
|
-
return newNode;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
function deepCloneNode(node, visitorKeys) {
|
|
133
|
-
const clone = JSON.parse(JSON.stringify(node, (key, value) => {
|
|
134
|
-
if (key === 'parent') {
|
|
135
|
-
return undefined;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
return value;
|
|
139
|
-
}));
|
|
140
|
-
updateAllParentPointers(clone, visitorKeys);
|
|
141
|
-
return clone;
|
|
142
|
-
}
|
|
@@ -1,245 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.CommentPlacement = void 0;
|
|
7
|
-
exports.addComment = addComment;
|
|
8
|
-
exports.appendCommentToSource = appendCommentToSource;
|
|
9
|
-
exports.attachComments = attachComments;
|
|
10
|
-
exports.cloneComment = cloneComment;
|
|
11
|
-
exports.cloneCommentWithMarkers = cloneCommentWithMarkers;
|
|
12
|
-
exports.cloneCommentsToNewNode = cloneCommentsToNewNode;
|
|
13
|
-
exports.cloneJSDocCommentsToNewNode = cloneJSDocCommentsToNewNode;
|
|
14
|
-
exports.getCommentsForNode = getCommentsForNode;
|
|
15
|
-
exports.getLeadingCommentsForNode = getLeadingCommentsForNode;
|
|
16
|
-
exports.getTrailingCommentsForNode = getTrailingCommentsForNode;
|
|
17
|
-
exports.isAttachedComment = isAttachedComment;
|
|
18
|
-
exports.isLeadingComment = isLeadingComment;
|
|
19
|
-
exports.isTrailingComment = isTrailingComment;
|
|
20
|
-
exports.makeCommentOwnLine = makeCommentOwnLine;
|
|
21
|
-
exports.moveCommentsToNewNode = moveCommentsToNewNode;
|
|
22
|
-
exports.mutateESTreeASTCommentsForPrettier = mutateESTreeASTCommentsForPrettier;
|
|
23
|
-
exports.setCommentsOnNode = setCommentsOnNode;
|
|
24
|
-
|
|
25
|
-
var _comments = require("./prettier/main/comments");
|
|
26
|
-
|
|
27
|
-
var _loc = require("./prettier/language-js/loc");
|
|
28
|
-
|
|
29
|
-
var _printerEstree = _interopRequireDefault(require("./prettier/language-js/printer-estree"));
|
|
30
|
-
|
|
31
|
-
var _util = require("./prettier/common/util");
|
|
32
|
-
|
|
33
|
-
var _flowEstree = require("flow-estree");
|
|
34
|
-
|
|
35
|
-
var _os = require("os");
|
|
36
|
-
|
|
37
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
38
|
-
|
|
39
|
-
const CommentPlacement = require("flow-enums-runtime").Mirrored(["LEADING_OWN_LINE", "LEADING_INLINE", "TRAILING_OWN_LINE", "TRAILING_INLINE"]);
|
|
40
|
-
|
|
41
|
-
exports.CommentPlacement = CommentPlacement;
|
|
42
|
-
|
|
43
|
-
function attachComments(comments, ast, text) {
|
|
44
|
-
(0, _comments.attach)(comments, ast, text, {
|
|
45
|
-
locStart: _loc.locStart,
|
|
46
|
-
locEnd: _loc.locEnd,
|
|
47
|
-
printer: _printerEstree.default
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function mutateESTreeASTCommentsForPrettier(program, text) {
|
|
52
|
-
let code = text;
|
|
53
|
-
delete program.comments;
|
|
54
|
-
|
|
55
|
-
if (program.docblock != null && program.docblock.comment != null) {
|
|
56
|
-
const docblockComment = program.docblock.comment;
|
|
57
|
-
const isDocblockCommentNew = !isAttachedComment(docblockComment);
|
|
58
|
-
|
|
59
|
-
if (isDocblockCommentNew) {
|
|
60
|
-
docblockComment.printed = false;
|
|
61
|
-
docblockComment.leading = true;
|
|
62
|
-
docblockComment.trailing = false;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
if (program.body.length > 0) {
|
|
66
|
-
const firstStatement = program.body[0];
|
|
67
|
-
const leadingComments = getLeadingCommentsForNode(firstStatement);
|
|
68
|
-
|
|
69
|
-
if (!leadingComments.includes(docblockComment)) {
|
|
70
|
-
setCommentsOnNode(firstStatement, [docblockComment, ...getCommentsForNode(firstStatement)]);
|
|
71
|
-
|
|
72
|
-
if (isDocblockCommentNew) {
|
|
73
|
-
code = makeCommentOwnLine(code, docblockComment);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
} else {
|
|
77
|
-
setCommentsOnNode(program, [docblockComment]);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
delete program.docblock;
|
|
82
|
-
return code;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
function moveCommentsToNewNode(oldNode, newNode) {
|
|
86
|
-
setCommentsOnNode(newNode, getCommentsForNode(oldNode));
|
|
87
|
-
setCommentsOnNode(oldNode, []);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
function cloneCommentsToNewNode(oldNode, newNode) {
|
|
91
|
-
setCommentsOnNode(newNode, getCommentsForNode(oldNode).map(comment => cloneCommentWithMarkers(comment)));
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
function cloneJSDocCommentsToNewNode(oldNode, newNode) {
|
|
95
|
-
const comments = getCommentsForNode(oldNode).filter(comment => {
|
|
96
|
-
return (0, _flowEstree.isBlockComment)(comment) && comment.value.startsWith('*');
|
|
97
|
-
});
|
|
98
|
-
setCommentsOnNode(newNode, [...getCommentsForNode(newNode), ...comments.map(cloneCommentWithMarkers)]);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
function setCommentsOnNode(node, comments) {
|
|
102
|
-
node.comments = comments;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
function getCommentsForNode(node) {
|
|
106
|
-
var _node$comments;
|
|
107
|
-
|
|
108
|
-
return (_node$comments = node.comments) != null ? _node$comments : [];
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
function isAttachedComment(comment) {
|
|
112
|
-
return comment.printed === false;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
function isLeadingComment(comment) {
|
|
116
|
-
return comment.leading === true;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
function isTrailingComment(comment) {
|
|
120
|
-
return comment.trailing === true;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
function getLeadingCommentsForNode(node) {
|
|
124
|
-
return getCommentsForNode(node).filter(isLeadingComment);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
function getTrailingCommentsForNode(node) {
|
|
128
|
-
return getCommentsForNode(node).filter(isTrailingComment);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
function addComment(node, comment, placement) {
|
|
132
|
-
switch (placement) {
|
|
133
|
-
case CommentPlacement.LEADING_OWN_LINE:
|
|
134
|
-
case CommentPlacement.LEADING_INLINE:
|
|
135
|
-
{
|
|
136
|
-
(0, _util.addLeadingComment)(node, comment);
|
|
137
|
-
break;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
case CommentPlacement.TRAILING_OWN_LINE:
|
|
141
|
-
case CommentPlacement.TRAILING_INLINE:
|
|
142
|
-
{
|
|
143
|
-
(0, _util.addTrailingComment)(node, comment);
|
|
144
|
-
break;
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
function cloneComment(comment) {
|
|
150
|
-
return {
|
|
151
|
-
type: comment.type,
|
|
152
|
-
value: comment.value,
|
|
153
|
-
loc: comment.loc,
|
|
154
|
-
range: comment.range
|
|
155
|
-
};
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
function cloneCommentWithMarkers(comment) {
|
|
159
|
-
return {
|
|
160
|
-
type: comment.type,
|
|
161
|
-
value: comment.value,
|
|
162
|
-
loc: comment.loc,
|
|
163
|
-
range: comment.range,
|
|
164
|
-
leading: isLeadingComment(comment),
|
|
165
|
-
trailing: isTrailingComment(comment)
|
|
166
|
-
};
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
function getFirstNewlineIndex(code) {
|
|
170
|
-
return code.search(/\r\n|\n|\r/);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
function getFirstNonWhitespaceIndex(code) {
|
|
174
|
-
return code.search(/\S/);
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
function makeCommentOwnLine(code, comment) {
|
|
178
|
-
let newCode = code;
|
|
179
|
-
let firstNewline = getFirstNewlineIndex(code);
|
|
180
|
-
|
|
181
|
-
if (firstNewline === -1) {
|
|
182
|
-
newCode += _os.EOL;
|
|
183
|
-
firstNewline = newCode.length;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
comment.range = [firstNewline + 1, firstNewline];
|
|
187
|
-
return newCode;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
function appendCommentToSource(code, comment, placement) {
|
|
191
|
-
let newCode = code;
|
|
192
|
-
|
|
193
|
-
switch (comment.type) {
|
|
194
|
-
case 'Block':
|
|
195
|
-
{
|
|
196
|
-
switch (placement) {
|
|
197
|
-
case CommentPlacement.LEADING_OWN_LINE:
|
|
198
|
-
case CommentPlacement.TRAILING_OWN_LINE:
|
|
199
|
-
{
|
|
200
|
-
newCode = makeCommentOwnLine(code, comment);
|
|
201
|
-
break;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
case CommentPlacement.LEADING_INLINE:
|
|
205
|
-
case CommentPlacement.TRAILING_INLINE:
|
|
206
|
-
{
|
|
207
|
-
let firstNonWhitespace = getFirstNonWhitespaceIndex(code);
|
|
208
|
-
|
|
209
|
-
if (firstNonWhitespace === -1) {
|
|
210
|
-
newCode += '$FORCE_INLINE_ON_EMPTY_FILE_TOKEN$;';
|
|
211
|
-
firstNonWhitespace = newCode.length;
|
|
212
|
-
break;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
comment.range = [firstNonWhitespace + 1, firstNonWhitespace];
|
|
216
|
-
break;
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
break;
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
case 'Line':
|
|
224
|
-
{
|
|
225
|
-
const commentText = `//${comment.value}`;
|
|
226
|
-
const lastChar = newCode[newCode.length - 1];
|
|
227
|
-
|
|
228
|
-
if (lastChar !== '\n' && lastChar !== '\r') {
|
|
229
|
-
newCode += _os.EOL;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
if (placement === CommentPlacement.TRAILING_INLINE) {
|
|
233
|
-
newCode += '$FORCE_END_OF_LINE_COMMENT_TOKEN$;';
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
const start = newCode.length;
|
|
237
|
-
newCode += commentText;
|
|
238
|
-
const end = newCode.length;
|
|
239
|
-
comment.range = [start, end];
|
|
240
|
-
break;
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
return newCode;
|
|
245
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const {
|
|
4
|
-
isNonEmptyArray
|
|
5
|
-
} = require('../common/util.js');
|
|
6
|
-
|
|
7
|
-
function locStart(node, opts) {
|
|
8
|
-
const {
|
|
9
|
-
ignoreDecorators
|
|
10
|
-
} = opts || {};
|
|
11
|
-
|
|
12
|
-
if (!ignoreDecorators) {
|
|
13
|
-
const decorators = node.declaration && node.declaration.decorators || node.decorators;
|
|
14
|
-
|
|
15
|
-
if (isNonEmptyArray(decorators)) {
|
|
16
|
-
return locStart(decorators[0]);
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
return node.range ? node.range[0] : node.start;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function locEnd(node) {
|
|
24
|
-
return node.range ? node.range[1] : node.end;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
module.exports = {
|
|
28
|
-
locStart,
|
|
29
|
-
locEnd
|
|
30
|
-
};
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const handleComments = require('./comments.js');
|
|
4
|
-
|
|
5
|
-
const {
|
|
6
|
-
isBlockComment,
|
|
7
|
-
isLineComment
|
|
8
|
-
} = require('./utils.js');
|
|
9
|
-
|
|
10
|
-
function canAttachComment(node) {
|
|
11
|
-
return node.type && !isBlockComment(node) && !isLineComment(node) && node.type !== 'EmptyStatement' && node.type !== 'TemplateElement' && node.type !== 'Import' && node.type !== 'TSEmptyBodyFunctionExpression';
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
module.exports = {
|
|
15
|
-
canAttachComment,
|
|
16
|
-
handleComments: {
|
|
17
|
-
avoidAstMutation: true,
|
|
18
|
-
ownLine: handleComments.handleOwnLineComment,
|
|
19
|
-
endOfLine: handleComments.handleEndOfLineComment,
|
|
20
|
-
remaining: handleComments.handleRemainingComment
|
|
21
|
-
},
|
|
22
|
-
getCommentChildNodes: handleComments.getCommentChildNodes
|
|
23
|
-
};
|