Security News
New Python Packaging Proposal Aims to Solve Phantom Dependency Problem with SBOMs
PEP 770 proposes adding SBOM support to Python packages to improve transparency and catch hidden non-Python dependencies that security tools often miss.
@putout/printer
Advanced tools
Easiest possible opinionated Babel AST printer made with ❤️ to use in 🐊Putout
Prints Babel AST to readable JavaScript.
Supports:
npm i @putout/printer
const {print} = require('@putout/printer');
const {parse} = require('@babel/parser');
const ast = parse('const a = (b, c) => {const d = 5; return a;}');
print(ast);
// returns
`
const a = (b, c) => {
const d = 5;
return a;
};
`;
When you need to extend syntax of @putout/printer
just pass a function which receives:
path
, Babel Pathprint
, a function to output result of printing into token array;When path
contains to dashes __
and name, it is the same as: print(path.get('right'))
, and this is
actually traverse(path.get('right'))
shortened to simplify read and process.
Here is how you can override AssignmentPattern
:
const ast = parse('const {a = 5} = b');
print(ast, {
format: {
indent: ' ',
},
visitors: {
AssignmentPattern(path, {print}) {
print(' /* [hello world] */= ');
print('__right');
},
},
});
// returns
'const {a /* [hello world] */= 5} = b;\n';
print
Used in previous example print
can be used for a couple purposes:
string
;node
when object
passed;node
when string
started with __
;print(ast, {
visitors: {
AssignmentPattern(path, {print, maybe}) {
maybe.print.newline(path.parentPath.isCallExpression());
print(' /* [hello world] */= ');
print('__right');
},
},
});
maybe
When you need some condition use maybe
. For example, to add newline only when parent node is CallExpression
you
can use maybe.print.newline(condition)
:
print(ast, {
visitors: {
AssignmentPattern(path, {print, maybe}) {
maybe.print.newline(path.parentPath.isCallExpression());
print(' /* [hello world] */= ');
print('__right');
},
},
});
write
When are you going to output string you can use low-level function write
:
print(ast, {
visitors: {
BlockStatement(path, {write}) {
write('hello');
},
},
});
indent
When you need to add indentation use indent
, for example when you output body,
you need to increment indentation, and then decrement it back:
print(ast, {
visitors: {
BlockStatement(path, {write, indent}) {
write('{');
indent.inc();
indent();
write('some;');
indent.dec();
write('{');
},
},
});
traverse
When are you needing to traverse node, you can use traverse
:
print(ast, {
visitors: {
AssignmentExpression(path, {traverse}) {
traverse(path.get('left'));
},
},
});
This is the same as print('__left')
but more low-level, and supports only objects.
MIT
FAQs
Simplest possible opinionated Babel AST printer for 🐊Putout
The npm package @putout/printer receives a total of 4,026 weekly downloads. As such, @putout/printer popularity was classified as popular.
We found that @putout/printer demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PEP 770 proposes adding SBOM support to Python packages to improve transparency and catch hidden non-Python dependencies that security tools often miss.
Security News
Socket CEO Feross Aboukhadijeh discusses open source security challenges, including zero-day attacks and supply chain risks, on the Cyber Security Council podcast.
Security News
Research
Socket researchers uncover how threat actors weaponize Out-of-Band Application Security Testing (OAST) techniques across the npm, PyPI, and RubyGems ecosystems to exfiltrate sensitive data.