Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
ast-whitelist
Advanced tools
Check an ECMAScript AST node against a whitelist or predicate
npm install ast-whitelist
const { check } = require( 'ast-whitelist' )
// JSON node types
const whitelist = [
'ObjectExpression', 'ArrayExpression', 'Property', 'Identifier', 'Literal'
]
// length will be zero if no bad nodes
const unexpectedNodes = check( node, whitelist )
// or stop after the first bad node - first will be undefined if no bad nodes
const [ first ] = check( node, whitelist, true )
Or using import syntax:
import { check } from 'ast-whitelist'
const check = (
node: Node,
whitelist: string[],
stopOnFirst?: boolean
) => Node[]
const { test } = require( 'ast-whitelist' )
// ban member expressions
const notMemberExpression = node => node.type !== 'MemberExpression'
const unexpectedNodes = test( test, notMemberExpression )
// or stop after the first bad node
const [ first ] = test( node, notMemberExpression, true )
The predicate you pass also recieves a parent node (null
if the current node
is the root node) and the root node, in case your predicate needs some context:
const predicate = ( node, parent, root ) => {
if( node !== root && parent && parent.type === 'VariableDeclarator' )
return false
return true
}
const test = (
node: Node,
predicate: NodePredicate,
stopOnFirst?: boolean
) => Node[]
type NodePredicate = (
node: Node,
parent: Node | null,
root: Node
) => boolean
Same as test
, except instead of returning a boolean from your predicate you
return either:
{ valid: true }
Or:
{
valid: false,
reason: 'It was bad'
}
Instead of returning an array of unexpected nodes, it returns an array of:
{
node: { /*...*/ },
reason: '...'
}
reason
is a string by default but you can return anything:
const { testWithReason } = require( 'ast-whitelist' )
const whitelist = [
'ObjectExpression', 'ArrayExpression', 'Property', 'Identifier', 'Literal'
]
const predicate = node => {
if( whitelist.includes( node.type ) ) return { valid: true }
return {
valid: false,
reason: {
errorType: 'Unexpected type',
nodeType: node.type
}
}
}
const [ first ] = testWithReason( node, predicate, true )
if( first ){
const { reason } = first
const { errorType, nodeType } = reason
throw Error( `${ errorType } ${ nodeType }` )
}
const testWithReason = <T = string>(
node: Node,
predicate: NodePredicateWithReason<T>,
stopOnFirst?: boolean
) => NodeWithReason<T>[]
interface NodeWithReason<T = string> {
node: Node
reason: T
}
type NodePredicateReasonResult<T = string> =
NodePredicateReasonValid | NodePredicateReasonInvalid<T>
type NodePredicateReasonValid = {
valid: true
}
type NodePredicateReasonInvalid<T = string> = {
valid: false
reason: T
}
type NodePredicateWithReason<T = string> =
( node: Node, parent: Node | null, root: Node ) =>
NodePredicateReasonResult<T>
MIT License
Copyright (c) 2019 Nik Coughlin
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
Check an ECMAScript AST node against a whitelist or predicate
The npm package ast-whitelist receives a total of 0 weekly downloads. As such, ast-whitelist popularity was classified as not popular.
We found that ast-whitelist demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.