Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

hermes-transform

Package Overview
Dependencies
Maintainers
1
Versions
35
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hermes-transform - npm Package Compare versions

Comparing version 0.5.0 to 0.6.0

dist/transform/mutations/RemoveNode.js

74

dist/detachedNode.js

@@ -16,18 +16,15 @@ "use strict";

function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*
* @format
*/
// used by the node type function codegen
function detachedProps(parent, props) {
// $FlowExpectedError[incompatible-return]
return _objectSpread(_objectSpread({}, props), {}, {
return { ...props,
// if not provided, then we purposely don't set this here

@@ -48,3 +45,3 @@ // and will rely on the tooling to update it as appropriate.

}
});
};
}

@@ -56,4 +53,3 @@ /**

function shallowCloneNode(node) {
var newProps = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
function shallowCloneNode(node, newProps = {}) {
return detachedProps(null, Object.assign({}, node, newProps));

@@ -66,5 +62,4 @@ }

function deepCloneNode(node) {
var newProps = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var clone = Object.assign(JSON.parse(JSON.stringify(node, function (key, value) {
function deepCloneNode(node, newProps = {}) {
const clone = Object.assign(JSON.parse(JSON.stringify(node, (key, value) => {
// null out parent pointers

@@ -87,32 +82,11 @@ if (key === 'parent') {

function setParentPointersInDirectChildren(node) {
var _iterator = _createForOfIteratorHelper((0, _getVisitorKeys.getVisitorKeys)(node)),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var key = _step.value;
if ((0, _getVisitorKeys.isNode)( // $FlowExpectedError[prop-missing]
node[key])) {
node[key].parent = node;
} else if (Array.isArray(node[key])) {
var _iterator2 = _createForOfIteratorHelper(node[key]),
_step2;
try {
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
var child = _step2.value;
child.parent = node;
}
} catch (err) {
_iterator2.e(err);
} finally {
_iterator2.f();
}
for (const key of (0, _getVisitorKeys.getVisitorKeys)(node)) {
if ((0, _getVisitorKeys.isNode)( // $FlowExpectedError[prop-missing]
node[key])) {
node[key].parent = node;
} else if (Array.isArray(node[key])) {
for (const child of node[key]) {
child.parent = node;
}
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}

@@ -127,8 +101,10 @@ }

_SimpleTraverser.SimpleTraverser.traverse(node, {
enter: function enter(node, parent) {
enter(node, parent) {
// $FlowExpectedError[cannot-write]
node.parent = parent;
},
leave: function leave() {}
leave() {}
});
}

@@ -7,2 +7,3 @@ "use strict";

exports.ArrowFunctionExpression = ArrowFunctionExpression;
exports.BigIntLiteral = BigIntLiteral;
exports.BlockComment = BlockComment;

@@ -20,27 +21,28 @@ exports.BooleanLiteral = BooleanLiteral;

var _excluded = ["parent"],
_excluded2 = ["tail", "parent"],
_excluded3 = ["parent", "optional", "typeAnnotation"],
_excluded4 = ["parent"];
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*
* @format
*/
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function ArrowFunctionExpression(_ref) {
var parent = _ref.parent,
props = _objectWithoutProperties(_ref, _excluded);
var node = (0, _detachedNode.detachedProps)(parent, _objectSpread({
/*
These are a number of special-case node creation functions that we can't auto-generate.
The list of exported functions here must be kept in sync with the `NODES_WITH_SPECIAL_HANDLING`
list in `scripts/genTransformNodeTypes` to ensure there's no duplicates
*/
function ArrowFunctionExpression({
parent,
...props
}) {
const node = (0, _detachedNode.detachedProps)(parent, {
type: 'ArrowFunctionExpression',
id: null,
// $FlowExpectedError[incompatible-use]
expression: props.body.type !== 'BlockStatement'
}, props));
expression: props.body.type !== 'BlockStatement',
...props
});
(0, _detachedNode.setParentPointersInDirectChildren)(node);

@@ -52,14 +54,15 @@ return node;

function RegExpLiteral(_ref2) {
var pattern = _ref2.pattern,
flags = _ref2.flags,
parent = _ref2.parent;
var value = new RegExp(pattern, flags);
function RegExpLiteral({
pattern,
flags,
parent
}) {
const value = new RegExp(pattern, flags);
return (0, _detachedNode.detachedProps)(parent, {
type: 'Literal',
value: value,
value,
raw: value.toString(),
regex: {
pattern: pattern,
flags: flags
pattern,
flags
}

@@ -70,11 +73,11 @@ });

function TemplateElement(_ref3) {
var tail = _ref3.tail,
parent = _ref3.parent,
value = _objectWithoutProperties(_ref3, _excluded2);
function TemplateElement({
tail,
parent,
...value
}) {
return (0, _detachedNode.detachedProps)(parent, {
type: 'TemplateElement',
tail: tail,
value: value
tail,
value
});

@@ -85,15 +88,14 @@ } // Identifier has a bunch of stuff that usually you don't want to provide - so we have

function Identifier(_ref4) {
var parent = _ref4.parent,
_ref4$optional = _ref4.optional,
optional = _ref4$optional === void 0 ? false : _ref4$optional,
_ref4$typeAnnotation = _ref4.typeAnnotation,
typeAnnotation = _ref4$typeAnnotation === void 0 ? null : _ref4$typeAnnotation,
props = _objectWithoutProperties(_ref4, _excluded3);
var node = (0, _detachedNode.detachedProps)(parent, _objectSpread({
function Identifier({
parent,
optional = false,
typeAnnotation = null,
...props
}) {
const node = (0, _detachedNode.detachedProps)(parent, {
type: 'Identifier',
optional: optional,
typeAnnotation: typeAnnotation
}, props));
optional,
typeAnnotation,
...props
});
(0, _detachedNode.setParentPointersInDirectChildren)(node);

@@ -106,29 +108,45 @@ return node;

function BooleanLiteral(_ref5) {
var parent = _ref5.parent,
value = _ref5.value;
function BigIntLiteral({
parent,
...props
}) {
var _props$raw;
const node = (0, _detachedNode.detachedProps)(parent, {
type: 'Literal',
...props,
raw: (_props$raw = props.raw) != null ? _props$raw : `${props.value}n`,
bigint: `${props.value}`
});
(0, _detachedNode.setParentPointersInDirectChildren)(node);
return node;
}
function BooleanLiteral({
parent,
value
}) {
return (0, _detachedNode.detachedProps)(parent, {
type: 'Literal',
raw: value ? 'true' : 'false',
value: value
value
});
}
function NumericLiteral(_ref6) {
var _props$raw;
function NumericLiteral({
parent,
...props
}) {
var _props$raw2;
var parent = _ref6.parent,
props = _objectWithoutProperties(_ref6, _excluded4);
return (0, _detachedNode.detachedProps)(parent, _objectSpread(_objectSpread({
type: 'Literal'
}, props), {}, {
raw: (_props$raw = props.raw) !== null && _props$raw !== void 0 ? _props$raw : "".concat(props.value)
}));
return (0, _detachedNode.detachedProps)(parent, {
type: 'Literal',
...props,
raw: (_props$raw2 = props.raw) != null ? _props$raw2 : `${props.value}`
});
}
function NullLiteral() {
var _ref7 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
parent = _ref7.parent;
function NullLiteral({
parent
} = {}) {
return (0, _detachedNode.detachedProps)(parent, {

@@ -141,17 +159,18 @@ type: 'Literal',

function StringLiteral(_ref8) {
var parent = _ref8.parent,
rawIn = _ref8.raw,
value = _ref8.value;
var hasSingleQuote = value.includes('"');
var hasDoubleQuote = value.includes("'");
var raw = rawIn;
function StringLiteral({
parent,
raw: rawIn,
value
}) {
const hasSingleQuote = value.includes('"');
const hasDoubleQuote = value.includes("'");
let raw = rawIn;
if (raw == null) {
if (hasSingleQuote && hasDoubleQuote) {
raw = "'".concat(value.replace(/'/g, "\\'"), "'");
raw = `'${value.replace(/'/g, "\\'")}'`;
} else if (hasSingleQuote) {
raw = "\"".concat(value, "\"");
raw = `"${value}"`;
} else {
raw = "'".concat(value, "'");
raw = `'${value}'`;
}

@@ -162,9 +181,10 @@ }

type: 'Literal',
raw: raw,
value: value
raw,
value
});
}
function LineComment(_ref9) {
var value = _ref9.value;
function LineComment({
value
}) {
// $FlowExpectedError[prop-missing]

@@ -174,8 +194,9 @@ // $FlowExpectedError[incompatible-return]

type: 'Line',
value: value
value
});
}
function BlockComment(_ref10) {
var value = _ref10.value;
function BlockComment({
value
}) {
// $FlowExpectedError[prop-missing]

@@ -185,4 +206,4 @@ // $FlowExpectedError[incompatible-return]

type: 'Block',
value: value
value
});
}
/**
* Copyright (c) Facebook, Inc. and its affiliates.
* Copyright (c) Meta Platforms, Inc. and affiliates.
*

@@ -20,13 +20,11 @@ * This source code is licensed under the MIT license found in the

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function isNode(thing) {
return _typeof(thing) === 'object' && thing != null && typeof thing.type === 'string';
return typeof thing === 'object' && thing != null && typeof thing.type === 'string';
}
function getVisitorKeys(node) {
var keys = _hermesEslint.VisitorKeys[node.type];
const keys = _hermesEslint.VisitorKeys[node.type];
if (keys == null) {
throw new Error("No visitor keys found for node type \"".concat(node.type, "\"."));
throw new Error(`No visitor keys found for node type "${node.type}".`);
} // $FlowExpectedError[prop-missing]

@@ -33,0 +31,0 @@

/**
* Copyright (c) Facebook, Inc. and its affiliates.
* Copyright (c) Meta Platforms, Inc. and affiliates.
*

@@ -12,25 +12,20 @@ * This source code is licensed under the MIT license found in the

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.t = void 0;
Object.defineProperty(exports, "transform", {
enumerable: true,
get: function get() {
return _transform.transform;
}
});
Object.defineProperty(exports, "traverse", {
enumerable: true,
get: function get() {
return _traverse.traverse;
}
});
exports.traverseWithContext = exports.traverse = exports.transform = exports.t = exports.SimpleTraverser = void 0;
var _SimpleTraverser = require("./traverse/SimpleTraverser");
exports.SimpleTraverser = _SimpleTraverser.SimpleTraverser;
var _traverse = require("./traverse/traverse");
exports.traverse = _traverse.traverse;
exports.traverseWithContext = _traverse.traverseWithContext;
var _transform = require("./transform/transform");
exports.transform = _transform.transform;
var _t = _interopRequireWildcard(require("./generated/node-types"));

@@ -40,4 +35,4 @@

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
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); }
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; }
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; }

@@ -27,16 +27,16 @@ "use strict";

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*
* @format
*/
// $FlowExpectedError[untyped-import]
// $FlowExpectedError[untyped-import]
// $FlowExpectedError[untyped-import]
function attachComments(comments, ast, text) {

@@ -46,3 +46,3 @@ (0, _comments.attach)(comments, ast, text, {

locEnd: _loc.locEnd,
printer: _printerEstree["default"]
printer: _printerEstree.default
});

@@ -62,8 +62,8 @@ }

function addCommentsToNode(node, comments) {
var _node$comments, _ref;
var _node$comments;
// $FlowExpectedError - this property is secretly added by prettier.
node.comments = (_node$comments = node.comments) !== null && _node$comments !== void 0 ? _node$comments : []; // $FlowExpectedError
node.comments = (_node$comments = node.comments) != null ? _node$comments : []; // $FlowExpectedError
(_ref = node.comments).push.apply(_ref, _toConsumableArray(comments));
node.comments.push(...comments);
}

@@ -75,3 +75,3 @@

// $FlowExpectedError - this property is secretly added by prettier.
return (_node$comments2 = node.comments) !== null && _node$comments2 !== void 0 ? _node$comments2 : [];
return (_node$comments2 = node.comments) != null ? _node$comments2 : [];
}

@@ -124,19 +124,19 @@

// source code or else prettier will slice nothing and bork up the transform
var commentText = comment.value;
let commentText = comment.value;
switch (comment.type) {
case 'Block':
commentText = "/*".concat(commentText, "*/");
commentText = `/*${commentText}*/`;
break;
case 'Line':
commentText = "//".concat(commentText);
commentText = `//${commentText}`;
break;
}
var newCode = code;
let newCode = code;
newCode += '\n';
var start = newCode.length;
const start = newCode.length;
newCode += commentText;
var end = newCode.length; // $FlowExpectedError[cannot-write]
const end = newCode.length; // $FlowExpectedError[cannot-write]

@@ -143,0 +143,0 @@ comment.range = [start, end];

/**
* Copyright (c) Facebook, Inc. and its affiliates.
* Copyright (c) Meta Platforms, Inc. and affiliates.
*

@@ -11,9 +11,7 @@ * This source code is licensed under the MIT license found in the

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
const stringWidth = require('string-width');
var stringWidth = require('string-width');
const getLast = require('../utils/get-last.js');
var getLast = require('../utils/get-last.js');
var notAsciiRegex = /[^\x20-\x7F]/;
const notAsciiRegex = /[^\x20-\x7F]/;
/**

@@ -29,4 +27,4 @@ * @typedef {{backwards?: boolean}} SkipOptions

function skip(chars) {
return function (text, index, opts) {
var backwards = opts && opts.backwards; // Allow `skip` functions to be threaded together without having
return (text, index, opts) => {
const backwards = opts && opts.backwards; // Allow `skip` functions to be threaded together without having
// to check for failures (did someone say monads?).

@@ -40,7 +38,9 @@

var length = text.length;
var cursor = index;
const {
length
} = text;
let cursor = index;
while (cursor >= 0 && cursor < length) {
var c = text.charAt(cursor);
const c = text.charAt(cursor);

@@ -74,3 +74,3 @@ if (chars instanceof RegExp) {

var skipWhitespace = skip(/\s/);
const skipWhitespace = skip(/\s/);
/**

@@ -80,3 +80,3 @@ * @type {(text: string, index: number | false, opts?: SkipOptions) => number | false}

var skipSpaces = skip(' \t');
const skipSpaces = skip(' \t');
/**

@@ -86,3 +86,3 @@ * @type {(text: string, index: number | false, opts?: SkipOptions) => number | false}

var skipToLineEnd = skip(',; \t');
const skipToLineEnd = skip(',; \t');
/**

@@ -92,3 +92,3 @@ * @type {(text: string, index: number | false, opts?: SkipOptions) => number | false}

var skipEverythingButNewLine = skip(/[^\n\r]/);
const skipEverythingButNewLine = skip(/[^\n\r]/);
/**

@@ -107,3 +107,3 @@ * @param {string} text

if (text.charAt(index) === '/' && text.charAt(index + 1) === '*') {
for (var i = index + 2; i < text.length; ++i) {
for (let i = index + 2; i < text.length; ++i) {
if (text.charAt(i) === '*' && text.charAt(i + 1) === '/') {

@@ -148,3 +148,3 @@ return i + 2;

function skipNewline(text, index, opts) {
var backwards = opts && opts.backwards;
const backwards = opts && opts.backwards;

@@ -155,3 +155,3 @@ if (index === false) {

var atIndex = text.charAt(index);
const atIndex = text.charAt(index);

@@ -166,3 +166,3 @@ if (backwards) {

if (atIndex === '\n' || atIndex === '\r' || atIndex === "\u2028" || atIndex === "\u2029") {
if (atIndex === '\n' || atIndex === '\r' || atIndex === '\u2028' || atIndex === '\u2029') {
return index - 1;

@@ -178,3 +178,3 @@ }

if (atIndex === '\n' || atIndex === '\r' || atIndex === "\u2028" || atIndex === "\u2029") {
if (atIndex === '\n' || atIndex === '\r' || atIndex === '\u2028' || atIndex === '\u2029') {
return index + 1;

@@ -194,6 +194,5 @@ }

function hasNewline(text, index) {
var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
var idx = skipSpaces(text, opts.backwards ? index - 1 : index, opts);
var idx2 = skipNewline(text, idx, opts);
function hasNewline(text, index, opts = {}) {
const idx = skipSpaces(text, opts.backwards ? index - 1 : index, opts);
const idx2 = skipNewline(text, idx, opts);
return idx !== idx2;

@@ -210,3 +209,3 @@ }

function hasNewlineInRange(text, start, end) {
for (var i = start; i < end; ++i) {
for (let i = start; i < end; ++i) {
if (text.charAt(i) === '\n') {

@@ -228,6 +227,6 @@ return true;

/** @type {number | false} */
var oldIdx = null;
let oldIdx = null;
/** @type {number | false} */
var idx = index;
let idx = index;

@@ -255,6 +254,6 @@ while (idx !== oldIdx) {

/** @type {number | false} */
var oldIdx = null;
let oldIdx = null;
/** @type {number | false} */
var nextIdx = idx;
let nextIdx = idx;

@@ -316,3 +315,3 @@ while (nextIdx !== oldIdx) {

function addCommentHelper(node, comment) {
var comments = node.comments || (node.comments = []);
const comments = node.comments || (node.comments = []);
comments.push(comment);

@@ -356,4 +355,4 @@ comment.printed = false;

function describeNodeForDebugging(node) {
var nodeType = node.type || node.kind || '(unknown type)';
var nodeName = String(node.name || node.id && (_typeof(node.id) === 'object' ? node.id.name : node.id) || node.key && (_typeof(node.key) === 'object' ? node.key.name : node.key) || node.value && (_typeof(node.value) === 'object' ? '' : String(node.value)) || node.operator || '');
const nodeType = node.type || node.kind || '(unknown type)';
let nodeName = String(node.name || node.id && (typeof node.id === 'object' ? node.id.name : node.id) || node.key && (typeof node.key === 'object' ? node.key.name : node.key) || node.value && (typeof node.value === 'object' ? '' : String(node.value)) || node.operator || '');

@@ -368,17 +367,17 @@ if (nodeName.length > 20) {

module.exports = {
getStringWidth: getStringWidth,
getLast: getLast,
getNextNonSpaceNonCommentCharacterIndexWithStartIndex: getNextNonSpaceNonCommentCharacterIndexWithStartIndex,
getNextNonSpaceNonCommentCharacterIndex: getNextNonSpaceNonCommentCharacterIndex,
getNextNonSpaceNonCommentCharacter: getNextNonSpaceNonCommentCharacter,
skipWhitespace: skipWhitespace,
skipSpaces: skipSpaces,
skipNewline: skipNewline,
isNextLineEmptyAfterIndex: isNextLineEmptyAfterIndex,
hasNewline: hasNewline,
hasNewlineInRange: hasNewlineInRange,
addLeadingComment: addLeadingComment,
addDanglingComment: addDanglingComment,
addTrailingComment: addTrailingComment,
isNonEmptyArray: isNonEmptyArray
getStringWidth,
getLast,
getNextNonSpaceNonCommentCharacterIndexWithStartIndex,
getNextNonSpaceNonCommentCharacterIndex,
getNextNonSpaceNonCommentCharacter,
skipWhitespace,
skipSpaces,
skipNewline,
isNextLineEmptyAfterIndex,
hasNewline,
hasNewlineInRange,
addLeadingComment,
addDanglingComment,
addTrailingComment,
isNonEmptyArray
};
/**
* Copyright (c) Facebook, Inc. and its affiliates.
* Copyright (c) Meta Platforms, Inc. and affiliates.
*

@@ -11,39 +11,30 @@ * This source code is licensed under the MIT license found in the

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
const {
getLast,
hasNewline,
addLeadingComment,
getNextNonSpaceNonCommentCharacterIndexWithStartIndex,
getNextNonSpaceNonCommentCharacterIndex,
hasNewlineInRange,
addTrailingComment,
addDanglingComment,
getNextNonSpaceNonCommentCharacter,
isNonEmptyArray
} = require('../common/util.js');
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
const {
isBlockComment,
getFunctionParameters,
isPrettierIgnoreComment,
isCallLikeExpression,
getCallArguments,
isCallExpression,
isMemberExpression,
isObjectProperty
} = require('./utils.js');
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
var _require = require('../common/util.js'),
getLast = _require.getLast,
hasNewline = _require.hasNewline,
addLeadingComment = _require.addLeadingComment,
getNextNonSpaceNonCommentCharacterIndexWithStartIndex = _require.getNextNonSpaceNonCommentCharacterIndexWithStartIndex,
getNextNonSpaceNonCommentCharacterIndex = _require.getNextNonSpaceNonCommentCharacterIndex,
hasNewlineInRange = _require.hasNewlineInRange,
addTrailingComment = _require.addTrailingComment,
addDanglingComment = _require.addDanglingComment,
getNextNonSpaceNonCommentCharacter = _require.getNextNonSpaceNonCommentCharacter,
isNonEmptyArray = _require.isNonEmptyArray;
var _require2 = require('./utils.js'),
isBlockComment = _require2.isBlockComment,
getFunctionParameters = _require2.getFunctionParameters,
isPrettierIgnoreComment = _require2.isPrettierIgnoreComment,
isCallLikeExpression = _require2.isCallLikeExpression,
getCallArguments = _require2.getCallArguments,
isCallExpression = _require2.isCallExpression,
isMemberExpression = _require2.isMemberExpression,
isObjectProperty = _require2.isObjectProperty;
var _require3 = require('./loc.js'),
locStart = _require3.locStart,
locEnd = _require3.locEnd;
const {
locStart,
locEnd
} = require('./loc.js');
/**

@@ -72,5 +63,3 @@ * @typedef {import("./types/estree").Node} Node

function handleOwnLineComment(context) {
return [handleIgnoreComments, handleLastFunctionArgComments, handleMemberExpressionComments, handleIfStatementComments, handleWhileComments, handleTryStatementComments, handleClassComments, handleImportSpecifierComments, handleForComments, handleUnionTypeComments, handleOnlyComments, handleImportDeclarationComments, handleAssignmentPatternComments, handleMethodNameComments, handleLabeledStatementComments].some(function (fn) {
return fn(context);
});
return [handleIgnoreComments, handleLastFunctionArgComments, handleMemberExpressionComments, handleIfStatementComments, handleWhileComments, handleTryStatementComments, handleClassComments, handleImportSpecifierComments, handleForComments, handleUnionTypeComments, handleOnlyComments, handleImportDeclarationComments, handleAssignmentPatternComments, handleMethodNameComments, handleLabeledStatementComments].some(fn => fn(context));
}

@@ -84,5 +73,3 @@ /**

function handleEndOfLineComment(context) {
return [handleClosureTypeCastComments, handleLastFunctionArgComments, handleConditionalExpressionComments, handleImportSpecifierComments, handleIfStatementComments, handleWhileComments, handleTryStatementComments, handleClassComments, handleLabeledStatementComments, handleCallExpressionComments, handlePropertyComments, handleOnlyComments, handleTypeAliasComments, handleVariableDeclaratorComments].some(function (fn) {
return fn(context);
});
return [handleClosureTypeCastComments, handleLastFunctionArgComments, handleConditionalExpressionComments, handleImportSpecifierComments, handleIfStatementComments, handleWhileComments, handleTryStatementComments, handleClassComments, handleLabeledStatementComments, handleCallExpressionComments, handlePropertyComments, handleOnlyComments, handleTypeAliasComments, handleVariableDeclaratorComments].some(fn => fn(context));
}

@@ -96,5 +83,3 @@ /**

function handleRemainingComment(context) {
return [handleIgnoreComments, handleIfStatementComments, handleWhileComments, handleObjectPropertyAssignment, handleCommentInEmptyParens, handleMethodNameComments, handleOnlyComments, handleCommentAfterArrowParams, handleFunctionNameComments, handleTSMappedTypeComments, handleBreakAndContinueStatementComments, handleTSFunctionTrailingComments].some(function (fn) {
return fn(context);
});
return [handleIgnoreComments, handleIfStatementComments, handleWhileComments, handleObjectPropertyAssignment, handleCommentInEmptyParens, handleMethodNameComments, handleOnlyComments, handleCommentAfterArrowParams, handleFunctionNameComments, handleTSMappedTypeComments, handleBreakAndContinueStatementComments, handleTSFunctionTrailingComments].some(fn => fn(context));
}

@@ -109,6 +94,5 @@ /**

// @ts-expect-error
var firstNonEmptyNode = (node.body || node.properties).find(function (_ref) {
var type = _ref.type;
return type !== 'EmptyStatement';
});
const firstNonEmptyNode = (node.body || node.properties).find(({
type
}) => type !== 'EmptyStatement');

@@ -135,6 +119,6 @@ if (firstNonEmptyNode) {

function handleClosureTypeCastComments(_ref2) {
var comment = _ref2.comment,
followingNode = _ref2.followingNode;
function handleClosureTypeCastComments({
comment,
followingNode
}) {
if (followingNode && isTypeCastComment(comment)) {

@@ -164,9 +148,9 @@ addLeadingComment(followingNode, comment);

function handleIfStatementComments(_ref3) {
var comment = _ref3.comment,
precedingNode = _ref3.precedingNode,
enclosingNode = _ref3.enclosingNode,
followingNode = _ref3.followingNode,
text = _ref3.text;
function handleIfStatementComments({
comment,
precedingNode,
enclosingNode,
followingNode,
text
}) {
if (!enclosingNode || enclosingNode.type !== 'IfStatement' || !followingNode) {

@@ -181,3 +165,3 @@ return false;

var nextCharacter = getNextNonSpaceNonCommentCharacter(text, comment, locEnd);
const nextCharacter = getNextNonSpaceNonCommentCharacter(text, comment, locEnd);

@@ -225,9 +209,9 @@ if (nextCharacter === ')') {

function handleWhileComments(_ref4) {
var comment = _ref4.comment,
precedingNode = _ref4.precedingNode,
enclosingNode = _ref4.enclosingNode,
followingNode = _ref4.followingNode,
text = _ref4.text;
function handleWhileComments({
comment,
precedingNode,
enclosingNode,
followingNode,
text
}) {
if (!enclosingNode || enclosingNode.type !== 'WhileStatement' || !followingNode) {

@@ -242,3 +226,3 @@ return false;

var nextCharacter = getNextNonSpaceNonCommentCharacter(text, comment, locEnd);
const nextCharacter = getNextNonSpaceNonCommentCharacter(text, comment, locEnd);

@@ -264,8 +248,8 @@ if (nextCharacter === ')') {

function handleTryStatementComments(_ref5) {
var comment = _ref5.comment,
precedingNode = _ref5.precedingNode,
enclosingNode = _ref5.enclosingNode,
followingNode = _ref5.followingNode;
function handleTryStatementComments({
comment,
precedingNode,
enclosingNode,
followingNode
}) {
if (!enclosingNode || enclosingNode.type !== 'TryStatement' && enclosingNode.type !== 'CatchClause' || !followingNode) {

@@ -298,7 +282,7 @@ return false;

function handleMemberExpressionComments(_ref6) {
var comment = _ref6.comment,
enclosingNode = _ref6.enclosingNode,
followingNode = _ref6.followingNode;
function handleMemberExpressionComments({
comment,
enclosingNode,
followingNode
}) {
if (isMemberExpression(enclosingNode) && followingNode && followingNode.type === 'Identifier') {

@@ -312,9 +296,10 @@ addLeadingComment(enclosingNode, comment);

function handleConditionalExpressionComments(_ref7) {
var comment = _ref7.comment,
precedingNode = _ref7.precedingNode,
enclosingNode = _ref7.enclosingNode,
followingNode = _ref7.followingNode,
text = _ref7.text;
var isSameLineAsPrecedingNode = precedingNode && !hasNewlineInRange(text, locEnd(precedingNode), locStart(comment));
function handleConditionalExpressionComments({
comment,
precedingNode,
enclosingNode,
followingNode,
text
}) {
const isSameLineAsPrecedingNode = precedingNode && !hasNewlineInRange(text, locEnd(precedingNode), locStart(comment));

@@ -329,7 +314,7 @@ if ((!precedingNode || !isSameLineAsPrecedingNode) && enclosingNode && (enclosingNode.type === 'ConditionalExpression' || enclosingNode.type === 'TSConditionalType') && followingNode) {

function handleObjectPropertyAssignment(_ref8) {
var comment = _ref8.comment,
precedingNode = _ref8.precedingNode,
enclosingNode = _ref8.enclosingNode;
function handleObjectPropertyAssignment({
comment,
precedingNode,
enclosingNode
}) {
if (isObjectProperty(enclosingNode) && enclosingNode.shorthand && enclosingNode.key === precedingNode && enclosingNode.value.type === 'AssignmentPattern') {

@@ -343,8 +328,8 @@ addTrailingComment(enclosingNode.value.left, comment);

function handleClassComments(_ref9) {
var comment = _ref9.comment,
precedingNode = _ref9.precedingNode,
enclosingNode = _ref9.enclosingNode,
followingNode = _ref9.followingNode;
function handleClassComments({
comment,
precedingNode,
enclosingNode,
followingNode
}) {
if (enclosingNode && (enclosingNode.type === 'ClassDeclaration' || enclosingNode.type === 'ClassExpression' || enclosingNode.type === 'DeclareClass' || enclosingNode.type === 'DeclareInterface' || enclosingNode.type === 'InterfaceDeclaration' || enclosingNode.type === 'TSInterfaceDeclaration')) {

@@ -364,5 +349,3 @@ if (isNonEmptyArray(enclosingNode.decorators) && !(followingNode && followingNode.type === 'Decorator')) {

if (followingNode) {
for (var _i = 0, _arr = ['implements', 'extends', 'mixins']; _i < _arr.length; _i++) {
var prop = _arr[_i];
for (const prop of ['implements', 'extends', 'mixins']) {
if (enclosingNode[prop] && followingNode === enclosingNode[prop][0]) {

@@ -384,8 +367,8 @@ if (precedingNode && (precedingNode === enclosingNode.id || precedingNode === enclosingNode.typeParameters || precedingNode === enclosingNode.superClass)) {

function handleMethodNameComments(_ref10) {
var comment = _ref10.comment,
precedingNode = _ref10.precedingNode,
enclosingNode = _ref10.enclosingNode,
text = _ref10.text;
function handleMethodNameComments({
comment,
precedingNode,
enclosingNode,
text
}) {
// This is only needed for estree parsers (flow, typescript) to attach

@@ -412,8 +395,8 @@ // after a method name:

function handleFunctionNameComments(_ref11) {
var comment = _ref11.comment,
precedingNode = _ref11.precedingNode,
enclosingNode = _ref11.enclosingNode,
text = _ref11.text;
function handleFunctionNameComments({
comment,
precedingNode,
enclosingNode,
text
}) {
if (getNextNonSpaceNonCommentCharacter(text, comment, locEnd) !== '(') {

@@ -431,7 +414,7 @@ return false;

function handleCommentAfterArrowParams(_ref12) {
var comment = _ref12.comment,
enclosingNode = _ref12.enclosingNode,
text = _ref12.text;
function handleCommentAfterArrowParams({
comment,
enclosingNode,
text
}) {
if (!(enclosingNode && enclosingNode.type === 'ArrowFunctionExpression')) {

@@ -441,3 +424,3 @@ return false;

var index = getNextNonSpaceNonCommentCharacterIndex(text, comment, locEnd);
const index = getNextNonSpaceNonCommentCharacterIndex(text, comment, locEnd);

@@ -452,7 +435,7 @@ if (index !== false && text.slice(index, index + 2) === '=>') {

function handleCommentInEmptyParens(_ref13) {
var comment = _ref13.comment,
enclosingNode = _ref13.enclosingNode,
text = _ref13.text;
function handleCommentInEmptyParens({
comment,
enclosingNode,
text
}) {
if (getNextNonSpaceNonCommentCharacter(text, comment, locEnd) !== ')') {

@@ -477,9 +460,9 @@ return false;

function handleLastFunctionArgComments(_ref14) {
var comment = _ref14.comment,
precedingNode = _ref14.precedingNode,
enclosingNode = _ref14.enclosingNode,
followingNode = _ref14.followingNode,
text = _ref14.text;
function handleLastFunctionArgComments({
comment,
precedingNode,
enclosingNode,
followingNode,
text
}) {
// Flow function type definitions

@@ -498,4 +481,4 @@ if (precedingNode && precedingNode.type === 'FunctionTypeParam' && enclosingNode && enclosingNode.type === 'FunctionTypeAnnotation' && followingNode && followingNode.type !== 'FunctionTypeParam') {

if (enclosingNode && enclosingNode.type === 'FunctionDeclaration' && followingNode && followingNode.type === 'BlockStatement') {
var functionParamRightParenIndex = function () {
var parameters = getFunctionParameters(enclosingNode);
const functionParamRightParenIndex = (() => {
const parameters = getFunctionParameters(enclosingNode);

@@ -506,5 +489,5 @@ if (parameters.length > 0) {

var functionParamLeftParenIndex = getNextNonSpaceNonCommentCharacterIndexWithStartIndex(text, locEnd(enclosingNode.id));
const functionParamLeftParenIndex = getNextNonSpaceNonCommentCharacterIndexWithStartIndex(text, locEnd(enclosingNode.id));
return functionParamLeftParenIndex !== false && getNextNonSpaceNonCommentCharacterIndexWithStartIndex(text, functionParamLeftParenIndex + 1);
}();
})();

@@ -520,6 +503,6 @@ if (locStart(comment) > functionParamRightParenIndex) {

function handleImportSpecifierComments(_ref15) {
var comment = _ref15.comment,
enclosingNode = _ref15.enclosingNode;
function handleImportSpecifierComments({
comment,
enclosingNode
}) {
if (enclosingNode && enclosingNode.type === 'ImportSpecifier') {

@@ -533,6 +516,6 @@ addLeadingComment(enclosingNode, comment);

function handleLabeledStatementComments(_ref16) {
var comment = _ref16.comment,
enclosingNode = _ref16.enclosingNode;
function handleLabeledStatementComments({
comment,
enclosingNode
}) {
if (enclosingNode && enclosingNode.type === 'LabeledStatement') {

@@ -546,6 +529,6 @@ addLeadingComment(enclosingNode, comment);

function handleBreakAndContinueStatementComments(_ref17) {
var comment = _ref17.comment,
enclosingNode = _ref17.enclosingNode;
function handleBreakAndContinueStatementComments({
comment,
enclosingNode
}) {
if (enclosingNode && (enclosingNode.type === 'ContinueStatement' || enclosingNode.type === 'BreakStatement') && !enclosingNode.label) {

@@ -559,7 +542,7 @@ addTrailingComment(enclosingNode, comment);

function handleCallExpressionComments(_ref18) {
var comment = _ref18.comment,
precedingNode = _ref18.precedingNode,
enclosingNode = _ref18.enclosingNode;
function handleCallExpressionComments({
comment,
precedingNode,
enclosingNode
}) {
if (isCallExpression(enclosingNode) && precedingNode && enclosingNode.callee === precedingNode && enclosingNode.arguments.length > 0) {

@@ -573,8 +556,8 @@ addLeadingComment(enclosingNode.arguments[0], comment);

function handleUnionTypeComments(_ref19) {
var comment = _ref19.comment,
precedingNode = _ref19.precedingNode,
enclosingNode = _ref19.enclosingNode,
followingNode = _ref19.followingNode;
function handleUnionTypeComments({
comment,
precedingNode,
enclosingNode,
followingNode
}) {
if (enclosingNode && (enclosingNode.type === 'UnionTypeAnnotation' || enclosingNode.type === 'TSUnionType')) {

@@ -602,6 +585,6 @@ if (isPrettierIgnoreComment(comment)) {

function handlePropertyComments(_ref20) {
var comment = _ref20.comment,
enclosingNode = _ref20.enclosingNode;
function handlePropertyComments({
comment,
enclosingNode
}) {
if (isObjectProperty(enclosingNode)) {

@@ -615,9 +598,9 @@ addLeadingComment(enclosingNode, comment);

function handleOnlyComments(_ref21) {
var comment = _ref21.comment,
enclosingNode = _ref21.enclosingNode,
followingNode = _ref21.followingNode,
ast = _ref21.ast,
isLastComment = _ref21.isLastComment;
function handleOnlyComments({
comment,
enclosingNode,
followingNode,
ast,
isLastComment
}) {
// With Flow the enclosingNode is undefined so use the AST instead.

@@ -652,6 +635,6 @@ if (ast && ast.body && ast.body.length === 0) {

function handleForComments(_ref22) {
var comment = _ref22.comment,
enclosingNode = _ref22.enclosingNode;
function handleForComments({
comment,
enclosingNode
}) {
if (enclosingNode && (enclosingNode.type === 'ForInStatement' || enclosingNode.type === 'ForOfStatement')) {

@@ -665,8 +648,8 @@ addLeadingComment(enclosingNode, comment);

function handleImportDeclarationComments(_ref23) {
var comment = _ref23.comment,
precedingNode = _ref23.precedingNode,
enclosingNode = _ref23.enclosingNode,
text = _ref23.text;
function handleImportDeclarationComments({
comment,
precedingNode,
enclosingNode,
text
}) {
if (precedingNode && precedingNode.type === 'ImportSpecifier' && enclosingNode && enclosingNode.type === 'ImportDeclaration' && hasNewline(text, locEnd(comment))) {

@@ -680,6 +663,6 @@ addTrailingComment(precedingNode, comment);

function handleAssignmentPatternComments(_ref24) {
var comment = _ref24.comment,
enclosingNode = _ref24.enclosingNode;
function handleAssignmentPatternComments({
comment,
enclosingNode
}) {
if (enclosingNode && enclosingNode.type === 'AssignmentPattern') {

@@ -693,6 +676,6 @@ addLeadingComment(enclosingNode, comment);

function handleTypeAliasComments(_ref25) {
var comment = _ref25.comment,
enclosingNode = _ref25.enclosingNode;
function handleTypeAliasComments({
comment,
enclosingNode
}) {
if (enclosingNode && enclosingNode.type === 'TypeAlias') {

@@ -706,7 +689,7 @@ addLeadingComment(enclosingNode, comment);

function handleVariableDeclaratorComments(_ref26) {
var comment = _ref26.comment,
enclosingNode = _ref26.enclosingNode,
followingNode = _ref26.followingNode;
function handleVariableDeclaratorComments({
comment,
enclosingNode,
followingNode
}) {
if (enclosingNode && (enclosingNode.type === 'VariableDeclarator' || enclosingNode.type === 'AssignmentExpression') && followingNode && (followingNode.type === 'ObjectExpression' || followingNode.type === 'ArrayExpression' || followingNode.type === 'TemplateLiteral' || followingNode.type === 'TaggedTemplateExpression' || isBlockComment(comment))) {

@@ -720,8 +703,8 @@ addLeadingComment(followingNode, comment);

function handleTSFunctionTrailingComments(_ref27) {
var comment = _ref27.comment,
enclosingNode = _ref27.enclosingNode,
followingNode = _ref27.followingNode,
text = _ref27.text;
function handleTSFunctionTrailingComments({
comment,
enclosingNode,
followingNode,
text
}) {
if (!followingNode && enclosingNode && (enclosingNode.type === 'TSMethodSignature' || enclosingNode.type === 'TSDeclareFunction' || enclosingNode.type === 'TSAbstractMethodDefinition') && getNextNonSpaceNonCommentCharacter(text, comment, locEnd) === ';') {

@@ -735,7 +718,7 @@ addTrailingComment(enclosingNode, comment);

function handleIgnoreComments(_ref28) {
var comment = _ref28.comment,
enclosingNode = _ref28.enclosingNode,
followingNode = _ref28.followingNode;
function handleIgnoreComments({
comment,
enclosingNode,
followingNode
}) {
if (isPrettierIgnoreComment(comment) && enclosingNode && enclosingNode.type === 'TSMappedType' && followingNode && followingNode.type === 'TSTypeParameter' && followingNode.constraint) {

@@ -748,8 +731,8 @@ enclosingNode.prettierIgnore = true;

function handleTSMappedTypeComments(_ref29) {
var comment = _ref29.comment,
precedingNode = _ref29.precedingNode,
enclosingNode = _ref29.enclosingNode,
followingNode = _ref29.followingNode;
function handleTSMappedTypeComments({
comment,
precedingNode,
enclosingNode,
followingNode
}) {
if (!enclosingNode || enclosingNode.type !== 'TSMappedType') {

@@ -795,3 +778,3 @@ return false;

if ((options.parser === 'typescript' || options.parser === 'flow' || options.parser === 'espree' || options.parser === 'meriyah' || options.parser === '__babel_estree') && node.type === 'MethodDefinition' && node.value && node.value.type === 'FunctionExpression' && getFunctionParameters(node.value).length === 0 && !node.value.returnType && !isNonEmptyArray(node.value.typeParameters) && node.value.body) {
return [].concat(_toConsumableArray(node.decorators || []), [node.key, node.value.body]);
return [...(node.decorators || []), node.key, node.value.body];
}

@@ -813,6 +796,6 @@ }

module.exports = {
handleOwnLineComment: handleOwnLineComment,
handleEndOfLineComment: handleEndOfLineComment,
handleRemainingComment: handleRemainingComment,
getCommentChildNodes: getCommentChildNodes
handleOwnLineComment,
handleEndOfLineComment,
handleRemainingComment,
getCommentChildNodes
};
/**
* Copyright (c) Facebook, Inc. and its affiliates.
* Copyright (c) Meta Platforms, Inc. and affiliates.
*

@@ -11,4 +11,5 @@ * This source code is licensed under the MIT license found in the

var _require = require('../common/util.js'),
isNonEmptyArray = _require.isNonEmptyArray;
const {
isNonEmptyArray
} = require('../common/util.js');
/**

@@ -20,8 +21,8 @@ * @typedef {import("./types/estree").Node} Node

function locStart(node, opts) {
var _ref = opts || {},
ignoreDecorators = _ref.ignoreDecorators; // Handle nodes with decorators. They should start at the first decorator
const {
ignoreDecorators
} = opts || {}; // Handle nodes with decorators. They should start at the first decorator
if (!ignoreDecorators) {
var decorators = node.declaration && node.declaration.decorators || node.decorators;
const decorators = node.declaration && node.declaration.decorators || node.decorators;

@@ -41,4 +42,4 @@ if (isNonEmptyArray(decorators)) {

module.exports = {
locStart: locStart,
locEnd: locEnd
locStart,
locEnd
};
/**
* Copyright (c) Facebook, Inc. and its affiliates.
* Copyright (c) Meta Platforms, Inc. and affiliates.
*

@@ -11,7 +11,8 @@ * This source code is licensed under the MIT license found in the

var handleComments = require('./comments.js');
const handleComments = require('./comments.js');
var _require = require('./utils.js'),
isBlockComment = _require.isBlockComment,
isLineComment = _require.isLineComment;
const {
isBlockComment,
isLineComment
} = require('./utils.js');

@@ -24,3 +25,3 @@ function canAttachComment(node) {

module.exports = {
canAttachComment: canAttachComment,
canAttachComment,
handleComments: {

@@ -27,0 +28,0 @@ avoidAstMutation: true,

/**
* Copyright (c) Facebook, Inc. and its affiliates.
* Copyright (c) Meta Platforms, Inc. and affiliates.
*

@@ -15,14 +15,2 @@ * This source code is licensed under the MIT license found in the

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function isBlockComment(comment) {

@@ -61,3 +49,3 @@ return comment.type === 'Block' || comment.type === 'CommentBlock' || // `meriyah`

var functionParametersCache = new WeakMap();
const functionParametersCache = new WeakMap();

@@ -69,6 +57,6 @@ function getFunctionParameters(node) {

var parameters = [];
const parameters = [];
if (node["this"]) {
parameters.push(node["this"]);
if (node.this) {
parameters.push(node.this);
} // `params` vs `parameters` - see https://github.com/babel/babel/issues/9231

@@ -78,5 +66,5 @@

if (Array.isArray(node.parameters)) {
parameters.push.apply(parameters, _toConsumableArray(node.parameters));
parameters.push(...node.parameters);
} else if (Array.isArray(node.params)) {
parameters.push.apply(parameters, _toConsumableArray(node.params));
parameters.push(...node.params);
}

@@ -92,3 +80,3 @@

var callArgumentsCache = new WeakMap();
const callArgumentsCache = new WeakMap();

@@ -100,3 +88,3 @@ function getCallArguments(node) {

var args = node.arguments;
let args = node.arguments;

@@ -128,11 +116,11 @@ if (node.type === 'ImportExpression') {

module.exports = {
getFunctionParameters: getFunctionParameters,
getCallArguments: getCallArguments,
isBlockComment: isBlockComment,
isCallLikeExpression: isCallLikeExpression,
isLineComment: isLineComment,
isPrettierIgnoreComment: isPrettierIgnoreComment,
isCallExpression: isCallExpression,
isMemberExpression: isMemberExpression,
isObjectProperty: isObjectProperty
getFunctionParameters,
getCallArguments,
isBlockComment,
isCallLikeExpression,
isLineComment,
isPrettierIgnoreComment,
isCallExpression,
isMemberExpression,
isObjectProperty
};
/**
* Copyright (c) Facebook, Inc. and its affiliates.
* Copyright (c) Meta Platforms, Inc. and affiliates.
*

@@ -11,42 +11,13 @@ * This source code is licensed under the MIT license found in the

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
const assert = require('assert');
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
const {
hasNewline,
addLeadingComment,
addDanglingComment,
addTrailingComment
} = require('../common/util.js');
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
const childNodesCache = new WeakMap();
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e2) { throw _e2; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
var assert = require('assert');
var _require = require('../common/util.js'),
hasNewline = _require.hasNewline,
addLeadingComment = _require.addLeadingComment,
addDanglingComment = _require.addDanglingComment,
addTrailingComment = _require.addTrailingComment;
var childNodesCache = new WeakMap();
function getSortedChildNodes(node, options, resultArray) {

@@ -57,5 +28,7 @@ if (!node) {

var printer = options.printer,
locStart = options.locStart,
locEnd = options.locEnd;
const {
printer,
locStart,
locEnd
} = options;

@@ -67,3 +40,3 @@ if (resultArray) {

// nodes in order anyway.
var i;
let i;

@@ -83,14 +56,4 @@ for (i = resultArray.length - 1; i >= 0; --i) {

var childNodes = printer.getCommentChildNodes && printer.getCommentChildNodes(node, options) || _typeof(node) === 'object' && Object.entries(node).filter(function (_ref) {
var _ref2 = _slicedToArray(_ref, 1),
key = _ref2[0];
const childNodes = printer.getCommentChildNodes && printer.getCommentChildNodes(node, options) || typeof node === 'object' && Object.entries(node).filter(([key]) => key !== 'enclosingNode' && key !== 'precedingNode' && key !== 'followingNode' && key !== 'tokens' && key !== 'comments').map(([, value]) => value);
return key !== 'enclosingNode' && key !== 'precedingNode' && key !== 'followingNode' && key !== 'tokens' && key !== 'comments';
}).map(function (_ref3) {
var _ref4 = _slicedToArray(_ref3, 2),
value = _ref4[1];
return value;
});
if (!childNodes) {

@@ -105,14 +68,4 @@ return;

var _iterator = _createForOfIteratorHelper(childNodes),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var childNode = _step.value;
getSortedChildNodes(childNode, options, resultArray);
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
for (const childNode of childNodes) {
getSortedChildNodes(childNode, options, resultArray);
}

@@ -127,18 +80,20 @@

function decorateComment(node, comment, options, enclosingNode) {
var locStart = options.locStart,
locEnd = options.locEnd;
var commentStart = locStart(comment);
var commentEnd = locEnd(comment);
var childNodes = getSortedChildNodes(node, options);
var precedingNode;
var followingNode; // Time to dust off the old binary search robes and wizard hat.
const {
locStart,
locEnd
} = options;
const commentStart = locStart(comment);
const commentEnd = locEnd(comment);
const childNodes = getSortedChildNodes(node, options);
let precedingNode;
let followingNode; // Time to dust off the old binary search robes and wizard hat.
var left = 0;
var right = childNodes.length;
let left = 0;
let right = childNodes.length;
while (left < right) {
var middle = left + right >> 1;
var child = childNodes[middle];
var start = locStart(child);
var end = locEnd(child); // The comment is completely contained by this child node.
const middle = left + right >> 1;
const child = childNodes[middle];
const start = locStart(child);
const end = locEnd(child); // The comment is completely contained by this child node.

@@ -178,4 +133,6 @@ if (start <= commentStart && commentEnd <= end) {

if (enclosingNode && enclosingNode.type === 'TemplateLiteral') {
var quasis = enclosingNode.quasis;
var commentIndex = findExpressionIndexForComment(quasis, comment, options);
const {
quasis
} = enclosingNode;
const commentIndex = findExpressionIndexForComment(quasis, comment, options);

@@ -192,11 +149,9 @@ if (precedingNode && findExpressionIndexForComment(quasis, precedingNode, options) !== commentIndex) {

return {
enclosingNode: enclosingNode,
precedingNode: precedingNode,
followingNode: followingNode
enclosingNode,
precedingNode,
followingNode
};
}
var returnFalse = function returnFalse() {
return false;
};
const returnFalse = () => false;

@@ -208,141 +163,130 @@ function attach(comments, ast, text, options) {

var tiesToBreak = [];
var locStart = options.locStart,
locEnd = options.locEnd,
_options$printer$hand = options.printer.handleComments,
handleComments = _options$printer$hand === void 0 ? {} : _options$printer$hand; // TODO: Make this as default behavior
const tiesToBreak = [];
const {
locStart,
locEnd,
printer: {
handleComments = {}
}
} = options; // TODO: Make this as default behavior
var avoidAstMutation = handleComments.avoidAstMutation,
_handleComments$ownLi = handleComments.ownLine,
handleOwnLineComment = _handleComments$ownLi === void 0 ? returnFalse : _handleComments$ownLi,
_handleComments$endOf = handleComments.endOfLine,
handleEndOfLineComment = _handleComments$endOf === void 0 ? returnFalse : _handleComments$endOf,
_handleComments$remai = handleComments.remaining,
handleRemainingComment = _handleComments$remai === void 0 ? returnFalse : _handleComments$remai;
var decoratedComments = comments.map(function (comment, index) {
return _objectSpread(_objectSpread({}, decorateComment(ast, comment, options)), {}, {
comment: comment,
text: text,
options: options,
ast: ast,
isLastComment: comments.length - 1 === index
});
});
const {
avoidAstMutation,
ownLine: handleOwnLineComment = returnFalse,
endOfLine: handleEndOfLineComment = returnFalse,
remaining: handleRemainingComment = returnFalse
} = handleComments;
const decoratedComments = comments.map((comment, index) => ({ ...decorateComment(ast, comment, options),
comment,
text,
options,
ast,
isLastComment: comments.length - 1 === index
}));
var _iterator2 = _createForOfIteratorHelper(decoratedComments.entries()),
_step2;
for (const [index, context] of decoratedComments.entries()) {
const {
comment,
precedingNode,
enclosingNode,
followingNode,
text,
options,
ast,
isLastComment
} = context;
try {
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
var _step2$value = _slicedToArray(_step2.value, 2),
index = _step2$value[0],
context = _step2$value[1];
if (options.parser === 'json' || options.parser === 'json5' || options.parser === '__js_expression' || options.parser === '__vue_expression') {
if (locStart(comment) - locStart(ast) <= 0) {
addLeadingComment(ast, comment);
continue;
}
var _comment = context.comment,
precedingNode = context.precedingNode,
enclosingNode = context.enclosingNode,
followingNode = context.followingNode,
_text = context.text,
_options = context.options,
_ast = context.ast,
isLastComment = context.isLastComment;
if (locEnd(comment) - locEnd(ast) >= 0) {
addTrailingComment(ast, comment);
continue;
}
}
if (_options.parser === 'json' || _options.parser === 'json5' || _options.parser === '__js_expression' || _options.parser === '__vue_expression') {
if (locStart(_comment) - locStart(_ast) <= 0) {
addLeadingComment(_ast, _comment);
continue;
}
let args;
if (locEnd(_comment) - locEnd(_ast) >= 0) {
addTrailingComment(_ast, _comment);
continue;
}
}
if (avoidAstMutation) {
args = [context];
} else {
comment.enclosingNode = enclosingNode;
comment.precedingNode = precedingNode;
comment.followingNode = followingNode;
args = [comment, text, options, ast, isLastComment];
}
var args = void 0;
if (isOwnLineComment(text, options, decoratedComments, index)) {
comment.placement = 'ownLine'; // If a comment exists on its own line, prefer a leading comment.
// We also need to check if it's the first line of the file.
if (avoidAstMutation) {
args = [context];
if (handleOwnLineComment(...args)) {// We're good
} else if (followingNode) {
// Always a leading comment.
addLeadingComment(followingNode, comment);
} else if (precedingNode) {
addTrailingComment(precedingNode, comment);
} else if (enclosingNode) {
addDanglingComment(enclosingNode, comment);
} else {
_comment.enclosingNode = enclosingNode;
_comment.precedingNode = precedingNode;
_comment.followingNode = followingNode;
args = [_comment, _text, _options, _ast, isLastComment];
// There are no nodes, let's attach it to the root of the ast
/* istanbul ignore next */
addDanglingComment(ast, comment);
}
} else if (isEndOfLineComment(text, options, decoratedComments, index)) {
comment.placement = 'endOfLine';
if (isOwnLineComment(_text, _options, decoratedComments, index)) {
_comment.placement = 'ownLine'; // If a comment exists on its own line, prefer a leading comment.
// We also need to check if it's the first line of the file.
if (handleEndOfLineComment(...args)) {// We're good
} else if (precedingNode) {
// There is content before this comment on the same line, but
// none after it, so prefer a trailing comment of the previous node.
addTrailingComment(precedingNode, comment);
} else if (followingNode) {
addLeadingComment(followingNode, comment);
} else if (enclosingNode) {
addDanglingComment(enclosingNode, comment);
} else {
// There are no nodes, let's attach it to the root of the ast
if (handleOwnLineComment.apply(void 0, _toConsumableArray(args))) {// We're good
} else if (followingNode) {
// Always a leading comment.
addLeadingComment(followingNode, _comment);
} else if (precedingNode) {
addTrailingComment(precedingNode, _comment);
} else if (enclosingNode) {
addDanglingComment(enclosingNode, _comment);
} else {
// There are no nodes, let's attach it to the root of the ast
/* istanbul ignore next */
addDanglingComment(ast, comment);
}
} else {
comment.placement = 'remaining';
/* istanbul ignore next */
addDanglingComment(_ast, _comment);
}
} else if (isEndOfLineComment(_text, _options, decoratedComments, index)) {
_comment.placement = 'endOfLine';
if (handleRemainingComment(...args)) {// We're good
} else if (precedingNode && followingNode) {
// Otherwise, text exists both before and after the comment on
// the same line. If there is both a preceding and following
// node, use a tie-breaking algorithm to determine if it should
// be attached to the next or previous node. In the last case,
// simply attach the right node;
const tieCount = tiesToBreak.length;
if (handleEndOfLineComment.apply(void 0, _toConsumableArray(args))) {// We're good
} else if (precedingNode) {
// There is content before this comment on the same line, but
// none after it, so prefer a trailing comment of the previous node.
addTrailingComment(precedingNode, _comment);
} else if (followingNode) {
addLeadingComment(followingNode, _comment);
} else if (enclosingNode) {
addDanglingComment(enclosingNode, _comment);
} else {
// There are no nodes, let's attach it to the root of the ast
if (tieCount > 0) {
const lastTie = tiesToBreak[tieCount - 1];
/* istanbul ignore next */
addDanglingComment(_ast, _comment);
if (lastTie.followingNode !== followingNode) {
breakTies(tiesToBreak, text, options);
}
}
tiesToBreak.push(context);
} else if (precedingNode) {
addTrailingComment(precedingNode, comment);
} else if (followingNode) {
addLeadingComment(followingNode, comment);
} else if (enclosingNode) {
addDanglingComment(enclosingNode, comment);
} else {
_comment.placement = 'remaining';
// There are no nodes, let's attach it to the root of the ast
if (handleRemainingComment.apply(void 0, _toConsumableArray(args))) {// We're good
} else if (precedingNode && followingNode) {
// Otherwise, text exists both before and after the comment on
// the same line. If there is both a preceding and following
// node, use a tie-breaking algorithm to determine if it should
// be attached to the next or previous node. In the last case,
// simply attach the right node;
var tieCount = tiesToBreak.length;
if (tieCount > 0) {
var lastTie = tiesToBreak[tieCount - 1];
if (lastTie.followingNode !== followingNode) {
breakTies(tiesToBreak, _text, _options);
}
}
tiesToBreak.push(context);
} else if (precedingNode) {
addTrailingComment(precedingNode, _comment);
} else if (followingNode) {
addLeadingComment(followingNode, _comment);
} else if (enclosingNode) {
addDanglingComment(enclosingNode, _comment);
} else {
// There are no nodes, let's attach it to the root of the ast
/* istanbul ignore next */
addDanglingComment(_ast, _comment);
}
/* istanbul ignore next */
addDanglingComment(ast, comment);
}
}
} catch (err) {
_iterator2.e(err);
} finally {
_iterator2.f();
}

@@ -353,19 +297,9 @@

if (!avoidAstMutation) {
var _iterator3 = _createForOfIteratorHelper(comments),
_step3;
try {
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
var comment = _step3.value;
// These node references were useful for breaking ties, but we
// don't need them anymore, and they create cycles in the AST that
// may lead to infinite recursion if we don't delete them here.
delete comment.precedingNode;
delete comment.enclosingNode;
delete comment.followingNode;
}
} catch (err) {
_iterator3.e(err);
} finally {
_iterator3.f();
for (const comment of comments) {
// These node references were useful for breaking ties, but we
// don't need them anymore, and they create cycles in the AST that
// may lead to infinite recursion if we don't delete them here.
delete comment.precedingNode;
delete comment.enclosingNode;
delete comment.followingNode;
}

@@ -375,26 +309,28 @@ }

var isAllEmptyAndNoLineBreak = function isAllEmptyAndNoLineBreak(text) {
return !/[\S\n\u2028\u2029]/.test(text);
};
const isAllEmptyAndNoLineBreak = text => !/[\S\n\u2028\u2029]/.test(text);
function isOwnLineComment(text, options, decoratedComments, commentIndex) {
var _decoratedComments$co = decoratedComments[commentIndex],
comment = _decoratedComments$co.comment,
precedingNode = _decoratedComments$co.precedingNode;
var locStart = options.locStart,
locEnd = options.locEnd;
var start = locStart(comment);
const {
comment,
precedingNode
} = decoratedComments[commentIndex];
const {
locStart,
locEnd
} = options;
let start = locStart(comment);
if (precedingNode) {
// Find first comment on the same line
for (var index = commentIndex - 1; index >= 0; index--) {
var _decoratedComments$in = decoratedComments[index],
_comment2 = _decoratedComments$in.comment,
currentCommentPrecedingNode = _decoratedComments$in.precedingNode;
for (let index = commentIndex - 1; index >= 0; index--) {
const {
comment,
precedingNode: currentCommentPrecedingNode
} = decoratedComments[index];
if (currentCommentPrecedingNode !== precedingNode || !isAllEmptyAndNoLineBreak(text.slice(locEnd(_comment2), start))) {
if (currentCommentPrecedingNode !== precedingNode || !isAllEmptyAndNoLineBreak(text.slice(locEnd(comment), start))) {
break;
}
start = locStart(_comment2);
start = locStart(comment);
}

@@ -409,21 +345,25 @@ }

function isEndOfLineComment(text, options, decoratedComments, commentIndex) {
var _decoratedComments$co2 = decoratedComments[commentIndex],
comment = _decoratedComments$co2.comment,
followingNode = _decoratedComments$co2.followingNode;
var locStart = options.locStart,
locEnd = options.locEnd;
var end = locEnd(comment);
const {
comment,
followingNode
} = decoratedComments[commentIndex];
const {
locStart,
locEnd
} = options;
let end = locEnd(comment);
if (followingNode) {
// Find last comment on the same line
for (var index = commentIndex + 1; index < decoratedComments.length; index++) {
var _decoratedComments$in2 = decoratedComments[index],
_comment3 = _decoratedComments$in2.comment,
currentCommentFollowingNode = _decoratedComments$in2.followingNode;
for (let index = commentIndex + 1; index < decoratedComments.length; index++) {
const {
comment,
followingNode: currentCommentFollowingNode
} = decoratedComments[index];
if (currentCommentFollowingNode !== followingNode || !isAllEmptyAndNoLineBreak(text.slice(end, locStart(_comment3)))) {
if (currentCommentFollowingNode !== followingNode || !isAllEmptyAndNoLineBreak(text.slice(end, locStart(comment)))) {
break;
}
end = locEnd(_comment3);
end = locEnd(comment);
}

@@ -436,3 +376,3 @@ }

function breakTies(tiesToBreak, text, options) {
var tieCount = tiesToBreak.length;
const tieCount = tiesToBreak.length;

@@ -443,8 +383,9 @@ if (tieCount === 0) {

var _tiesToBreak$ = tiesToBreak[0],
precedingNode = _tiesToBreak$.precedingNode,
followingNode = _tiesToBreak$.followingNode,
enclosingNode = _tiesToBreak$.enclosingNode;
var gapRegExp = options.printer.getGapRegex && options.printer.getGapRegex(enclosingNode) || /^[\s(]*$/;
var gapEndPos = options.locStart(followingNode); // Iterate backwards through tiesToBreak, examining the gaps
const {
precedingNode,
followingNode,
enclosingNode
} = tiesToBreak[0];
const gapRegExp = options.printer.getGapRegex && options.printer.getGapRegex(enclosingNode) || /^[\s(]*$/;
let gapEndPos = options.locStart(followingNode); // Iterate backwards through tiesToBreak, examining the gaps
// between the tied comments. In order to qualify as leading, a

@@ -455,12 +396,13 @@ // comment must be separated from followingNode by an unbroken series of

var indexOfFirstLeadingComment;
let indexOfFirstLeadingComment;
for (indexOfFirstLeadingComment = tieCount; indexOfFirstLeadingComment > 0; --indexOfFirstLeadingComment) {
var _tiesToBreak = tiesToBreak[indexOfFirstLeadingComment - 1],
comment = _tiesToBreak.comment,
currentCommentPrecedingNode = _tiesToBreak.precedingNode,
currentCommentFollowingNode = _tiesToBreak.followingNode;
const {
comment,
precedingNode: currentCommentPrecedingNode,
followingNode: currentCommentFollowingNode
} = tiesToBreak[indexOfFirstLeadingComment - 1];
assert.strictEqual(currentCommentPrecedingNode, precedingNode);
assert.strictEqual(currentCommentFollowingNode, followingNode);
var gap = text.slice(options.locEnd(comment), gapEndPos);
const gap = text.slice(options.locEnd(comment), gapEndPos);

@@ -476,30 +418,15 @@ if (gapRegExp.test(gap)) {

var _iterator4 = _createForOfIteratorHelper(tiesToBreak.entries()),
_step4;
try {
for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
var _step4$value = _slicedToArray(_step4.value, 2),
i = _step4$value[0],
_comment4 = _step4$value[1].comment;
if (i < indexOfFirstLeadingComment) {
addTrailingComment(precedingNode, _comment4);
} else {
addLeadingComment(followingNode, _comment4);
}
for (const [i, {
comment
}] of tiesToBreak.entries()) {
if (i < indexOfFirstLeadingComment) {
addTrailingComment(precedingNode, comment);
} else {
addLeadingComment(followingNode, comment);
}
} catch (err) {
_iterator4.e(err);
} finally {
_iterator4.f();
}
for (var _i2 = 0, _arr2 = [precedingNode, followingNode]; _i2 < _arr2.length; _i2++) {
var node = _arr2[_i2];
for (const node of [precedingNode, followingNode]) {
if (node.comments && node.comments.length > 1) {
node.comments.sort(function (a, b) {
return options.locStart(a) - options.locStart(b);
});
node.comments.sort((a, b) => options.locStart(a) - options.locStart(b));
}

@@ -512,5 +439,5 @@ }

function findExpressionIndexForComment(quasis, comment, options) {
var startPos = options.locStart(comment) - 1;
const startPos = options.locStart(comment) - 1;
for (var i = 1; i < quasis.length; ++i) {
for (let i = 1; i < quasis.length; ++i) {
if (startPos < options.locStart(quasis[i])) {

@@ -529,3 +456,3 @@ return i - 1;

module.exports = {
attach: attach
attach
};
/**
* Copyright (c) Facebook, Inc. and its affiliates.
* Copyright (c) Meta Platforms, Inc. and affiliates.
*

@@ -11,6 +11,4 @@ * This source code is licensed under the MIT license found in the

var getLast = function getLast(arr) {
return arr[arr.length - 1];
};
const getLast = arr => arr[arr.length - 1];
module.exports = getLast;
"use strict";
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
Object.defineProperty(exports, "__esModule", {

@@ -10,26 +8,4 @@ value: true

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null || !_isNativeFunction(Class)) return Class; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); }
function _construct(Parent, args, Class) { if (_isNativeReflectConstruct()) { _construct = Reflect.construct; } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function _isNativeFunction(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
/**
* Copyright (c) Facebook, Inc. and its affiliates.
* Copyright (c) Meta Platforms, Inc. and affiliates.
*

@@ -42,112 +18,28 @@ * This source code is licensed under the MIT license found in the

*/
var NodeIsDeletedError = /*#__PURE__*/function (_Error) {
_inherits(NodeIsDeletedError, _Error);
class NodeIsDeletedError extends Error {}
var _super = _createSuper(NodeIsDeletedError);
function NodeIsDeletedError() {
_classCallCheck(this, NodeIsDeletedError);
return _super.apply(this, arguments);
}
return NodeIsDeletedError;
}( /*#__PURE__*/_wrapNativeSuper(Error));
exports.NodeIsDeletedError = NodeIsDeletedError;
var NodeIsMutatedError = /*#__PURE__*/function (_Error2) {
_inherits(NodeIsMutatedError, _Error2);
class NodeIsMutatedError extends Error {}
var _super2 = _createSuper(NodeIsMutatedError);
function NodeIsMutatedError() {
_classCallCheck(this, NodeIsMutatedError);
return _super2.apply(this, arguments);
}
return NodeIsMutatedError;
}( /*#__PURE__*/_wrapNativeSuper(Error));
exports.NodeIsMutatedError = NodeIsMutatedError;
var InvalidInsertionError = /*#__PURE__*/function (_Error3) {
_inherits(InvalidInsertionError, _Error3);
class InvalidInsertionError extends Error {}
var _super3 = _createSuper(InvalidInsertionError);
function InvalidInsertionError() {
_classCallCheck(this, InvalidInsertionError);
return _super3.apply(this, arguments);
}
return InvalidInsertionError;
}( /*#__PURE__*/_wrapNativeSuper(Error));
exports.InvalidInsertionError = InvalidInsertionError;
var UnexpectedTransformationState = /*#__PURE__*/function (_Error4) {
_inherits(UnexpectedTransformationState, _Error4);
class UnexpectedTransformationState extends Error {}
var _super4 = _createSuper(UnexpectedTransformationState);
function UnexpectedTransformationState() {
_classCallCheck(this, UnexpectedTransformationState);
return _super4.apply(this, arguments);
}
return UnexpectedTransformationState;
}( /*#__PURE__*/_wrapNativeSuper(Error));
exports.UnexpectedTransformationState = UnexpectedTransformationState;
var InvalidStatementError = /*#__PURE__*/function (_Error5) {
_inherits(InvalidStatementError, _Error5);
class InvalidStatementError extends Error {}
var _super5 = _createSuper(InvalidStatementError);
function InvalidStatementError() {
_classCallCheck(this, InvalidStatementError);
return _super5.apply(this, arguments);
}
return InvalidStatementError;
}( /*#__PURE__*/_wrapNativeSuper(Error));
exports.InvalidStatementError = InvalidStatementError;
var InvalidRemovalError = /*#__PURE__*/function (_Error6) {
_inherits(InvalidRemovalError, _Error6);
class InvalidRemovalError extends Error {}
var _super6 = _createSuper(InvalidRemovalError);
function InvalidRemovalError() {
_classCallCheck(this, InvalidRemovalError);
return _super6.apply(this, arguments);
}
return InvalidRemovalError;
}( /*#__PURE__*/_wrapNativeSuper(Error));
exports.InvalidRemovalError = InvalidRemovalError;
var InvalidReplacementError = /*#__PURE__*/function (_Error7) {
_inherits(InvalidReplacementError, _Error7);
class InvalidReplacementError extends Error {}
var _super7 = _createSuper(InvalidReplacementError);
function InvalidReplacementError() {
_classCallCheck(this, InvalidReplacementError);
return _super7.apply(this, arguments);
}
return InvalidReplacementError;
}( /*#__PURE__*/_wrapNativeSuper(Error));
exports.InvalidReplacementError = InvalidReplacementError;
/**
* Copyright (c) Facebook, Inc. and its affiliates.
* Copyright (c) Meta Platforms, Inc. and affiliates.
*

@@ -39,2 +39,4 @@ * This source code is licensed under the MIT license found in the

var _RemoveNode = require("./mutations/RemoveNode");
var _RemoveStatement = require("./mutations/RemoveStatement");

@@ -46,113 +48,95 @@

function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function getTransformedAST(code, visitors) {
var _parseForESLint = (0, _hermesEslint.parseForESLint)(code, {
const {
ast,
scopeManager
} = (0, _hermesEslint.parseForESLint)(code, {
sourceType: 'module'
}),
ast = _parseForESLint.ast,
scopeManager = _parseForESLint.scopeManager; // attach comments before mutation. this will ensure that as nodes are
}); // attach comments before mutation. this will ensure that as nodes are
// cloned / moved around - comments remain in the correct place with respect to the node
(0, _comments.attachComments)(ast.comments, ast, code); // traverse the AST and colllect the mutations
var transformContext = (0, _TransformContext.getTransformContext)();
(0, _traverse.traverseWithContext)(ast, scopeManager, function () {
return transformContext;
}, visitors); // apply the mutations to the AST
const transformContext = (0, _TransformContext.getTransformContext)(code);
(0, _traverse.traverseWithContext)(ast, scopeManager, () => transformContext, visitors); // apply the mutations to the AST
var mutationContext = new _MutationContext.MutationContext(code);
var removeCommentMutations = [];
const mutationContext = new _MutationContext.MutationContext(code);
const removeCommentMutations = [];
var _iterator = _createForOfIteratorHelper(transformContext.mutations),
_step;
for (const mutation of transformContext.mutations) {
const mutationRoot = (() => {
switch (mutation.type) {
case 'insertStatement':
{
return (0, _InsertStatement.performInsertStatementMutation)(mutationContext, mutation);
}
try {
var _loop = function _loop() {
var mutation = _step.value;
case 'replaceNode':
{
return (0, _ReplaceNode.performReplaceNodeMutation)(mutationContext, mutation);
}
var mutationRoot = function () {
switch (mutation.type) {
case 'insertStatement':
{
return (0, _InsertStatement.performInsertStatementMutation)(mutationContext, mutation);
}
case 'replaceStatementWithMany':
{
return (0, _ReplaceStatementWithMany.performReplaceStatementWithManyMutation)(mutationContext, mutation);
}
case 'replaceNode':
{
return (0, _ReplaceNode.performReplaceNodeMutation)(mutationContext, mutation);
}
case 'removeNode':
{
return (0, _RemoveNode.performRemoveNodeMutation)(mutationContext, mutation);
}
case 'replaceStatementWithMany':
{
return (0, _ReplaceStatementWithMany.performReplaceStatementWithManyMutation)(mutationContext, mutation);
}
case 'removeStatement':
{
return (0, _RemoveStatement.performRemoveStatementMutation)(mutationContext, mutation);
}
case 'removeStatement':
{
return (0, _RemoveStatement.performRemoveStatementMutation)(mutationContext, mutation);
}
case 'removeComment':
{
// these are handled later
removeCommentMutations.push(mutation);
return null;
}
case 'removeComment':
{
// these are handled later
removeCommentMutations.push(mutation);
return null;
}
case 'addLeadingComments':
{
return (0, _AddLeadingComments.performAddLeadingCommentsMutation)(mutationContext, mutation);
}
case 'addLeadingComments':
{
return (0, _AddLeadingComments.performAddLeadingCommentsMutation)(mutationContext, mutation);
}
case 'addTrailingComments':
{
return (0, _AddTrailingComments.performAddTrailingCommentsMutation)(mutationContext, mutation);
}
case 'addTrailingComments':
{
return (0, _AddTrailingComments.performAddTrailingCommentsMutation)(mutationContext, mutation);
}
case 'cloneCommentsTo':
{
return (0, _CloneCommentsTo.performCloneCommentsToMutation)(mutationContext, mutation);
}
}
})(); // ensure the subtree's parent pointers are correct
// this is required for two reasons:
// 1) The userland transform is just JS - so there's nothing stopping them
// from doing anything dodgy. The flow types have some enforcement, but
// ofc that can just be ignored with a suppression.
// 2) Shallow clones are a necessary evil in the transform because they
// allow codemods to do simple changes to just one node without the
// weight that comes with deeply cloning the entire AST.
// However we can't update the parent pointers of the cloned node's
// children until the mutation step or else we would be mutating
// real AST nodes and potentially break the traverse step.
//
// Being strict here just helps us ensure we keep everything in sync
case 'cloneCommentsTo':
{
return (0, _CloneCommentsTo.performCloneCommentsToMutation)(mutationContext, mutation);
}
}
}(); // ensure the subtree's parent pointers are correct
// this is required for two reasons:
// 1) The userland transform is just JS - so there's nothing stopping them
// from doing anything dodgy. The flow types have some enforcement, but
// ofc that can just be ignored with a suppression.
// 2) Shallow clones are a necessary evil in the transform because they
// allow codemods to do simple changes to just one node without the
// weight that comes with deeply cloning the entire AST.
// However we can't update the parent pointers of the cloned node's
// children until the mutation step or else we would be mutating
// real AST nodes and potentially break the traverse step.
//
// Being strict here just helps us ensure we keep everything in sync
if (mutationRoot) {
(0, _detachedNode.updateAllParentPointers)(mutationRoot);
}
} // remove the comments
// this is done at the end because it requires a complete traversal of the AST
// so that we can find relevant node's attachment array
if (mutationRoot) {
(0, _detachedNode.updateAllParentPointers)(mutationRoot);
}
};
for (_iterator.s(); !(_step = _iterator.n()).done;) {
_loop();
} // remove the comments
// this is done at the end because it requires a complete traversal of the AST
// so that we can find relevant node's attachment array
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
(0, _RemoveComment.performRemoveCommentMutations)(ast, removeCommentMutations);
return {
ast: ast,
ast,
astWasMutated: transformContext.astWasMutated,

@@ -159,0 +143,0 @@ mutatedCode: mutationContext.code

@@ -12,84 +12,71 @@ "use strict";

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
var MutationContext = /*#__PURE__*/function () {
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*
* @format
*/
class MutationContext {
// TODO - do we care about this? Arrays are pretty safe to concurrently mutate
function MutationContext(code) {
_classCallCheck(this, MutationContext);
constructor(code) {
this._deletedNodes = new Set();
this._mutatedKeys = new Map();
this._mutatedArrays = new Map();
this.code = void 0;
this.code = code;
}
/**
* Marks a node and its entire subtree as deleted.
*/
_defineProperty(this, "_deletedNodes", new Set());
_defineProperty(this, "_mutatedKeys", new Map());
markDeletion(node) {
this._deletedNodes.add(node);
}
/**
* Marks the key of the node as having been mutated.
*/
_defineProperty(this, "_mutatedArrays", new Map());
_defineProperty(this, "code", void 0);
markMutation(node, key) {
var _map$get$add, _map$get;
this.code = code;
this.assertNotDeleted(node, `Attempted to mutate a \`${node.type}.${key}\` on a deleted node.`);
this.assertNotMutated(node, key, `Attempted to mutate a \`${node.type}.${key}\` when it has already been mutated.`);
const map = Array.isArray( // $FlowExpectedError[prop-missing]
node[key]) ? this._mutatedArrays : this._mutatedKeys;
map.set(node, (_map$get$add = (_map$get = map.get(node)) == null ? void 0 : _map$get.add(key)) != null ? _map$get$add : new Set([key]));
}
/**
* Marks a node and its entire subtree as deleted.
* Throws if the node has been deleted
*/
_createClass(MutationContext, [{
key: "markDeletion",
value: function markDeletion(node) {
this._deletedNodes.add(node);
assertNotDeleted(node, message) {
if (this._deletedNodes.has(node)) {
throw new _Errors.NodeIsDeletedError(message);
}
/**
* Marks the key of the node as having been mutated.
*/
}
/**
* Throws if the key of the node has been mutated
*/
}, {
key: "markMutation",
value: function markMutation(node, key) {
var _map$get$add, _map$get;
this.assertNotDeleted(node, "Attempted to mutate a `".concat(node.type, ".").concat(key, "` on a deleted node."));
this.assertNotMutated(node, key, "Attempted to mutate a `".concat(node.type, ".").concat(key, "` when it has already been mutated."));
var map = Array.isArray( // $FlowExpectedError[prop-missing]
node[key]) ? this._mutatedArrays : this._mutatedKeys;
map.set(node, (_map$get$add = (_map$get = map.get(node)) === null || _map$get === void 0 ? void 0 : _map$get.add(key)) !== null && _map$get$add !== void 0 ? _map$get$add : new Set([key]));
}
/**
* Throws if the node has been deleted
*/
assertNotMutated(node, key, message) {
var _this$_mutatedKeys$ge;
}, {
key: "assertNotDeleted",
value: function assertNotDeleted(node, message) {
if (this._deletedNodes.has(node)) {
throw new _Errors.NodeIsDeletedError(message);
}
if (((_this$_mutatedKeys$ge = this._mutatedKeys.get(node)) == null ? void 0 : _this$_mutatedKeys$ge.has(key)) === true) {
throw new _Errors.NodeIsMutatedError(message);
}
/**
* Throws if the key of the node has been mutated
*/
}
}, {
key: "assertNotMutated",
value: function assertNotMutated(node, key, message) {
var _this$_mutatedKeys$ge;
appendCommentToSource(comment) {
this.code = (0, _comments.appendCommentToSource)(this.code, comment);
}
if (((_this$_mutatedKeys$ge = this._mutatedKeys.get(node)) === null || _this$_mutatedKeys$ge === void 0 ? void 0 : _this$_mutatedKeys$ge.has(key)) === true) {
throw new _Errors.NodeIsMutatedError(message);
}
}
}, {
key: "appendCommentToSource",
value: function appendCommentToSource(comment) {
this.code = (0, _comments.appendCommentToSource)(this.code, comment);
}
}]);
}
return MutationContext;
}();
exports.MutationContext = MutationContext;

@@ -11,8 +11,11 @@ "use strict";

function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*
* @format
*/
function createAddLeadingCommentsMutation(node, comments) {

@@ -25,4 +28,4 @@ if (comments.length === 0) {

type: 'addLeadingComments',
comments: comments,
node: node
comments,
node
};

@@ -32,16 +35,6 @@ }

function performAddLeadingCommentsMutation(mutationContext, mutation) {
var _iterator = _createForOfIteratorHelper(mutation.comments),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var originalComment = _step.value;
var comment = (0, _comments.cloneComment)(originalComment);
mutationContext.appendCommentToSource(comment);
(0, _comments.addLeadingComment)(mutation.node, comment);
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
for (const originalComment of mutation.comments) {
const comment = (0, _comments.cloneComment)(originalComment);
mutationContext.appendCommentToSource(comment);
(0, _comments.addLeadingComment)(mutation.node, comment);
}

@@ -48,0 +41,0 @@

@@ -11,8 +11,11 @@ "use strict";

function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*
* @format
*/
function createAddTrailingCommentsMutation(node, comments) {

@@ -25,4 +28,4 @@ if (comments.length === 0) {

type: 'addTrailingComments',
comments: comments,
node: node
comments,
node
};

@@ -32,16 +35,6 @@ }

function performAddTrailingCommentsMutation(mutationContext, mutation) {
var _iterator = _createForOfIteratorHelper(mutation.comments),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var originalComment = _step.value;
var comment = (0, _comments.cloneComment)(originalComment);
mutationContext.appendCommentToSource(comment);
(0, _comments.addTrailingComment)(mutation.node, comment);
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
for (const originalComment of mutation.comments) {
const comment = (0, _comments.cloneComment)(originalComment);
mutationContext.appendCommentToSource(comment);
(0, _comments.addTrailingComment)(mutation.node, comment);
}

@@ -48,0 +41,0 @@

@@ -11,13 +11,16 @@ "use strict";

function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*
* @format
*/
function createCloneCommentsToMutation(target, destination) {
return {
type: 'cloneCommentsTo',
target: target,
destination: destination
target,
destination
};

@@ -27,18 +30,8 @@ }

function performCloneCommentsToMutation(mutationContext, mutation) {
var newComments = [];
const newComments = [];
var _iterator = _createForOfIteratorHelper((0, _comments.getCommentsForNode)(mutation.target)),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var originalComment = _step.value;
var comment = (0, _comments.cloneCommentWithMarkers)(originalComment);
mutationContext.appendCommentToSource(comment);
newComments.push(comment);
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
for (const originalComment of (0, _comments.getCommentsForNode)(mutation.target)) {
const comment = (0, _comments.cloneCommentWithMarkers)(originalComment);
mutationContext.appendCommentToSource(comment);
newComments.push(comment);
}

@@ -45,0 +38,0 @@

"use strict";
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
Object.defineProperty(exports, "__esModule", {

@@ -21,18 +19,15 @@ value: true

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
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); }
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; }
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; }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*
* @format
*/
function createInsertStatementMutation(side, target, nodesToInsert) {

@@ -45,5 +40,5 @@ if (nodesToInsert.length === 0) {

type: 'insertStatement',
side: side,
target: target,
nodesToInsert: nodesToInsert
side,
target,
nodesToInsert
};

@@ -53,6 +48,6 @@ }

function performInsertStatementMutation(mutationContext, mutation) {
var insertionParent = (0, _getStatementParent.getStatementParent)(mutation.target); // enforce that if we are inserting module declarations - they are being inserted in a valid location
const insertionParent = (0, _getStatementParent.getStatementParent)(mutation.target); // enforce that if we are inserting module declarations - they are being inserted in a valid location
if (!(0, _isValidModuleDeclarationParent.isValidModuleDeclarationParent)(insertionParent.parent, mutation.nodesToInsert)) {
throw new _Errors.InvalidInsertionError("import/export cannot be inserted into a ".concat(insertionParent.parent.type, "."));
throw new _Errors.InvalidInsertionError(`import/export cannot be inserted into a ${insertionParent.parent.type}.`);
}

@@ -63,3 +58,3 @@

if (insertionParent.type === 'array') {
var _parent = insertionParent.parent;
const parent = insertionParent.parent;

@@ -69,3 +64,3 @@ switch (mutation.side) {

{
_parent[insertionParent.key] = (0, _arrayUtils.insertInArray)(_parent[insertionParent.key], insertionParent.targetIndex - 1, mutation.nodesToInsert);
parent[insertionParent.key] = (0, _arrayUtils.insertInArray)(parent[insertionParent.key], insertionParent.targetIndex - 1, mutation.nodesToInsert);
break;

@@ -76,3 +71,3 @@ }

{
_parent[insertionParent.key] = (0, _arrayUtils.insertInArray)(_parent[insertionParent.key], insertionParent.targetIndex + 1, mutation.nodesToInsert);
parent[insertionParent.key] = (0, _arrayUtils.insertInArray)(parent[insertionParent.key], insertionParent.targetIndex + 1, mutation.nodesToInsert);
break;

@@ -85,11 +80,13 @@ }

var statementsToInsert = // $FlowExpectedError[incompatible-cast] -- this is enforced by isValidModuleDeclarationParent above
const statementsToInsert = // $FlowExpectedError[incompatible-cast] -- this is enforced by isValidModuleDeclarationParent above
mutation.nodesToInsert;
var parent = insertionParent.parent,
key = insertionParent.key; // $FlowExpectedError[prop-missing]
const {
parent,
key
} = insertionParent; // $FlowExpectedError[prop-missing]
var statementToWrap = parent[key]; // we need to wrap this key in a BlockStatement so we can insert the new statement
const statementToWrap = parent[key]; // we need to wrap this key in a BlockStatement so we can insert the new statement
var blockStatement = t.BlockStatement({
body: mutation.side === 'before' ? [].concat(_toConsumableArray(statementsToInsert), [statementToWrap]) : [statementToWrap].concat(_toConsumableArray(statementsToInsert)),
const blockStatement = t.BlockStatement({
body: mutation.side === 'before' ? [...statementsToInsert, statementToWrap] : [statementToWrap, ...statementsToInsert],
parent: insertionParent.parent

@@ -96,0 +93,0 @@ });

@@ -13,12 +13,15 @@ "use strict";

function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*
* @format
*/
function createRemoveCommentMutation(comment) {
return {
type: 'removeComment',
comment: comment
comment
};

@@ -32,8 +35,6 @@ }

var commentsToRemove = new Set(mutations.map(function (m) {
return m.comment;
}));
const commentsToRemove = new Set(mutations.map(m => m.comment));
_SimpleTraverser.SimpleTraverser.traverse(ast, {
enter: function enter(node) {
enter(node) {
if (node === ast) {

@@ -43,3 +44,3 @@ return;

var nodeCommentsSet = new Set((0, _comments.getCommentsForNode)(node));
const nodeCommentsSet = new Set((0, _comments.getCommentsForNode)(node));

@@ -50,17 +51,7 @@ if (nodeCommentsSet.size === 0) {

var matchedComments = intersectSets(commentsToRemove, nodeCommentsSet);
const matchedComments = intersectSets(commentsToRemove, nodeCommentsSet);
var _iterator = _createForOfIteratorHelper(matchedComments),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var comment = _step.value;
commentsToRemove["delete"](comment);
nodeCommentsSet["delete"](comment);
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
for (const comment of matchedComments) {
commentsToRemove.delete(comment);
nodeCommentsSet.delete(comment);
}

@@ -75,3 +66,5 @@

},
leave: function leave() {}
leave() {}
});

@@ -81,19 +74,8 @@ }

function intersectSets(first, other) {
var ret = new Set();
const ret = new Set();
var _iterator2 = _createForOfIteratorHelper(first),
_step2;
try {
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
var value = _step2.value;
if (other.has(value)) {
ret.add(value);
}
for (const value of first) {
if (other.has(value)) {
ret.add(value);
}
} catch (err) {
_iterator2.e(err);
} finally {
_iterator2.f();
}

@@ -100,0 +82,0 @@

"use strict";
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
Object.defineProperty(exports, "__esModule", {

@@ -17,8 +15,8 @@ value: true

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
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); }
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; }
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; }
/**
* Copyright (c) Facebook, Inc. and its affiliates.
* Copyright (c) Meta Platforms, Inc. and affiliates.
*

@@ -34,3 +32,3 @@ * This source code is licensed under the MIT license found in the

type: 'removeStatement',
node: node
node
};

@@ -40,3 +38,3 @@ }

function performRemoveStatementMutation(mutationContext, mutation) {
var removalParent = (0, _getStatementParent.getStatementParent)(mutation.node);
const removalParent = (0, _getStatementParent.getStatementParent)(mutation.node);
mutationContext.markDeletion(mutation.node);

@@ -46,3 +44,3 @@ mutationContext.markMutation(removalParent.parent, removalParent.key);

if (removalParent.type === 'array') {
var parent = removalParent.parent;
const parent = removalParent.parent;
parent[removalParent.key] = (0, _arrayUtils.removeFromArray)(parent[removalParent.key], removalParent.targetIndex);

@@ -57,3 +55,3 @@ } else {

// and cleaned up later.
var blockStatement = t.BlockStatement({
const blockStatement = t.BlockStatement({
body: [],

@@ -60,0 +58,0 @@ parent: removalParent.parent

@@ -17,8 +17,11 @@ "use strict";

function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*
* @format
*/
function createReplaceNodeMutation(target, nodeToReplaceWith, options) {

@@ -29,5 +32,5 @@ var _options$keepComments;

type: 'replaceNode',
target: target,
nodeToReplaceWith: nodeToReplaceWith,
keepComments: (_options$keepComments = options === null || options === void 0 ? void 0 : options.keepComments) !== null && _options$keepComments !== void 0 ? _options$keepComments : false
target,
nodeToReplaceWith,
keepComments: (_options$keepComments = options == null ? void 0 : options.keepComments) != null ? _options$keepComments : false
};

@@ -37,3 +40,3 @@ }

function performReplaceNodeMutation(mutationContext, mutation) {
var replacementParent = getParentKey(mutation.target);
const replacementParent = getParentKey(mutation.target);
mutationContext.markDeletion(mutation.target);

@@ -46,3 +49,3 @@ mutationContext.markMutation(replacementParent.parent, replacementParent.key); // NOTE: currently this mutation assumes you're doing the right thing.

if (replacementParent.type === 'array') {
var parent = replacementParent.parent;
const parent = replacementParent.parent;
parent[replacementParent.key] = (0, _arrayUtils.replaceInArray)(parent[replacementParent.key], replacementParent.targetIndex, [mutation.nodeToReplaceWith]);

@@ -61,41 +64,30 @@ } else {

function getParentKey(target) {
var parent = target.parent;
const parent = target.parent;
var _iterator = _createForOfIteratorHelper((0, _getVisitorKeys.getVisitorKeys)(parent)),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var key = _step.value;
if ((0, _getVisitorKeys.isNode)( // $FlowExpectedError[prop-missing]
parent[key])) {
if (parent[key] === target) {
for (const key of (0, _getVisitorKeys.getVisitorKeys)(parent)) {
if ((0, _getVisitorKeys.isNode)( // $FlowExpectedError[prop-missing]
parent[key])) {
if (parent[key] === target) {
return {
type: 'single',
parent,
key
};
}
} else if (Array.isArray(parent[key])) {
for (let i = 0; i < parent[key].length; i += 1) {
if (parent[key][i] === target) {
return {
type: 'single',
parent: parent,
key: key
type: 'array',
parent,
key,
targetIndex: i
};
}
} else if (Array.isArray(parent[key])) {
for (var i = 0; i < parent[key].length; i += 1) {
if (parent[key][i] === target) {
return {
type: 'array',
parent: parent,
key: key,
targetIndex: i
};
}
}
}
} // this shouldn't happen ever
}
} // this shouldn't happen ever
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
throw new _Errors.InvalidReplacementError("Expected to find the ".concat(target.type, " as a direct child of the ").concat(target.type, "."));
throw new _Errors.InvalidReplacementError(`Expected to find the ${target.type} as a direct child of the ${target.type}.`);
}
"use strict";
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
Object.defineProperty(exports, "__esModule", {

@@ -23,8 +21,8 @@ value: true

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
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); }
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; }
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; }
/**
* Copyright (c) Facebook, Inc. and its affiliates.
* Copyright (c) Meta Platforms, Inc. and affiliates.
*

@@ -46,5 +44,5 @@ * This source code is licensed under the MIT license found in the

type: 'replaceStatementWithMany',
target: target,
nodesToReplaceWith: nodesToReplaceWith,
keepComments: (_options$keepComments = options === null || options === void 0 ? void 0 : options.keepComments) !== null && _options$keepComments !== void 0 ? _options$keepComments : false
target,
nodesToReplaceWith,
keepComments: (_options$keepComments = options == null ? void 0 : options.keepComments) != null ? _options$keepComments : false
};

@@ -54,6 +52,6 @@ }

function performReplaceStatementWithManyMutation(mutationContext, mutation) {
var replacementParent = (0, _getStatementParent.getStatementParent)(mutation.target); // enforce that if we are replacing with module declarations - they are being inserted in a valid location
const replacementParent = (0, _getStatementParent.getStatementParent)(mutation.target); // enforce that if we are replacing with module declarations - they are being inserted in a valid location
if (!(0, _isValidModuleDeclarationParent.isValidModuleDeclarationParent)(replacementParent.parent, mutation.nodesToReplaceWith)) {
throw new _Errors.InvalidReplacementError("import/export cannot be replaced into a ".concat(replacementParent.parent.type, "."));
throw new _Errors.InvalidReplacementError(`import/export cannot be replaced into a ${replacementParent.parent.type}.`);
}

@@ -70,3 +68,3 @@

if (replacementParent.type === 'array') {
var parent = replacementParent.parent;
const parent = replacementParent.parent;
parent[replacementParent.key] = (0, _arrayUtils.replaceInArray)(parent[replacementParent.key], replacementParent.targetIndex, mutation.nodesToReplaceWith);

@@ -76,6 +74,6 @@ return replacementParent.parent;

var statementsToReplaceWith = // $FlowExpectedError[incompatible-cast] -- this is enforced by isValidModuleDeclarationParent above
const statementsToReplaceWith = // $FlowExpectedError[incompatible-cast] -- this is enforced by isValidModuleDeclarationParent above
mutation.nodesToReplaceWith; // we need to wrap the nodes in a BlockStatement as before there was only 1 node
var blockStatement = t.BlockStatement({
const blockStatement = t.BlockStatement({
body: statementsToReplaceWith,

@@ -82,0 +80,0 @@ parent: replacementParent.parent

@@ -10,16 +10,4 @@ "use strict";

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
/**
* Copyright (c) Facebook, Inc. and its affiliates.
* Copyright (c) Meta Platforms, Inc. and affiliates.
*

@@ -37,3 +25,3 @@ * This source code is licensed under the MIT license found in the

function removeFromArray(array, index) {
return [].concat(_toConsumableArray(array.slice(0, index)), _toConsumableArray(array.slice(index + 1)));
return [...array.slice(0, index), ...array.slice(index + 1)];
}

@@ -40,0 +28,0 @@

@@ -11,3 +11,3 @@ "use strict";

/**
* Copyright (c) Facebook, Inc. and its affiliates.
* Copyright (c) Meta Platforms, Inc. and affiliates.
*

@@ -21,14 +21,9 @@ * This source code is licensed under the MIT license found in the

function getStatementParent(target) {
function assertValidStatementLocation(parentWithType) {
for (var _len = arguments.length, invalidKeys = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
invalidKeys[_key - 1] = arguments[_key];
}
for (var _i = 0, _invalidKeys = invalidKeys; _i < _invalidKeys.length; _i++) {
var key = _invalidKeys[_i];
function assertValidStatementLocation(parentWithType, ...invalidKeys) {
for (const key of invalidKeys) {
// $FlowExpectedError[prop-missing]
var value = parentWithType[key];
const value = parentWithType[key];
if (value === target || Array.isArray(value) && value.includes(target)) {
throw new _Errors.InvalidStatementError("Attempted to insert a statement into `".concat(parentWithType.type, ".").concat(key, "`."));
throw new _Errors.InvalidStatementError(`Attempted to insert a statement into \`${parentWithType.type}.${key}\`.`);
}

@@ -39,6 +34,6 @@ }

function getAssertedIndex(key, arr) {
var idx = arr.indexOf(target);
const idx = arr.indexOf(target);
if (idx == null) {
throw new _Errors.InvalidStatementError("Could not find target in array of `".concat(parent.type, ".").concat(key, "`."));
if (idx === -1) {
throw new _Errors.InvalidStatementError(`Could not find target in array of \`${parent.type}.${key}\`.`);
}

@@ -49,5 +44,5 @@

var parent = target.parent;
const parent = target.parent;
var result = function () {
const result = (() => {
switch (parent.type) {

@@ -57,7 +52,7 @@ case 'IfStatement':

assertValidStatementLocation(parent, 'test');
var key = parent.consequent === target ? 'consequent' : 'alternate';
const key = parent.consequent === target ? 'consequent' : 'alternate';
return {
type: 'single',
parent: parent,
key: key
parent,
key
};

@@ -71,3 +66,3 @@ }

type: 'single',
parent: parent,
parent,
key: 'body'

@@ -82,3 +77,3 @@ };

type: 'single',
parent: parent,
parent,
key: 'body'

@@ -94,3 +89,3 @@ };

type: 'single',
parent: parent,
parent,
key: 'body'

@@ -105,3 +100,3 @@ };

type: 'single',
parent: parent,
parent,
key: 'body'

@@ -118,3 +113,3 @@ };

type: 'single',
parent: parent,
parent,
key: 'body'

@@ -129,3 +124,3 @@ };

type: 'array',
parent: parent,
parent,
key: 'consequent',

@@ -141,3 +136,3 @@ targetIndex: getAssertedIndex('consequent', parent.consequent)

type: 'array',
parent: parent,
parent,
key: 'body',

@@ -149,4 +144,4 @@ targetIndex: getAssertedIndex('body', parent.body)

throw new _Errors.InvalidStatementError("Expected to find a valid statement parent, but found a parent of type \"".concat(parent.type, "\"."));
}();
throw new _Errors.InvalidStatementError(`Expected to find a valid statement parent, but found a parent of type "${parent.type}".`);
})();

@@ -156,3 +151,3 @@ if ( // array insertions are already validated by the getAssertedIndex function

result.parent[result.key] !== target) {
throw new _Errors.InvalidStatementError("Expected to find the target \"".concat(target.type, "\" on the \"").concat(result.parent.type, ".").concat(result.key, "\", but found a different node. ") + 'This likely means that you attempted to mutate around the target after it was deleted/replaced.');
throw new _Errors.InvalidStatementError(`Expected to find the target "${target.type}" on the "${result.parent.type}.${result.key}", but found a different node. ` + 'This likely means that you attempted to mutate around the target after it was deleted/replaced.');
}

@@ -159,0 +154,0 @@

@@ -8,10 +8,4 @@ "use strict";

function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
/**
* Copyright (c) Facebook, Inc. and its affiliates.
* Copyright (c) Meta Platforms, Inc. and affiliates.
*

@@ -33,20 +27,9 @@ * This source code is licensed under the MIT license found in the

var _iterator = _createForOfIteratorHelper(nodesToInsertOrReplace),
_step;
for (const node of nodesToInsertOrReplace) {
if (!isModuleDeclaration( // $FlowExpectedError[incompatible-cast]
node)) {
continue;
}
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var node = _step.value;
if (!isModuleDeclaration( // $FlowExpectedError[incompatible-cast]
node)) {
continue;
}
return false;
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
return false;
}

@@ -53,0 +36,0 @@

/**
* Copyright (c) Facebook, Inc. and its affiliates.
* Copyright (c) Meta Platforms, Inc. and affiliates.
*

@@ -12,4 +12,2 @@ * This source code is licensed under the MIT license found in the

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
Object.defineProperty(exports, "__esModule", {

@@ -22,24 +20,17 @@ value: true

var _getTransformedAST2 = require("./getTransformedAST");
var _getTransformedAST = require("./getTransformedAST");
var _SimpleTraverser = require("../traverse/SimpleTraverser");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
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); }
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; }
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; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
function transform(originalCode, visitors, prettierOptions = {}) {
const {
ast,
astWasMutated,
mutatedCode
} = (0, _getTransformedAST.getTransformedAST)(originalCode, visitors);
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function transform(originalCode, visitors) {
var prettierOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
var _getTransformedAST = (0, _getTransformedAST2.getTransformedAST)(originalCode, visitors),
ast = _getTransformedAST.ast,
astWasMutated = _getTransformedAST.astWasMutated,
mutatedCode = _getTransformedAST.mutatedCode;
if (!astWasMutated) {

@@ -54,7 +45,9 @@ return originalCode;

_SimpleTraverser.SimpleTraverser.traverse(ast, {
enter: function enter(node) {
enter(node) {
// $FlowExpectedError[cannot-write]
delete node.parent;
},
leave: function leave() {}
leave() {}
}); // we need to delete the comments prop or else prettier will do

@@ -67,7 +60,10 @@ // its own attachment pass after the mutation and duplicate the

delete ast.comments;
return prettier.format(mutatedCode, _objectSpread(_objectSpread({}, prettierOptions), {}, {
parser: function parser() {
return prettier.format(mutatedCode, // $FlowExpectedError[incompatible-exact] - we don't want to create a dependency on the prettier types
{ ...prettierOptions,
parser() {
return ast;
}
}));
});
}

@@ -8,2 +8,4 @@ "use strict";

var _codeFrame = require("@babel/code-frame");
var _detachedNode = require("../detachedNode");

@@ -23,2 +25,4 @@

var _RemoveNode = require("./mutations/RemoveNode");
var _RemoveStatement = require("./mutations/RemoveStatement");

@@ -30,19 +34,16 @@

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function getTransformContext() {
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*
* @format
*/
function getTransformContext(code) {
/**
* The mutations in order of collection.
*/
var mutations = [];
const mutations = [];

@@ -55,12 +56,13 @@ function pushMutation(mutation) {

return {
mutations: mutations,
const cloneAPIs = {
// $FlowExpectedError[incompatible-exact]
shallowCloneNode: node => {
if (node == null) {
return null;
}
// $FlowExpectedError[unsafe-getters-setters]
get astWasMutated() {
return mutations.length > 0;
return (0, _detachedNode.shallowCloneNode)(node);
},
// $FlowExpectedError[incompatible-exact]
shallowCloneNode: function (node, newProps) {
shallowCloneNodeWithOverrides: (node, newProps) => {
if (node == null) {

@@ -72,3 +74,3 @@ return null;

},
shallowCloneArray: function (nodes) {
shallowCloneArray: nodes => {
if (nodes == null) {

@@ -78,8 +80,6 @@ return null;

return nodes.map(function (node) {
return (0, _detachedNode.shallowCloneNode)(node);
});
return nodes.map(node => (0, _detachedNode.shallowCloneNode)(node));
},
// $FlowExpectedError[incompatible-exact]
deepCloneNode: function (node, newProps) {
deepCloneNode: node => {
if (node == null) {

@@ -89,42 +89,34 @@ return null;

return (0, _detachedNode.deepCloneNode)(node);
},
// $FlowExpectedError[incompatible-exact]
deepCloneNodeWithOverrides: (node, newProps) => {
if (node == null) {
return null;
}
return (0, _detachedNode.deepCloneNode)(node, newProps);
}
};
const commentAPIs = {
getComments: node => {
return [...(0, _comments.getCommentsForNode)(node)];
},
insertAfterStatement: function (target, nodesToInsert) {
pushMutation((0, _InsertStatement.createInsertStatementMutation)('after', target, toArray(nodesToInsert)));
},
insertBeforeStatement: function (target, nodesToInsert) {
pushMutation((0, _InsertStatement.createInsertStatementMutation)('before', target, toArray(nodesToInsert)));
},
replaceNode: function (target, nodeToReplaceWith, options) {
pushMutation((0, _ReplaceNode.createReplaceNodeMutation)(target, nodeToReplaceWith, options));
},
replaceStatementWithMany: function (target, nodesToReplaceWith, options) {
pushMutation((0, _ReplaceStatementWithMany.createReplaceStatementWithManyMutation)(target, nodesToReplaceWith, options));
},
removeStatement: function (node) {
pushMutation((0, _RemoveStatement.createRemoveStatementMutation)(node));
},
//
// Comment APIs
//
getComments: function (node) {
return _toConsumableArray((0, _comments.getCommentsForNode)(node));
},
getLeadingComments: function (node) {
getLeadingComments: node => {
return (0, _comments.getCommentsForNode)(node).filter(_comments.isLeadingComment);
},
getTrailingComments: function (node) {
getTrailingComments: node => {
return (0, _comments.getCommentsForNode)(node).filter(_comments.isTrailingComment);
},
cloneCommentsTo: function (target, destination) {
cloneCommentsTo: (target, destination) => {
pushMutation((0, _CloneCommentsTo.createCloneCommentsToMutation)(target, destination));
},
addLeadingComments: function (node, comments) {
addLeadingComments: (node, comments) => {
pushMutation((0, _AddLeadingComments.createAddLeadingCommentsMutation)(node, toArray(comments)));
},
addTrailingComments: function (node, comments) {
addTrailingComments: (node, comments) => {
pushMutation((0, _AddTrailingComments.createAddTrailingCommentsMutation)(node, toArray(comments)));
},
removeComments: function (comments) {
toArray(comments).forEach(function (comment) {
removeComments: comments => {
toArray(comments).forEach(comment => {
pushMutation((0, _RemoveComment.createRemoveCommentMutation)(comment));

@@ -134,2 +126,62 @@ });

};
const insertAPIs = {
insertAfterStatement: (target, nodesToInsert) => {
pushMutation((0, _InsertStatement.createInsertStatementMutation)('after', target, toArray(nodesToInsert)));
},
insertBeforeStatement: (target, nodesToInsert) => {
pushMutation((0, _InsertStatement.createInsertStatementMutation)('before', target, toArray(nodesToInsert)));
}
};
const removeAPIs = {
removeNode: node => {
pushMutation((0, _RemoveNode.createRemoveNodeMutation)(node));
},
removeStatement: node => {
pushMutation((0, _RemoveStatement.createRemoveStatementMutation)(node));
}
};
const replaceAPIs = {
replaceNode: (target, nodeToReplaceWith, options) => {
pushMutation((0, _ReplaceNode.createReplaceNodeMutation)(target, nodeToReplaceWith, options));
},
replaceStatementWithMany: (target, nodesToReplaceWith, options) => {
pushMutation((0, _ReplaceStatementWithMany.createReplaceStatementWithManyMutation)(target, nodesToReplaceWith, options));
}
};
return {
mutations,
// $FlowExpectedError[unsafe-getters-setters]
get astWasMutated() {
return mutations.length > 0;
},
buildCodeFrame: (node, message) => {
// babel uses 1-indexed columns
const locForBabel = {
start: {
line: node.loc.start.line,
column: node.loc.start.column + 1
},
end: {
line: node.loc.end.line,
column: node.loc.end.column + 1
}
};
return (0, _codeFrame.codeFrameColumns)(code, locForBabel, {
linesAbove: 0,
linesBelow: 0,
highlightCode: process.env.NODE_ENV !== 'test',
message: message
});
},
buildSimpleCodeFrame: (node, message) => {
return `[${node.type}:${node.loc.start.line}:${node.loc.start.column}] ${message}`;
},
...cloneAPIs,
...commentAPIs,
...insertAPIs,
...removeAPIs,
...replaceAPIs
};
}

@@ -136,0 +188,0 @@

/**
* Copyright (c) Facebook, Inc. and its affiliates.
* Copyright (c) Meta Platforms, Inc. and affiliates.
*

@@ -19,3 +19,3 @@ * This source code is licensed under the MIT license found in the

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

@@ -25,15 +25,15 @@ // $FlowExpectedError[cannot-resolve-module]

/** Parse a selector and return its AST. */
var parse = _esquery["default"].parse;
const parse = _esquery.default.parse;
/** From a JS AST and a selector AST, collect all JS AST nodes that match the selector. */
exports.parse = parse;
var match = _esquery["default"].match;
const match = _esquery.default.match;
/** Given a `node` and its ancestors, determine if `node` is matched by `selector`. */
exports.match = match;
var matches = _esquery["default"].matches;
const matches = _esquery.default.matches;
/** Query the code AST using the selector string. */
exports.matches = matches;
var query = _esquery["default"].query;
const query = _esquery.default.query;
exports.query = query;
/**
* Copyright (c) Facebook, Inc. and its affiliates.
* Copyright (c) Meta Platforms, Inc. and affiliates.
*

@@ -12,4 +12,2 @@ * This source code is licensed under the MIT license found in the

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
Object.defineProperty(exports, "__esModule", {

@@ -24,32 +22,10 @@ value: true

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
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); }
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; }
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; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
var ESQUERY_OPTIONS = Object.freeze({
const ESQUERY_OPTIONS = Object.freeze({
visitorKeys: _hermesEslint.VisitorKeys,
fallback: function fallback(node) {
throw new Error("No visitor keys found for node type \"".concat(node.type, "\"."));
fallback: node => {
throw new Error(`No visitor keys found for node type "${node.type}".`);
}

@@ -63,8 +39,4 @@ });

function union() {
for (var _len = arguments.length, arrays = new Array(_len), _key = 0; _key < _len; _key++) {
arrays[_key] = arguments[_key];
}
return _toConsumableArray(new Set(arrays.flat()));
function union(...arrays) {
return [...new Set(arrays.flat())];
}

@@ -78,7 +50,3 @@ /**

function intersection() {
for (var _len2 = arguments.length, arrays = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
arrays[_key2] = arguments[_key2];
}
function intersection(...arrays) {
if (arrays.length === 0) {

@@ -88,22 +56,6 @@ return [];

var result = _toConsumableArray(new Set(arrays[0]));
let result = [...new Set(arrays[0])];
var _iterator = _createForOfIteratorHelper(arrays.slice(1)),
_step;
try {
var _loop = function _loop() {
var array = _step.value;
result = result.filter(function (x) {
return array.includes(x);
});
};
for (_iterator.s(); !(_step = _iterator.n()).done;) {
_loop();
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
for (const array of arrays.slice(1)) {
result = result.filter(x => array.includes(x));
}

@@ -124,3 +76,3 @@

if (!(parsedSelector.value in _hermesEslint.VisitorKeys)) {
throw new Error("Unexpected selector ".concat(parsedSelector.value));
throw new Error(`Unexpected selector ${parsedSelector.value}`);
} // $FlowExpectedError[incompatible-return]

@@ -133,7 +85,7 @@

{
var typesForComponents = parsedSelector.selectors.map(getPossibleTypes);
var typesForComponentsNonNull = typesForComponents.filter(Boolean);
const typesForComponents = parsedSelector.selectors.map(getPossibleTypes);
const typesForComponentsNonNull = typesForComponents.filter(Boolean);
if (typesForComponents.length === typesForComponentsNonNull.length) {
return union.apply(void 0, _toConsumableArray(typesForComponentsNonNull));
return union(...typesForComponentsNonNull);
}

@@ -146,6 +98,5 @@

{
var _typesForComponents = parsedSelector.selectors.map(getPossibleTypes).filter(Boolean); // If all of the components could match any type, then the compound could also match any type.
const typesForComponents = parsedSelector.selectors.map(getPossibleTypes).filter(Boolean); // If all of the components could match any type, then the compound could also match any type.
if (!_typesForComponents.length) {
if (!typesForComponents.length) {
return null;

@@ -159,3 +110,3 @@ }

return intersection.apply(void 0, _toConsumableArray(_typesForComponents));
return intersection(...typesForComponents);
}

@@ -198,5 +149,3 @@

case 'matches':
return parsedSelector.selectors.reduce(function (sum, childSelector) {
return sum + countClassAttributes(childSelector);
}, 0);
return parsedSelector.selectors.reduce((sum, childSelector) => sum + countClassAttributes(childSelector), 0);

@@ -231,5 +180,3 @@ case 'attribute':

case 'matches':
return parsedSelector.selectors.reduce(function (sum, childSelector) {
return sum + countIdentifiers(childSelector);
}, 0);
return parsedSelector.selectors.reduce((sum, childSelector) => sum + countIdentifiers(childSelector), 0);

@@ -268,6 +215,6 @@ case 'identifier':

try {
return esquery.parse(rawSelector.replace(/:exit$/, ''));
return esquery.parse(rawSelector.replace(/:exit$/u, ''));
} catch (err) {
if (err.location && err.location.start && typeof err.location.start.offset === 'number') {
throw new SyntaxError("Syntax error in selector \"".concat(rawSelector, "\" at position ").concat(err.location.start.offset, ": ").concat(err.message));
throw new SyntaxError(`Syntax error in selector "${rawSelector}" at position ${err.location.start.offset}: ${err.message}`);
}

@@ -279,3 +226,3 @@

var selectorCache = new Map();
const selectorCache = new Map();
/**

@@ -288,3 +235,3 @@ * Parses a raw selector string, and returns the parsed selector along with specificity and type information.

function parseSelector(rawSelector) {
var cachedSelector = selectorCache.get(rawSelector);
const cachedSelector = selectorCache.get(rawSelector);

@@ -295,7 +242,7 @@ if (cachedSelector) {

var parsedSelector = tryParseSelector(rawSelector);
var result = {
rawSelector: rawSelector,
const parsedSelector = tryParseSelector(rawSelector);
const result = {
rawSelector,
isExit: rawSelector.endsWith(':exit'),
parsedSelector: parsedSelector,
parsedSelector,
listenerTypes: getPossibleTypes(parsedSelector),

@@ -313,3 +260,3 @@ attributeCount: countClassAttributes(parsedSelector),

var NodeEventGenerator = /*#__PURE__*/function () {
class NodeEventGenerator {
/**

@@ -321,27 +268,17 @@ * @param emitter

*/
function NodeEventGenerator(emitter) {
var _this = this;
_classCallCheck(this, NodeEventGenerator);
_defineProperty(this, "emitter", void 0);
_defineProperty(this, "_currentAncestry", []);
_defineProperty(this, "_enterSelectorsByNodeType", new Map());
_defineProperty(this, "_exitSelectorsByNodeType", new Map());
_defineProperty(this, "_anyTypeEnterSelectors", []);
_defineProperty(this, "_anyTypeExitSelectors", []);
constructor(emitter) {
this.emitter = void 0;
this._currentAncestry = [];
this._enterSelectorsByNodeType = new Map();
this._exitSelectorsByNodeType = new Map();
this._anyTypeEnterSelectors = [];
this._anyTypeExitSelectors = [];
this.emitter = emitter;
emitter.eventNames().forEach(function (rawSelector) {
var selector = parseSelector(rawSelector);
emitter.eventNames().forEach(rawSelector => {
const selector = parseSelector(rawSelector);
if (selector.listenerTypes) {
var typeMap = selector.isExit ? _this._exitSelectorsByNodeType : _this._enterSelectorsByNodeType;
selector.listenerTypes.forEach(function (nodeType) {
var selectors = typeMap.get(nodeType);
const typeMap = selector.isExit ? this._exitSelectorsByNodeType : this._enterSelectorsByNodeType;
selector.listenerTypes.forEach(nodeType => {
const selectors = typeMap.get(nodeType);

@@ -355,3 +292,3 @@ if (!selectors) {

} else {
var selectors = selector.isExit ? _this._anyTypeExitSelectors : _this._anyTypeEnterSelectors;
const selectors = selector.isExit ? this._anyTypeExitSelectors : this._anyTypeEnterSelectors;
selectors.push(selector);

@@ -365,9 +302,5 @@ }

this._enterSelectorsByNodeType.forEach(function (selectorList) {
return selectorList.sort(compareSpecificity);
});
this._enterSelectorsByNodeType.forEach(selectorList => selectorList.sort(compareSpecificity));
this._exitSelectorsByNodeType.forEach(function (selectorList) {
return selectorList.sort(compareSpecificity);
});
this._exitSelectorsByNodeType.forEach(selectorList => selectorList.sort(compareSpecificity));
}

@@ -382,66 +315,59 @@ /**

_createClass(NodeEventGenerator, [{
key: "_applySelector",
value: function _applySelector(node, selector) {
if (esquery.matches(node, selector.parsedSelector, this._currentAncestry, ESQUERY_OPTIONS)) {
this.emitter.emit(selector.rawSelector, node);
}
_applySelector(node, selector) {
if (esquery.matches(node, selector.parsedSelector, this._currentAncestry, ESQUERY_OPTIONS)) {
this.emitter.emit(selector.rawSelector, node);
}
/**
* Applies all appropriate selectors to a node, in specificity order
* @param node The node to check
* @param isExit `false` if the node is currently being entered, `true` if it's currently being exited
* @private
}
/**
* Applies all appropriate selectors to a node, in specificity order
* @param node The node to check
* @param isExit `false` if the node is currently being entered, `true` if it's currently being exited
* @private
*/
_applySelectors(node, isExit) {
const selectorsByNodeType = (isExit ? this._exitSelectorsByNodeType : this._enterSelectorsByNodeType).get(node.type) || [];
const anyTypeSelectors = isExit ? this._anyTypeExitSelectors : this._anyTypeEnterSelectors;
/*
* selectorsByNodeType and anyTypeSelectors were already sorted by specificity in the constructor.
* Iterate through each of them, applying selectors in the right order.
*/
}, {
key: "_applySelectors",
value: function _applySelectors(node, isExit) {
var selectorsByNodeType = (isExit ? this._exitSelectorsByNodeType : this._enterSelectorsByNodeType).get(node.type) || [];
var anyTypeSelectors = isExit ? this._anyTypeExitSelectors : this._anyTypeEnterSelectors;
/*
* selectorsByNodeType and anyTypeSelectors were already sorted by specificity in the constructor.
* Iterate through each of them, applying selectors in the right order.
*/
let selectorsByTypeIndex = 0;
let anyTypeSelectorsIndex = 0;
var selectorsByTypeIndex = 0;
var anyTypeSelectorsIndex = 0;
while (selectorsByTypeIndex < selectorsByNodeType.length || anyTypeSelectorsIndex < anyTypeSelectors.length) {
if (selectorsByTypeIndex >= selectorsByNodeType.length || anyTypeSelectorsIndex < anyTypeSelectors.length && compareSpecificity(anyTypeSelectors[anyTypeSelectorsIndex], selectorsByNodeType[selectorsByTypeIndex]) < 0) {
this._applySelector(node, anyTypeSelectors[anyTypeSelectorsIndex++]);
} else {
this._applySelector(node, selectorsByNodeType[selectorsByTypeIndex++]);
}
while (selectorsByTypeIndex < selectorsByNodeType.length || anyTypeSelectorsIndex < anyTypeSelectors.length) {
if (selectorsByTypeIndex >= selectorsByNodeType.length || anyTypeSelectorsIndex < anyTypeSelectors.length && compareSpecificity(anyTypeSelectors[anyTypeSelectorsIndex], selectorsByNodeType[selectorsByTypeIndex]) < 0) {
this._applySelector(node, anyTypeSelectors[anyTypeSelectorsIndex++]);
} else {
this._applySelector(node, selectorsByNodeType[selectorsByTypeIndex++]);
}
}
/**
* Emits an event of entering AST node.
* @param node A node which was entered.
*/
}
/**
* Emits an event of entering AST node.
* @param node A node which was entered.
*/
}, {
key: "enterNode",
value: function enterNode(node) {
this._applySelectors(node, false);
this._currentAncestry.unshift(node);
}
/**
* Emits an event of leaving AST node.
* @param node A node which was left.
*/
enterNode(node) {
this._applySelectors(node, false);
}, {
key: "leaveNode",
value: function leaveNode(node) {
this._currentAncestry.shift();
this._currentAncestry.unshift(node);
}
/**
* Emits an event of leaving AST node.
* @param node A node which was left.
*/
this._applySelectors(node, true);
}
}]);
return NodeEventGenerator;
}();
leaveNode(node) {
this._currentAncestry.shift();
this._applySelectors(node, true);
}
}
exports.NodeEventGenerator = NodeEventGenerator;
/**
* Copyright (c) Facebook, Inc. and its affiliates.
* Copyright (c) Meta Platforms, Inc. and affiliates.
*

@@ -17,10 +17,2 @@ * This source code is licensed under the MIT license found in the

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
/**

@@ -36,37 +28,27 @@ * Creates an object which can listen for and emit events.

*/
var SafeEmitter = /*#__PURE__*/function () {
function SafeEmitter() {
_classCallCheck(this, SafeEmitter);
class SafeEmitter {
constructor() {
this.listeners = Object.create(null);
}
_defineProperty(this, "listeners", Object.create(null));
on(eventName, listener) {
if (eventName in this.listeners) {
this.listeners[eventName].push(listener);
} else {
this.listeners[eventName] = [listener];
}
}
_createClass(SafeEmitter, [{
key: "on",
value: function on(eventName, listener) {
if (eventName in this.listeners) {
this.listeners[eventName].push(listener);
} else {
this.listeners[eventName] = [listener];
}
emit(eventName, node) {
if (eventName in this.listeners) {
this.listeners[eventName].forEach(listener => listener(node));
}
}, {
key: "emit",
value: function emit(eventName, node) {
if (eventName in this.listeners) {
this.listeners[eventName].forEach(function (listener) {
return listener(node);
});
}
}
}, {
key: "eventNames",
value: function eventNames() {
return Object.keys(this.listeners);
}
}]);
}
return SafeEmitter;
}();
eventNames() {
return Object.keys(this.listeners);
}
}
exports.SafeEmitter = SafeEmitter;
/**
* Copyright (c) Facebook, Inc. and its affiliates.
* Copyright (c) Meta Platforms, Inc. and affiliates.
*

@@ -19,14 +19,2 @@ * This source code is licensed under the MIT license found in the

function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
/**

@@ -37,3 +25,3 @@ * Can be thrown within the traversal "enter" function to prevent the traverser

*/
var SimpleTraverserSkip = new Error();
const SimpleTraverserSkip = new Error();
/**

@@ -45,3 +33,3 @@ * Can be thrown at any point during the traversal to immediately stop traversal

exports.SimpleTraverserSkip = SimpleTraverserSkip;
var SimpleTraverserBreak = new Error();
const SimpleTraverserBreak = new Error();
/**

@@ -53,101 +41,82 @@ * A very simple traverser class to traverse AST trees.

var SimpleTraverser = /*#__PURE__*/function () {
function SimpleTraverser() {
_classCallCheck(this, SimpleTraverser);
class SimpleTraverser {
/**
* Traverse the given AST tree.
* @param node The root node to traverse.
* @param options The option object.
*/
traverse(node, options) {
try {
this._traverse(node, null, options);
} catch (ex) {
if (ex === SimpleTraverserBreak) {
return;
}
throw ex;
}
}
/**
* Traverse the given AST tree recursively.
* @param node The current node.
* @param parent The parent node.
* @private
*/
_createClass(SimpleTraverser, [{
key: "traverse",
value:
/**
* Traverse the given AST tree.
* @param node The root node to traverse.
* @param options The option object.
*/
function traverse(node, options) {
try {
this._traverse(node, null, options);
} catch (ex) {
if (ex === SimpleTraverserBreak) {
return;
}
throw ex;
}
_traverse(node, parent, options) {
if (!(0, _getVisitorKeys.isNode)(node)) {
return;
}
/**
* Traverse the given AST tree recursively.
* @param node The current node.
* @param parent The parent node.
* @private
*/
}, {
key: "_traverse",
value: function _traverse(node, parent, options) {
if (!(0, _getVisitorKeys.isNode)(node)) {
try {
options.enter(node, parent);
} catch (ex) {
if (ex === SimpleTraverserSkip) {
return;
}
try {
options.enter(node, parent);
} catch (ex) {
if (ex === SimpleTraverserSkip) {
return;
}
throw ex;
}
throw ex;
}
const keys = (0, _getVisitorKeys.getVisitorKeys)(node);
var keys = (0, _getVisitorKeys.getVisitorKeys)(node);
for (const key of keys) {
// $FlowExpectedError[prop-missing]
const child = node[key];
var _iterator = _createForOfIteratorHelper(keys),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var key = _step.value;
// $FlowExpectedError[prop-missing]
var child = node[key];
if (Array.isArray(child)) {
for (var j = 0; j < child.length; ++j) {
this._traverse(child[j], node, options);
}
} else {
this._traverse(child, node, options);
}
if (Array.isArray(child)) {
for (let j = 0; j < child.length; ++j) {
this._traverse(child[j], node, options);
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
} else {
this._traverse(child, node, options);
}
}
try {
options.leave(node, parent);
} catch (ex) {
if (ex === SimpleTraverserSkip) {
return;
}
throw ex;
try {
options.leave(node, parent);
} catch (ex) {
if (ex === SimpleTraverserSkip) {
return;
}
}
/**
* Traverse the given AST tree.
* @param node The root node to traverse.
* @param options The option object.
*/
}], [{
key: "traverse",
value: function traverse(node, options) {
new SimpleTraverser().traverse(node, options);
throw ex;
}
}]);
}
/**
* Traverse the given AST tree.
* @param node The root node to traverse.
* @param options The option object.
*/
return SimpleTraverser;
}();
exports.SimpleTraverser = SimpleTraverser;
static traverse(node, options) {
new SimpleTraverser().traverse(node, options);
}
}
exports.SimpleTraverser = SimpleTraverser;
SimpleTraverser.Break = SimpleTraverserBreak;
SimpleTraverser.Skip = SimpleTraverserSkip;

@@ -15,14 +15,12 @@ "use strict";

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*
* @format
*/
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
/**

@@ -35,8 +33,8 @@ * Traverse the AST with additional context members provided by `additionalContext`.

function traverseWithContext(ast, scopeManager, additionalContext, visitor) {
var emitter = new _SafeEmitter.SafeEmitter();
var nodeQueue = [];
var currentNode = ast; // set parent pointers and build up the traversal queue
const emitter = new _SafeEmitter.SafeEmitter();
const nodeQueue = [];
let currentNode = ast; // set parent pointers and build up the traversal queue
_SimpleTraverser.SimpleTraverser.traverse(ast, {
enter: function enter(node, parent) {
enter(node, parent) {
// $FlowExpectedError[cannot-write] - hermes doesn't set this

@@ -46,20 +44,21 @@ node.parent = parent;

isEntering: true,
node: node
node
});
},
leave: function leave(node) {
leave(node) {
nodeQueue.push({
isEntering: false,
node: node
node
});
}
});
var getScope = function getScope() {
var givenNode = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : currentNode;
const getScope = (givenNode = currentNode) => {
// On Program node, get the outermost scope to avoid return Node.js special function scope or ES modules scope.
var inner = givenNode.type !== 'Program';
const inner = givenNode.type !== 'Program';
for (var _node = givenNode; _node; _node = _node.parent) {
var scope = scopeManager.acquire(_node, inner);
for (let node = givenNode; node; node = node.parent) {
const scope = scopeManager.acquire(node, inner);

@@ -78,25 +77,12 @@ if (scope) {

var traversalContextBase = Object.freeze({
getDeclaredVariables: function getDeclaredVariables(node) {
return scopeManager.getDeclaredVariables(node);
},
getBinding: function getBinding(name) {
var currentScope = getScope();
const traversalContextBase = Object.freeze({
getDeclaredVariables: node => scopeManager.getDeclaredVariables(node),
getBinding: name => {
let currentScope = getScope();
while (currentScope != null) {
var _iterator = _createForOfIteratorHelper(currentScope.variables),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var variable = _step.value;
if (variable.defs.length && variable.name === name) {
return variable;
}
for (const variable of currentScope.variables) {
if (variable.defs.length && variable.name === name) {
return variable;
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}

@@ -109,12 +95,14 @@

},
getScope: getScope
getScope
});
var traversalContext = Object.freeze(_objectSpread(_objectSpread({}, traversalContextBase), additionalContext(traversalContextBase)));
var selectors = visitor(traversalContext); // add all the selectors from the visitor as listeners
const traversalContext = Object.freeze({ ...traversalContextBase,
...additionalContext(traversalContextBase)
});
const selectors = visitor(traversalContext); // add all the selectors from the visitor as listeners
Object.keys(selectors).forEach(function (selector) {
Object.keys(selectors).forEach(selector => {
// flow doesn't want us to be general here - but it's safe
// $FlowExpectedError[incompatible-type]
// $FlowExpectedError[prop-missing]
var listener = selectors[selector];
const listener = selectors[selector];

@@ -125,4 +113,4 @@ if (listener) {

});
var eventGenerator = new _NodeEventGenerator.NodeEventGenerator(emitter);
nodeQueue.forEach(function (traversalInfo) {
const eventGenerator = new _NodeEventGenerator.NodeEventGenerator(emitter);
nodeQueue.forEach(traversalInfo => {
currentNode = traversalInfo.node;

@@ -144,3 +132,3 @@

function traverse(ast, scopeManager, visitor) {
traverseWithContext(ast, scopeManager, function () {}, visitor);
traverseWithContext(ast, scopeManager, () => {}, visitor);
}
{
"name": "hermes-transform",
"version": "0.5.0",
"version": "0.6.0",
"description": "Tools built on top of Hermes-ESTree to enable codebase transformation",

@@ -12,5 +12,6 @@ "main": "dist/index.js",

"dependencies": {
"@babel/code-frame": "^7.16.0",
"esquery": "^1.4.0",
"hermes-eslint": "0.5.0",
"hermes-estree": "0.5.0"
"hermes-eslint": "0.6.0",
"hermes-estree": "0.6.0"
},

@@ -17,0 +18,0 @@ "peerDependencies": {

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc