New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

ast-pretty-print

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ast-pretty-print - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0

test.js

135

index.js
// @flow
'use strict';
const ansiStyles = require('ansi-styles');
/*::
type ThemeColor = {
open: string,
close: string,
};
type Theme = {
location: ThemeColor,
brackets: ThemeColor,
string: ThemeColor,
number: ThemeColor,
boolean: ThemeColor,
undefined: ThemeColor,
null: ThemeColor,
node: ThemeColor,
key: ThemeColor,
};
type Location = {

@@ -22,5 +41,5 @@ line: number,

let printArray = (arr, indentation) => {
let printArray = (arr, indentation, theme) => {
if (arr.length === 0) {
return '[]';
return theme.brackets.open + '[]' + theme.brackets.close;
}

@@ -31,3 +50,4 @@

for (let i = 0; i < arr.length; i++) {
res += '\n' + indentation + '- ' + printValue(arr[i], indentation + ' ');
res +=
'\n' + indentation + '- ' + printValue(arr[i], indentation + ' ', theme);
}

@@ -38,12 +58,20 @@

let printKeyValue = (key, val, indentation) => {
return '\n' + indentation + key + ': ' + printValue(val, indentation);
let printKeyValue = (key, val, indentation, theme) => {
return (
'\n' +
indentation +
theme.key.open +
key +
theme.key.close +
': ' +
printValue(val, indentation, theme)
);
};
let printObject = (obj /*: Object */, indentation) => {
if (isNodeLike(obj)) return printNode(obj, indentation);
let printObject = (obj /*: Object */, indentation, theme) => {
if (isNodeLike(obj)) return printNode(obj, indentation, theme);
let keys = Object.keys(obj);
if (keys.length === 0) {
return '{}';
return theme.brackets.open + '{}' + theme.brackets.close;
}

@@ -57,3 +85,3 @@

let key = keys[i];
res += printKeyValue(key, obj[key], indentation + ' ');
res += printKeyValue(key, obj[key], indentation + ' ', theme);
}

@@ -69,3 +97,3 @@

let printLocationNumber = val => {
return typeof val === 'number' ? val : '?';
return typeof val === 'number' ? String(val) : '';
};

@@ -75,7 +103,6 @@

if (location !== null && typeof location === 'object') {
return (
printLocationNumber(location.line) +
':' +
printLocationNumber(location.column)
);
let line = printLocationNumber(location.line);
let column = printLocationNumber(location.column);
let splitter = line && column ? ':' : '';
return line + splitter + column;
} else {

@@ -86,5 +113,16 @@ return '';

let printLoc = loc => {
let printLoc = (loc, theme) => {
if (loc !== null && typeof loc === 'object') {
return '(' + printLocation(loc.start) + ', ' + printLocation(loc.end) + ')';
let start = printLocation(loc.start);
let end = printLocation(loc.end);
let splitter = end && start ? ', ' : '';
return (
theme.location.open +
'(' +
start +
splitter +
end +
')' +
theme.location.close
);
} else {

@@ -102,4 +140,9 @@ return '';

let printNode = (node /*: Node */, indentation) => {
let res = node.type + ' ' + printLoc(node.loc);
let printNode = (node /*: Node */, indentation, theme) => {
let res =
theme.node.open +
node.type +
theme.node.close +
' ' +
printLoc(node.loc, theme);
let keys = Object.keys(node).sort();

@@ -110,3 +153,3 @@

if (DROP_KEYS[key]) continue;
res += printKeyValue(key, node[key], indentation + ' ');
res += printKeyValue(key, node[key], indentation + ' ', theme);
}

@@ -117,13 +160,31 @@

let printValue = (val, indentation) => {
if (val === null) return 'null';
let printValue = (val, indentation, theme /*: Theme */) => {
if (val === null) {
return theme.null.open + 'null' + theme.null.close;
}
if (typeof val === 'boolean') return String(val);
if (typeof val === 'string') return `"${val}"`;
if (typeof val === 'number') return String(val);
if (typeof val === 'undefined') return 'undefined';
if (typeof val === 'boolean') {
return theme.boolean.open + String(val) + theme.boolean.close;
}
if (Array.isArray(val)) return printArray(val, indentation + ' ');
if (typeof val === 'object') return printObject(val, indentation);
if (typeof val === 'string') {
return theme.string.open + `"${val}"` + theme.string.close;
}
if (typeof val === 'number') {
return theme.number.open + String(val) + theme.number.close;
}
if (typeof val === 'undefined') {
return theme.undefined.open + 'undefined' + theme.undefined.open;
}
if (Array.isArray(val)) {
return printArray(val, indentation + ' ', theme);
}
if (typeof val === 'object') {
return printObject(val, indentation, theme);
}
console.error(val);

@@ -133,6 +194,20 @@ throw new Error('Unknown value type found in AST');

let printAST = (val /*: mixed */) => {
return printValue(val, '');
let getThemeItem = (colors, ansiStyle) => {
return colors ? ansiStyles[ansiStyle] : {open: '', close: ''};
};
let printAST = (val /*: mixed */, theme /*: boolean */ = false) => {
return printValue(val, '', {
location: getThemeItem(theme, 'dim'),
brackets: getThemeItem(theme, 'white'),
boolean: getThemeItem(theme, 'cyan'),
string: getThemeItem(theme, 'green'),
number: getThemeItem(theme, 'yellow'),
null: getThemeItem(theme, 'cyan'),
undefined: getThemeItem(theme, 'cyan'),
node: getThemeItem(theme, 'bold'),
key: getThemeItem(theme, 'white'),
});
};
module.exports = printAST;
{
"name": "ast-pretty-print",
"version": "1.0.0",
"version": "1.1.0",
"description": "A pretty printer for AST-like structures",

@@ -10,3 +10,6 @@ "main": "index.js",

"flow-bin": "^0.46.0"
},
"dependencies": {
"ansi-styles": "^3.0.0"
}
}

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