Socket
Socket
Sign inDemoInstall

hermes-transform

Package Overview
Dependencies
Maintainers
1
Versions
32
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.6.0 to 0.7.0

dist/transform/mutations/AddComments.js

135

dist/transform/comments/comments.js

@@ -6,5 +6,5 @@ "use strict";

});
exports.CommentPlacement = void 0;
exports.addComment = addComment;
exports.addCommentsToNode = addCommentsToNode;
exports.addLeadingComment = addLeadingComment;
exports.addTrailingComment = addTrailingComment;
exports.appendCommentToSource = appendCommentToSource;

@@ -28,2 +28,4 @@ exports.attachComments = attachComments;

var _os = require("os");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

@@ -43,2 +45,6 @@

// $FlowExpectedError[untyped-import]
const CommentPlacement = require("flow-enums-runtime").Mirrored(["LEADING_OWN_LINE", "LEADING_INLINE", "TRAILING_OWN_LINE", "TRAILING_INLINE"]);
exports.CommentPlacement = CommentPlacement;
function attachComments(comments, ast, text) {

@@ -88,8 +94,18 @@ (0, _comments.attach)(comments, ast, text, {

function addLeadingComment(node, comment) {
(0, _util.addLeadingComment)(node, comment);
}
function addComment(node, comment, placement) {
switch (placement) {
case CommentPlacement.LEADING_OWN_LINE:
case CommentPlacement.LEADING_INLINE:
{
(0, _util.addLeadingComment)(node, comment);
break;
}
function addTrailingComment(node, comment) {
(0, _util.addTrailingComment)(node, comment);
case CommentPlacement.TRAILING_OWN_LINE:
case CommentPlacement.TRAILING_INLINE:
{
(0, _util.addTrailingComment)(node, comment);
break;
}
}
}

@@ -119,27 +135,98 @@

function appendCommentToSource(code, comment) {
// prettier slices comments directly from the source code when printing
// https://github.com/prettier/prettier/blob/5f0ee39fa03532c85bd1c35291450fe7ac3667b3/src/language-js/print/comment.js#L15-L20
// this means that we need to have any appended comments directly in the
// source code or else prettier will slice nothing and bork up the transform
let commentText = comment.value;
function getFirstNewlineIndex(code) {
return code.search(/\r\n|\n|\r/);
}
function getFirstNonWhitespaceIndex(code) {
return code.search(/\S/);
}
function appendCommentToSource(code, comment, placement) {
let newCode = code;
switch (comment.type) {
case 'Block':
commentText = `/*${commentText}*/`;
break;
{
// Prettier decides if a newline is necessary between the comment and its node by looking
// to see if a newline seperates them in the source text. We can trick prettier into
// formatting how we want for new comments by placing the range such that a newline
// will (OWN_LINE) or will not (INLINE) be found when searching from the specified range
// position.
switch (placement) {
case CommentPlacement.LEADING_OWN_LINE:
case CommentPlacement.TRAILING_OWN_LINE:
{
// Since we always want a line break we need to ensure a newline is found when
// searching out from either side of the comment range.
let firstNewline = getFirstNewlineIndex(code);
if (firstNewline === -1) {
// No newline in file, lets add one.
newCode += _os.EOL;
firstNewline = newCode.length;
} // Prettier only uses these ranges for detecting whitespace, so this nonsensical
// range is safe.
// $FlowExpectedError[cannot-write]
comment.range = [firstNewline + 1, firstNewline];
break;
}
case CommentPlacement.LEADING_INLINE:
case CommentPlacement.TRAILING_INLINE:
{
// Since we don't want a line break we need to ensure a non whitespace char is
// always found before a newline when searching out from either side of the
// comment range.
let firstNonWhitespace = getFirstNonWhitespaceIndex(code);
if (firstNonWhitespace === -1) {
// No non whitespace chars in file, lets add an identifiable statement for prettier to find.
newCode += '$FORCE_INLINE_ON_EMPTY_FILE_TOKEN$;';
firstNonWhitespace = newCode.length;
break;
} // $FlowExpectedError[cannot-write]
comment.range = [firstNonWhitespace + 1, firstNonWhitespace];
break;
}
}
break;
}
case 'Line':
commentText = `//${commentText}`;
break;
{
// For `Line` comments prettier slices comments directly from the source code when printing
// https://github.com/prettier/prettier/blob/5f0ee39fa03532c85bd1c35291450fe7ac3667b3/src/language-js/print/comment.js#L15-L20
// this means that we need to have any appended comments directly in the
// source code or else prettier will slice nothing and bork up the transform
const commentText = `//${comment.value}`;
const lastChar = newCode[newCode.length - 1];
if (lastChar !== '\n' && lastChar !== '\r') {
newCode += _os.EOL;
} // Line comments cannot be inline before a node so we only place trailing Line comments inline.
if (placement === CommentPlacement.TRAILING_INLINE) {
// Prettier determines an "end of line" comment by walking backwards from
// the comment start range through the source code to see if it finds a non
// newline token. In order to trick prettier for new comments we need to
// insert fake source code for it to find.
newCode += '$FORCE_END_OF_LINE_COMMENT_TOKEN$;';
}
const start = newCode.length;
newCode += commentText;
const end = newCode.length; // $FlowExpectedError[cannot-write]
comment.range = [start, end];
break;
}
}
let newCode = code;
newCode += '\n';
const start = newCode.length;
newCode += commentText;
const end = newCode.length; // $FlowExpectedError[cannot-write]
comment.range = [start, end];
return newCode;
}

17

dist/transform/getTransformedAST.js

@@ -29,6 +29,4 @@ /**

var _AddLeadingComments = require("./mutations/AddLeadingComments");
var _AddComments = require("./mutations/AddComments");
var _AddTrailingComments = require("./mutations/AddTrailingComments");
var _CloneCommentsTo = require("./mutations/CloneCommentsTo");

@@ -59,4 +57,4 @@

const transformContext = (0, _TransformContext.getTransformContext)(code);
(0, _traverse.traverseWithContext)(ast, scopeManager, () => transformContext, visitors); // apply the mutations to the AST
const transformContext = (0, _TransformContext.getTransformContext)();
(0, _traverse.traverseWithContext)(code, ast, scopeManager, () => transformContext, visitors); // apply the mutations to the AST

@@ -101,12 +99,7 @@ const mutationContext = new _MutationContext.MutationContext(code);

case 'addLeadingComments':
case 'addComments':
{
return (0, _AddLeadingComments.performAddLeadingCommentsMutation)(mutationContext, mutation);
return (0, _AddComments.performAddCommentsMutation)(mutationContext, mutation);
}
case 'addTrailingComments':
{
return (0, _AddTrailingComments.performAddTrailingCommentsMutation)(mutationContext, mutation);
}
case 'cloneCommentsTo':

@@ -113,0 +106,0 @@ {

@@ -75,4 +75,4 @@ "use strict";

appendCommentToSource(comment) {
this.code = (0, _comments.appendCommentToSource)(this.code, comment);
appendCommentToSource(comment, placement) {
this.code = (0, _comments.appendCommentToSource)(this.code, comment, placement);
}

@@ -79,0 +79,0 @@

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

function performCloneCommentsToMutation(mutationContext, mutation) {
function performCloneCommentsToMutation(_mutationContext, mutation) {
const newComments = [];

@@ -34,3 +34,2 @@

const comment = (0, _comments.cloneCommentWithMarkers)(originalComment);
mutationContext.appendCommentToSource(comment);
newComments.push(comment);

@@ -37,0 +36,0 @@ }

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

{
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, mutation.nodesToInsert);
break;

@@ -63,0 +63,0 @@ }

@@ -19,3 +19,15 @@ "use strict";

*/
function assertArrayBounds(array, index) {
if (index < 0 || index >= array.length) {
throw new Error(`Invalid Mutation: Tried to mutate an elements array with an out of bounds index. Index: ${index}, Array Size: ${array.length}`);
}
}
function insertInArray(array, index, elements) {
if (index === array.length) {
// Support the insert at end of array case.
return array.concat(elements);
}
assertArrayBounds(array, index);
return array.slice(0, index).concat(elements).concat(array.slice(index));

@@ -25,2 +37,3 @@ }

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

@@ -30,3 +43,4 @@ }

function replaceInArray(array, index, elements) {
assertArrayBounds(array, index);
return array.slice(0, index).concat(elements).concat(array.slice(index + 1));
}

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

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

@@ -15,6 +13,4 @@

var _AddLeadingComments = require("./mutations/AddLeadingComments");
var _AddComments = require("./mutations/AddComments");
var _AddTrailingComments = require("./mutations/AddTrailingComments");
var _CloneCommentsTo = require("./mutations/CloneCommentsTo");

@@ -43,3 +39,3 @@

*/
function getTransformContext(code) {
function getTransformContext() {
/**

@@ -57,3 +53,2 @@ * The mutations in order of collection.

const cloneAPIs = {
// $FlowExpectedError[incompatible-exact]
shallowCloneNode: node => {

@@ -81,3 +76,2 @@ if (node == null) {

},
// $FlowExpectedError[incompatible-exact]
deepCloneNode: node => {

@@ -113,7 +107,25 @@ if (node == null) {

addLeadingComments: (node, comments) => {
pushMutation((0, _AddLeadingComments.createAddLeadingCommentsMutation)(node, toArray(comments)));
pushMutation((0, _AddComments.createAddCommentsMutation)(node, toArray(comments).map(comment => ({
comment,
placement: _comments.CommentPlacement.LEADING_OWN_LINE
}))));
},
addLeadingInlineComments: (node, comments) => {
pushMutation((0, _AddComments.createAddCommentsMutation)(node, toArray(comments).map(comment => ({
comment,
placement: _comments.CommentPlacement.LEADING_INLINE
}))));
},
addTrailingComments: (node, comments) => {
pushMutation((0, _AddTrailingComments.createAddTrailingCommentsMutation)(node, toArray(comments)));
pushMutation((0, _AddComments.createAddCommentsMutation)(node, toArray(comments).map(comment => ({
comment,
placement: _comments.CommentPlacement.TRAILING_OWN_LINE
}))));
},
addTrailingInlineComments: (node, comments) => {
pushMutation((0, _AddComments.createAddCommentsMutation)(node, toArray(comments).map(comment => ({
comment,
placement: _comments.CommentPlacement.TRAILING_INLINE
}))));
},
removeComments: comments => {

@@ -157,24 +169,2 @@ toArray(comments).forEach(comment => {

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,

@@ -181,0 +171,0 @@ ...commentAPIs,

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

var _codeFrame = require("@babel/code-frame");
var _NodeEventGenerator = require("./NodeEventGenerator");

@@ -32,3 +34,3 @@

*/
function traverseWithContext(ast, scopeManager, additionalContext, visitor) {
function traverseWithContext(code, ast, scopeManager, additionalContext, visitor) {
const emitter = new _SafeEmitter.SafeEmitter();

@@ -77,2 +79,24 @@ const nodeQueue = [];

const traversalContextBase = Object.freeze({
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}`;
},
getDeclaredVariables: node => scopeManager.getDeclaredVariables(node),

@@ -128,4 +152,4 @@ getBinding: name => {

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

@@ -14,4 +14,5 @@ "main": "dist/index.js",

"esquery": "^1.4.0",
"hermes-eslint": "0.6.0",
"hermes-estree": "0.6.0"
"flow-enums-runtime": "^0.0.6",
"hermes-eslint": "0.7.0",
"hermes-estree": "0.7.0"
},

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

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