Coder Social home page Coder Social logo

amilajack / babel-plugin-fail-explicit Goto Github PK

View Code? Open in Web Editor NEW
7.0 3.0 3.0 872 KB

A babel plugin that prevents coercion and silent failure in JavaScript

License: MIT License

JavaScript 100.00%
babel plugin fail explicit coercion safe silent failure

babel-plugin-fail-explicit's Introduction

babel-plugin-fail-explicit

Build Status NPM version Dependency Status npm

A babel plugin that prevents coercion and silent failure in JavaScript

demo

Roadmap

  • Fail on unsafe coercion
  • Fail on unsafe property access
  • Do not fail inside conditional expressions or default statements (||), on by default
  • Allow unsafe access in if statement by default
  • Allow for configuration of strictness

Installation

npm install --save-dev babel-plugin-fail-explicit

Setup

// .babelrc
{
  "plugins": [
    "fail-explicit"
  ]
}

Demo:

To experiment with babel-plugin-fail-explicit, see this demo repo

Examples

// ------------------------------------------------
// Coercion safeguard
// ------------------------------------------------
[] + {}
// TypeError: 'Unexpected coercion of type "Array" and
// type "Object" using "+" operator'

NaN + undefined
// TypeError: Unexpected coercion of type "NaN" and type
// "undefined" using "+" operator

1 + 'some'
// '1some'


// ------------------------------------------------
// Safe Comparison
// ------------------------------------------------
new String('12') > 12
// TypeError: Unexpected comparison of type "String" and type
// "number" using ">" operator

null > undefined
// TypeError: Unexpected comparison of type "null" and type
// "undefined" using ">" operator


// ------------------------------------------------
// Usage for better undefined propagation errors
// ------------------------------------------------
const obj = {
  foo: {
    bar: {}
  }
}

obj.foo.bar.baz;
// TypeError: Property "baz" does not exist in "Object.foo.bar"


// ------------------------------------------------
// Usage as out of bounds check
// ------------------------------------------------
const some = new Array(3)
some[10]
// TypeError: '"Array[10]" is out of bounds'

const bar = []
some[100]
// TypeError: '"Array[100]" is out of bounds'

// TypeError: '"woo[1]" is out of bounds'
const obj = {
  woo: ['']
}

obj.woo[1]
// TypeError: '"woo[1]" is out of bounds'

babel-plugin-fail-explicit's People

Contributors

amilajack avatar andywer avatar greenkeeper[bot] avatar renovate-bot avatar renovate[bot] avatar vikr01 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

andywer vikr01

babel-plugin-fail-explicit's Issues

Dependency deprecation warning: babel-preset-es2015 (npm)

On registry https://registry.npmjs.org/, the "latest" version (v6.24.1) of dependency babel-preset-es2015 has the following deprecation notice:

๐Ÿ™Œ Thanks for using Babel: we recommend using babel-preset-env now: please read babeljs.io/env to update!

Marking the latest version of an npm package as deprecated results in the entire package being considered deprecated, so contact the package author you think this is a mistake.

Affected package file(s): package.json

If you don't care about this, you can close this issue and not be warned about babel-preset-es2015's deprecation again. If you would like to completely disable all future deprecation warnings then add the following to your config:

"suppressNotifications": ["deprecationWarningIssues"]

Add support for es6 destructuring

Example

const some = {}
const { foo } = some
// ^^^ Fails silently

Transformation

const some = {}
const foo = safePropertyAccess(['foo'], some)
// TypeError: Property "foo" does not exist in "Object"

safeCoerce: Blacklisting vs Whitelisting

Not 100% sure what the code's intention is, but I think you want to apply it only to operations like +, -, *, ..., but not apply it to comparisons.

I would definitely rather whitelist the supported operators than blacklisting the unsupported ones, since there is a real chance you miss something or tomorrow someone adds a new operator that breaks stuff.

Consider '<', for instance. It's also a comparison, but maybe I missed your intention here ๐Ÿ˜‰

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Location: package.json
Error type: The renovate configuration file contains some invalid settings
Message: Configuration option 'packageRules[2].node' should be a json object, Invalid configuration option: author, Invalid configuration option: babel, Invalid configuration option: bugs, Invalid configuration option: engineStrict, Invalid configuration option: eslintConfig, Invalid configuration option: files, Invalid configuration option: homepage, Invalid configuration option: jest, Invalid configuration option: keywords, Invalid configuration option: license, Invalid configuration option: main, Invalid configuration option: name, Invalid configuration option: packageRules[0].@babel/helper-module-imports, Invalid configuration option: packageRules[0].safe-access-check, Invalid configuration option: packageRules[1].@babel/cli, Invalid configuration option: packageRules[1].@babel/core, Invalid configuration option: packageRules[1].@babel/plugin-transform-modules-commonjs, Invalid configuration option: packageRules[1].@babel/plugin-transform-modules-umd, Invalid configuration option: packageRules[1].@babel/plugin-transform-runtime, Invalid configuration option: packageRules[1].@babel/preset-env, Invalid configuration option: packageRules[1].@babel/preset-flow, Invalid configuration option: packageRules[1].@babel/traverse, Invalid configuration option: packageRules[1].@babel/types, Invalid configuration option: packageRules[1].babel-eslint, Invalid configuration option: packageRules[1].babel-plugin-add-module-exports, Invalid configuration option: packageRules[1].babel-plugin-transform-async-to-bluebird, Invalid configuration option: packageRules[1].babel-plugin-transform-promise-to-bluebird, Invalid configuration option: packageRules[1].cross-env, Invalid configuration option: packageRules[1].eslint, Invalid configuration option: packageRules[1].eslint-config-bliss, Invalid configuration option: packageRules[1].flow-bin, Invalid configuration option: packageRules[1].flow-typed, Invalid configuration option: packageRules[1].jest, Invalid configuration option: renovate, Invalid configuration option: scripts, Invalid configuration option: version, The "node" object can only be configured at the top level of a config but was found inside "packageRules[2]"

Prefix methods

safeCoerce -> _safeCoerce. Helps avoid possible naming collisions

Dependency deprecation warning: babel-preset-es2015 (npm)

On registry https://registry.npmjs.org/, the "latest" version (v6.24.1) of dependency babel-preset-es2015 has the following deprecation notice:

๐Ÿ™Œ Thanks for using Babel: we recommend using babel-preset-env now: please read babeljs.io/env to update!

Marking the latest version of an npm package as deprecated results in the entire package being considered deprecated, so contact the package author you think this is a mistake.

Affected package file(s): package.json

If you don't care about this, you can close this issue and not be warned about babel-preset-es2015's deprecation again. If you would like to completely disable all future deprecation warnings then add the following to your config:

"suppressNotifications": ["deprecationWarningIssues"]

Dependency Dashboard

This issue provides visibility into Renovate updates and their statuses. Learn more

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.


  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.