Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge bugfix/remove-node-type-def-1 into master #2

Merged
merged 11 commits into from
Oct 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,23 @@ end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
printWidth = 80

[*.{ts,tsx,js,jsx,json}]
indent_size = 4
[*.{ts,tsx,js,jsx,json,json5}]
curly_bracket_next_line = true
spaces_around_operators = true
spaces_around_brackets = outside
indent_brace_style = K&R
max_line_length = 120

[{package.json,.travis.yml}]
indent_size = 2
[*.{ts,js}]
max_line_length = 80

# XML Project Files
[*.{csproj,vbproj,vcxproj,vcxproj.filters,proj,projitems,shproj}]
indent_size = 2
[*.{tsx,jsx}]
max_line_length = 120

[*.{json,json5}]
max_line_length = 120

# Markdown Files
[*.md]
trim_trailing_whitespace = false

# Web Files
[*.{htm,html,css,scss,less}]
indent_size = 2
insert_final_newline = true

# Bash Files
[*.sh]
end_of_line = lf
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ tests
.editorconfig
.gitignore
.npmignore
.prettierrc.toml
package-lock.json
tslint.json
31 changes: 31 additions & 0 deletions .prettierrc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
arrowParens = "always"
bracketSpacing = true
insertPragma = false
jsxBracketSameLine = false
printWidth = 80
proseWrap = "always"
requirePragma = false
semi = true
singleQuote = false
tabWidth = 2
trailingComma = "all"

[[overrides]]
files = ["*.ts", "*.js"]
[overrides.options]
printWidth = 80

[[overrides]]
files = ["*.tsx", ".jsx"]
[overrides.options]
printWidth = 120

[[overrides]]
files = ["*.json", "*.json5"]
[overrides.options]
printWidth = 120

[[overrides]]
files = "*.md"
[overrides.options]
# proseWrap = "never"
269 changes: 181 additions & 88 deletions README.md

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions lib/cjs.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/// <reference types="node" />
export declare class ExtendableError extends Error {
public name: string;
public message: string;
public stack?: string;
public name: string;
public message: string;
public stack?: string;
}
export default ExtendableError;
151 changes: 85 additions & 66 deletions lib/cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,95 +6,114 @@ var helpers = require("./helpers");
var setPrototypeOf = helpers.setPrototypeOf;
var getPrototypeOf = helpers.getPrototypeOf;
var defineProperty = helpers.defineProperty;
var hasOwnProperty = helpers.hasOwnProperty;
var objectCreate = helpers.objectCreate;

// Small test for IE6-8, which checks if the environment prints errors "nicely"
// If not, a toString() method to be added to the error objects with formatting like in more modern browsers
var uglyErrorPrinting = (new Error()).toString() === "[object Error]";
// If not, a toString() method to be added to the error objects with formatting
// like in more modern browsers
var uglyErrorPrinting = new Error().toString() === "[object Error]";

// For compatibility
var extendableErrorName = "";

