Giter Site home page Giter Site logo

babel-bug's Introduction

This repo describes Babel.js bug for distinguishing between Arrow Functions, Classes and Regular Functions.

Installation

git clone
npm i

And after installation, for dealing with bug:

npm run bug

for dealing with original JavaScript behaviour:

npm run original

Description

Accordingly with the following gis: https://gist.github.com/wentout/ea3afe9c822a6b6ef32f9e4f3e98b1ba

We might have difference between Functions, Regular Functions or Arrow Functions:

'use strict';

const myArrow = () => {};
const myFn = function () {};

class MyClass {};

And if we might be willing to check what sort of function we are dealing with, we have the ability to check the difference with the following steps:

const isArrowFunction = (fn) => {
	if (typeof fn !== 'function') {
		return false;
	}
	if (fn.prototype !== undefined) {
		return false;
	}
	return true;
};

const isRegularFunction = (fn) => {
	if (typeof fn !== 'function') {
		return false;
	}
	if (typeof fn.prototype !== 'object') {
		return false;
	}
	if (fn.prototype.constructor !== fn) {
		return false;
	}
	return Object.getOwnPropertyDescriptor(fn, 'prototype').writable === true;
};

const isClass = (fn) => {
	if (typeof fn !== 'function') {
		return false;
	}
	if (typeof fn.prototype !== 'object') {
		return false;
	}
	if (fn.prototype.constructor !== fn) {
		return false;
	}
	return Object.getOwnPropertyDescriptor(fn, 'prototype').writable === false;
};

And that is how babel transforms functions:

var myArrow = function myArrow() {};

var myFn = function myFn() {};

var MyClass = function MyClass() {
  _classCallCheck(this, MyClass);
};

Therefore regular checking via JavaScript runtime wouldn't work anymore, because everything becomes functions.

Screenshots

Before babel transforms

After

How to implement correct behaviour?

for Arrow Functions

const workingArrow = () => {};
// right after arrow function definition
Object.defineProperty(workingArrow, 'prototype', {
    value: undefined
});

for Classes

class WorkingClass {}
// right after class definition
Object.defineProperty(WorkingClass, 'prototype', {
    writable: false
});

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.