Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ast-whitelist

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ast-whitelist

Check an ECMAScript AST node against a whitelist or predicate

  • 0.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

ast-whitelist

Check an ECMAScript AST node against a whitelist or predicate

npm install ast-whitelist

check

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'

typing

const check = (
  node: Node,
  whitelist: string[],
  stopOnFirst?: boolean
) => Node[]

test

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
}

typing

const test = (
  node: Node,
  predicate: NodePredicate,
  stopOnFirst?: boolean
) => Node[]

type NodePredicate = (
  node: Node,
  parent: Node | null,
  root: Node
) => boolean

testWithReason

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 }` )
}

typing

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>

license

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

Package last updated on 11 Apr 2019

Did you know?

Socket

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.

Install

Related posts

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