Server IP : 185.86.78.101 / Your IP : 216.73.216.124 Web Server : Apache System : Linux 675867-vds-valikoshka1996.gmhost.pp.ua 5.4.0-150-generic #167-Ubuntu SMP Mon May 15 17:35:05 UTC 2023 x86_64 User : www ( 1000) PHP Version : 7.4.33 Disable Function : passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /www/wwwroot/mifepriston.org/node_modules/jscodeshift/recipes/ |
Upload File : |
# Retain comment on first line ## Problem When removing or replacing the first statement in a file, it is possible for [leading comments at the top of the file to be removed](https://github.com/facebook/jscodeshift/issues/44). ## Solution To retain the leading comments during a transformation, the `comments` array on the statement's `node` _must_ be copied to the next statement's `node` that will be at the top of the file. ## Examples #### Bad ##### Transform ```javascript export default function transformer(file, api) { const j = api.jscodeshift; return j(file.source) .find(j.VariableDeclaration) .replaceWith( j.expressionStatement(j.callExpression( j.identifier('foo'), [] ) ) ) .toSource(); }; ``` ##### In ```javascript // Comment on first line const firstStatement = require('some-module'); ``` ##### Out ```javascript foo(); ``` #### Good ##### Transform ```javascript export default function transformer(file, api) { const j = api.jscodeshift; const root = j(file.source); const getFirstNode = () => root.find(j.Program).get('body', 0).node; // Save the comments attached to the first node const firstNode = getFirstNode(); const { comments } = firstNode; root.find(j.VariableDeclaration).replaceWith( j.expressionStatement(j.callExpression( j.identifier('foo'), [] )) ); // If the first node has been modified or deleted, reattach the comments const firstNode2 = getFirstNode(); if (firstNode2 !== firstNode) { firstNode2.comments = comments; } return root.toSource(); }; ``` ##### In ```javascript // Comment on first line const firstStatement = require('some-module'); ``` ##### Out ```javascript // Comment on first line foo(); ```