Skip to content

Commit

Permalink
Merge pull request #30 from wentout/proto
Browse files Browse the repository at this point in the history
decorators explanation
  • Loading branch information
wentout committed Oct 17, 2023
2 parents 637399b + 7f90acc commit 9444d03
Show file tree
Hide file tree
Showing 12 changed files with 1,306 additions and 4,986 deletions.
34 changes: 15 additions & 19 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
module.exports = {
parser: '@typescript-eslint/parser',
env: {
node: true,
es6: true,
},
extends: ['eslint:recommended'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/eslint-recommended'
],
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module'
},
rules: {
'indent': ['error', 'tab'],
'key-spacing': [
'warn',
{
beforeColon: true,
afterColon: true,
align: 'colon',
},
],
indent: ['error', 'tab'],
'linebreak-style': ['error', 'unix'],
quotes: ['error', 'single'],
semi: ['error', 'always'],
'no-unused-vars': 'warn',
// 'no-unused-vars': 'warn',
'no-shadow': [
'error',
{
Expand All @@ -29,13 +26,13 @@ module.exports = {
allow: [],
},
],
'space-before-function-paren': [
'warn', {
'anonymous': 'always',
'named': 'always',
'asyncArrow': 'always'
}
],
// 'space-before-function-paren': [
// 'warn', {
// 'anonymous': 'always',
// 'named': 'always',
// 'asyncArrow': 'always'
// }
// ],
'prefer-template': 'warn',
'prefer-spread': 'warn',
'no-useless-concat': 'warn',
Expand All @@ -60,7 +57,6 @@ module.exports = {
position: 'above',
},
],
quotes: ['error', 'single'],
yoda: 'warn',
},
'overrides': [
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:

strategy:
matrix:
node-version: [14.x, 16.x, 18.x]
node-version: [16.x, 18.x]

steps:
- uses: actions/checkout@v2
Expand All @@ -34,10 +34,10 @@ jobs:

steps:
- uses: actions/checkout@master
- name: Use Node.js 14.x
- name: Use Node.js 16.x
uses: actions/setup-node@master
with:
node-version: 14.x
node-version: 16.x

- name: npm install
run: npm install
Expand Down
2 changes: 1 addition & 1 deletion lib/fields.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ interface FieldDefinition {
}
export declare class FieldConstructor implements FieldDefinition {
[SymbolInitialValue]: unknown;
get get(): (this: FieldDefinition) => unknown;
get get(): () => unknown;
get set(): () => never;
constructor(value: unknown);
static get SymbolInitialValue(): symbol;
Expand Down
15 changes: 7 additions & 8 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
declare const BaseConstructor: ObjectConstructor;
export declare class BaseClass extends BaseConstructor {
type Proto<P, T> = Pick<P, Exclude<keyof P, keyof T>> & T;
export declare const BaseConstructorPrototype: <P extends object, S extends Proto<T, P>, T extends {
(): P;
new (): { [key in keyof S]: S[key]; };
}>(this: T, InstanceTarget?: P) => T;
export declare class BaseClass extends BaseConstructorPrototype {
}
export type IDEF<T, P = {}, R = {}> = {
new (...args: unknown[]): T;
(this: T, ...args: unknown[]): R;
prototype: P;
};
export {};
export { FieldConstructor } from './fields';
22 changes: 13 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseClass = void 0;
exports.FieldConstructor = exports.BaseClass = exports.BaseConstructorPrototype = void 0;
const errors_1 = require("./errors");
const types_1 = require("./types");
const fields_1 = require("./fields");
Expand Down Expand Up @@ -59,7 +59,8 @@ const handlers = {
}
if (prop === 'toJSON') {
return function () {
return JSON.stringify(Object.entries(this).reduce((obj, [key, value]) => {
const entries = Object.entries(this);
return JSON.stringify(entries.reduce((obj, [key, value]) => {
obj[key] = value.valueOf();
return obj;
}, {}));
Expand All @@ -76,11 +77,11 @@ const handlers = {
},
};
const BaseTarget = Object.create(null);
const BaseConstructor = function (InstanceTarget = BaseTarget) {
const BaseConstructorPrototype = function (InstanceTarget = BaseTarget) {
if (!new.target) {
const self = BaseConstructor.bind(this, InstanceTarget);
const self = exports.BaseConstructorPrototype.bind(this, InstanceTarget);
self.prototype = {
constructor: BaseConstructor
constructor: exports.BaseConstructorPrototype
};
return self;
}
Expand All @@ -90,19 +91,22 @@ const BaseConstructor = function (InstanceTarget = BaseTarget) {
do {
protoPointer = Reflect.getPrototypeOf(protoPointer);
protoConstrcutor = Reflect.getOwnPropertyDescriptor(protoPointer, 'constructor').value;
} while (protoConstrcutor !== BaseConstructor);
} while (protoConstrcutor !== exports.BaseConstructorPrototype);
Reflect.setPrototypeOf(protoPointer, InstancePrototype);
return this;
};
exports.BaseConstructorPrototype = BaseConstructorPrototype;
Object.defineProperty(module, 'exports', {
get() {
return BaseConstructor;
return exports.BaseConstructorPrototype;
},
enumerable: true
});
class BaseClass extends BaseConstructor {
class BaseClass extends exports.BaseConstructorPrototype {
}
exports.BaseClass = BaseClass;
;
var fields_2 = require("./fields");
Object.defineProperty(exports, "FieldConstructor", { enumerable: true, get: function () { return fields_2.FieldConstructor; } });
Object.defineProperty(module.exports, 'BaseClass', {
get() {
return BaseClass;
Expand Down

0 comments on commit 9444d03

Please sign in to comment.