function ExtendableError(message) {
// Get the constructor
var originalConstructor = this.constructor;
// Get the constructor name from the non-standard name property. If undefined (on old IEs), it uses the string representation
// of the function to extract the name. This should work in all cases, except for directly instantiated ExtendableError objects,
// for which the name of the ExtendableError class / function is used
var constructorName = originalConstructor.name || (function() {
var constructorNameMatch = originalConstructor.toString().match(/^function\s*([^\s(]+)/);
return constructorNameMatch === null ? (extendableErrorName ? extendableErrorName : "Error") : constructorNameMatch[1];
// Get the constructor
var originalConstructor = this.constructor;
// Get the constructor name from the non-standard name property. If undefined
// (on old IEs), it uses the string representation of the function to extract
// the name. This should work in all cases, except for directly instantiated
// ExtendableError objects, for which the name of the ExtendableError class /
// function is used
var constructorName =
originalConstructor.name ||
(function() {
var constructorNameMatch = originalConstructor
.toString()
.match(/^function\s*([^\s(]+)/);
return constructorNameMatch === null
? extendableErrorName
? extendableErrorName
: "Error"
: constructorNameMatch[1];
})();
// If the constructor name is "Error", ...
var constructorNameIsError = constructorName === "Error";
// change it to the name of the ExtendableError class / function
var name = constructorNameIsError ? extendableErrorName : constructorName;
// If the constructor name is "Error", ...
var constructorNameIsError = constructorName === "Error";
// change it to the name of the ExtendableError class / function
var name = constructorNameIsError ? extendableErrorName : constructorName;

// Obtain a new Error instance. This also sets the message property already.
var instance = Error.apply(this, arguments);
// Obtain a new Error instance. This also sets the message property already.
var instance = Error.apply(this, arguments);

// Set the prototype of this to the prototype of instance
setPrototypeOf(instance, getPrototypeOf(this));
// Set the prototype of this to the prototype of instance
setPrototypeOf(instance, getPrototypeOf(this));

// On old IEs, the instance will not extend our subclasses this way. The fix is to use this from the function call instead.
if (!(instance instanceof originalConstructor) || !(instance instanceof ExtendableError)) {
var instance = this;
Error.apply(this, arguments);
defineProperty(instance, "message", {
configurable: true,
enumerable: false,
value: message,
writable: true
});
}

// define the name property
defineProperty(instance, "name", {
configurable: true,
enumerable: false,
value: name,
writable: true
// On old IEs, the instance will not extend our subclasses this way. The fix is to use this from the function call instead.
if (
!(instance instanceof originalConstructor) ||
!(instance instanceof ExtendableError)
) {
var instance = this;
Error.apply(this, arguments);
defineProperty(instance, "message", {
configurable: true,
enumerable: false,
value: message,
writable: true,
});
}

// define the name property
defineProperty(instance, "name", {
configurable: true,
enumerable: false,
value: name,
writable: true,
});

// Use Error.captureStackTrace on V8 to capture the proper stack trace excluding any of our error classes
if (Error.captureStackTrace) {
Error.captureStackTrace(instance, constructorNameIsError ? ExtendableError : originalConstructor);
}
// instance.stack can still be undefined, in which case the best solution is to create a new Error object and get it from there
if (instance.stack === undefined) {
var err = new Error(message);
err.name = instance.name;
instance.stack = err.stack;
}
// Use Error.captureStackTrace on V8 to capture the proper stack trace excluding any of our error classes
if (Error.captureStackTrace) {
Error.captureStackTrace(
instance,
constructorNameIsError ? ExtendableError : originalConstructor,
);
}
// instance.stack can still be undefined, in which case the best solution is to create a new Error object and get it from there
if (instance.stack === undefined) {
var err = new Error(message);
err.name = instance.name;
instance.stack = err.stack;
}

// If the environment does not have a proper string representation (IE), provide an alternative toString()
if (uglyErrorPrinting) {
defineProperty(instance, "toString", {
configurable: true,
enumerable: false,
value: function toString() {
return (this.name || "Error") + (typeof this.message === "undefined" ? "" : ": " + this.message);
},
writable: true
});
}
// If the environment does not have a proper string representation (IE), provide an alternative toString()
if (uglyErrorPrinting) {
defineProperty(instance, "toString", {
configurable: true,
enumerable: false,
value: function toString() {
return (
(this.name || "Error") +
(typeof this.message === "undefined" ? "" : ": " + this.message)
);
},
writable: true,
});
}

// We're done!
return instance;
// We're done!
return instance;
}

// Get the name of the ExtendableError function or use the string literal
extendableErrorName = ExtendableError.name || "ExtendableError";

// Set the prototype of ExtendableError to an Error object
ExtendableError.prototype = objectCreate(Error.prototype, {
constructor: {
value: Error,
enumerable: false,
writable: true,
configurable: true
}
constructor: {
value: Error,
enumerable: false,
writable: true,
configurable: true,
},
});

// Export
Expand Down
7 changes: 3 additions & 4 deletions lib/es.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/// <reference types="node" />
export declare class ExtendableError extends Error {
public name: string;
public message: string;
public stack?: string;
public name: string;
public message: string;
public stack?: string;
}
export default ExtendableError;
61 changes: 31 additions & 30 deletions lib/es.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,42 @@
"use strict";

class ExtendableError extends Error {
constructor(...params) {
super(...params);
var message = params.length > 0 && typeof params[0] === "string" ? params[0] : "";
constructor(...params) {
super(...params);
var message =
params.length > 0 && typeof params[0] === "string" ? params[0] : "";

// Replace Error with ClassName of the constructor, if it has not been overwritten already
if ((this.name === undefined) || (this.name === "Error")) {
Object.defineProperty(this, "name", {
configurable: true,
enumerable: false,
value: this.constructor.name,
writable: true
});
}
// Replace Error with ClassName of the constructor, if it has not been overwritten already
if (this.name === undefined || this.name === "Error") {
Object.defineProperty(this, "name", {
configurable: true,
enumerable: false,
value: this.constructor.name,
writable: true,
});
}

Object.defineProperty(this, "message", {
configurable: true,
enumerable: false,
value: message,
writable: true
});
Object.defineProperty(this, "message", {
configurable: true,
enumerable: false,
value: message,
writable: true,
});

Object.defineProperty(this, "stack", {
configurable: true,
enumerable: false,
value: "",
writable: true
});
Object.defineProperty(this, "stack", {
configurable: true,
enumerable: false,
value: "",
writable: true,
});

// Maintains proper stack trace for where our error was thrown (only available on V8)
if (Error.captureStackTrace) {
Error.captureStackTrace(this, this.constructor);
} else if (this.stack === "") {
this.stack = (new Error(message)).stack;
}
// Maintains proper stack trace for where our error was thrown (only available on V8)
if (Error.captureStackTrace) {
Error.captureStackTrace(this, this.constructor);
} else if (this.stack === "") {
this.stack = new Error(message).stack;
}
}
}

exports.ExtendableError = ExtendableError;
Expand Down
Loading