Skip to content
Open
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
5 changes: 4 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ module.exports = {
parser: 'babel-eslint',
parserOptions: {
ecmaVersion: 2017,
sourceType: 'module'
sourceType: 'module',
ecmaFeatures: {
legacyDecorators: true
}
},
plugins: ['ember', 'prettier'],
extends: [
Expand Down
62 changes: 29 additions & 33 deletions addon/-private/extensions/prevent-additional-arguments.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Component from '@ember/component';
import { assert } from '@ember/debug';
import collapseProto from '@ember-decorators/utils/collapse-proto';
import { afterMethod } from 'patch-method';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great! Really helpful utility here


import { argumentWhitelist } from '../config';
import { getValidationsFor } from '../validations-for';
Expand Down Expand Up @@ -30,44 +32,38 @@ export function hasExtension(klass) {
export function withExtension(klass) {
HAS_EXTENSION.add(klass);

return class extends klass {
static get name() {
return klass.name;
}

init(...args) {
super.init(...args);
collapseProto(klass.prototype);

const validations = getValidationsFor(this.constructor) || {};
afterMethod(klass, 'init', function() {
const validations = getValidationsFor(this.constructor) || {};

if (Object.keys(validations).length === 0) {
return;
}
if (Object.keys(validations).length === 0) {
return;
}

const attributes = this.attributeBindings || [];
const classNames = (this.classNameBindings || []).map(
binding => binding.split(':')[0]
);
const attributes = this.attributeBindings || [];
const classNames = (this.classNameBindings || []).map(
binding => binding.split(':')[0]
);

const expectedArguments = [
...attributes,
...classNames,
...Object.keys(validations),
...whitelist
];
const expectedArguments = [
...attributes,
...classNames,
...Object.keys(validations),
...whitelist
];

for (let key in this.attrs) {
const isValidArgOrAttr = expectedArguments.some(matcher =>
key.match(matcher)
);
for (let key in this.attrs) {
const isValidArgOrAttr = expectedArguments.some(matcher =>
key.match(matcher)
);

assert(
`Attempted to assign the argument '${key}' on an instance of ${
this.constructor.name
}, but no argument was defined for that key. Use the @argument helper on the class field to define an argument for that class.`,
isValidArgOrAttr
);
}
assert(
`Attempted to assign the argument '${key}' on an instance of ${
this.constructor.name
}, but no argument was defined for that key. Use the @argument helper on the class field to define an argument for that class.`,
isValidArgOrAttr
);
}
};
});
}
21 changes: 9 additions & 12 deletions addon/-private/extensions/with-validation.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import collapseProto from '@ember-decorators/utils/collapse-proto';
import { afterMethod } from 'patch-method';

import { wrapField } from '../wrap-field';
import { getValidationsFor } from '../validations-for';

Expand All @@ -13,19 +16,13 @@ export function hasExtension(klass) {
export function withExtension(klass) {
HAS_VALIDATION.add(klass);

return class extends klass {
static get name() {
return klass.name;
}

init(...args) {
super.init(...args);
collapseProto(klass.prototype);

const validations = getValidationsFor(this.constructor);
afterMethod(klass, 'init', function() {
const validations = getValidationsFor(this.constructor);

for (let key in validations) {
wrapField(this.constructor, this, validations, key);
}
for (let key in validations) {
wrapField(this.constructor, this, validations, key);
}
};
});
}
4 changes: 1 addition & 3 deletions addon/-private/wrap-field.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Ember from 'ember';
import { notifyPropertyChange } from '@ember/object';

import {
isMandatorySetter,
Expand All @@ -7,9 +8,6 @@ import {
} from './utils/computed';
import { getPropertyDescriptor } from './utils/object';

const notifyPropertyChange =
Ember.notifyPropertyChange || Ember.propertyDidChange;

function guardBind(fn, ...args) {
if (typeof fn === 'function') {
return fn.bind(...args);
Expand Down
26 changes: 11 additions & 15 deletions addon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function argument(typeDefinition) {
);
assert(
'`@argument` must be passed a type to validate against',
typeDefinition.toString() !== '[object Descriptor]'
!(typeDefinition instanceof Object && typeof arguments[1] === 'string')
);
assert(
'`@argument` must only be passed one type definition',
Expand All @@ -28,22 +28,18 @@ export function argument(typeDefinition) {

const validator = resolveValidator(typeDefinition);

return desc => {
return {
...desc,
finisher(klass) {
addValidationFor(klass, desc.key, validator);
return (target, key, desc) => {
let klass = target.constructor;
addValidationFor(klass, key, validator);

if (!hasValidationExtension(klass)) {
klass = withValidationExtension(klass);
}
if (!hasValidationExtension(klass)) {
withValidationExtension(klass);
}

if (!hasComponentExtension(klass) && needsComponentExtension(klass)) {
klass = withComponentExtension(klass);
}
if (!hasComponentExtension(klass) && needsComponentExtension(klass)) {
withComponentExtension(klass);
}

return klass;
}
};
return desc;
};
}
52 changes: 27 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,43 +26,45 @@
},
"dependencies": {
"babel-plugin-filter-imports": "^2.0.4",
"broccoli-funnel": "^2.0.1",
"debug": "^4.1.0",
"ember-cli-babel": "^7.1.4",
"broccoli-funnel": "^2.0.2",
"debug": "^4.1.1",
"ember-auto-import": "^1.3.0",
"ember-cli-babel": "^7.7.3",
"ember-cli-version-checker": "^2.0.0",
"ember-get-config": "^0.2.4"
"ember-get-config": "^0.2.4",
"patch-method": "^0.3.1"
},
"devDependencies": {
"@ember-decorators/babel-transforms": "^3.1.0",
"@ember/optional-features": "^0.6.3",
"@ember/optional-features": "^0.7.0",
"ava": "^0.25.0",
"babel-eslint": "^8.0.0",
"ember-cli": "~3.5.1",
"ember-cli-dependency-checker": "^3.0.0",
"babel-eslint": "^10.0.1",
"ember-cli": "~3.9.0",
"ember-cli-dependency-checker": "^3.1.0",
"ember-cli-htmlbars": "^3.0.0",
"ember-cli-htmlbars-inline-precompile": "^2.0.0",
"ember-cli-inject-live-reload": "^1.8.2",
"ember-cli-htmlbars-inline-precompile": "^2.1.0",
"ember-cli-inject-live-reload": "^2.0.1",
"ember-cli-sri": "^2.1.1",
"ember-cli-template-lint": "^1.0.0-beta.1",
"ember-decorators": "^3.1.5",
"ember-cli-template-lint": "^1.0.0-beta.3",
"ember-decorators": "^6.0.0",
"ember-decorators-polyfill": "^1.0.4",
"ember-disable-prototype-extensions": "^1.1.3",
"ember-export-application-global": "^2.0.0",
"ember-load-initializers": "^1.1.0",
"ember-load-initializers": "^2.0.0",
"ember-maybe-import-regenerator": "^0.1.6",
"ember-native-dom-helpers": "^0.5.4",
"ember-qunit": "^3.4.1",
"ember-resolver": "^5.0.1",
"ember-source": "~3.6.0",
"ember-native-dom-helpers": "^0.6.2",
"ember-qunit": "^4.4.1",
"ember-resolver": "^5.1.3",
"ember-source": "~3.9.1",
"ember-source-channel-url": "^1.1.0",
"ember-try": "^1.0.0",
"eslint": "^5.10.0",
"eslint-config-prettier": "^3.3.0",
"eslint-plugin-ember": "^5.2.0",
"eslint-plugin-node": "^7.0.1",
"eslint-plugin-prettier": "^3.0.0",
"eslint": "^5.16.0",
"eslint-config-prettier": "^4.2.0",
"eslint-plugin-ember": "^6.4.1",
"eslint-plugin-node": "^8.0.1",
"eslint-plugin-prettier": "^3.0.1",
"loader.js": "^4.7.0",
"prettier": "^1.15.3",
"qunit-dom": "^0.8.0"
"prettier": "^1.17.0",
"qunit-dom": "^0.8.4"
},
"engines": {
"node": "6.* || 8.* || >= 10.*"
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/argument-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { module, test, skip } from 'qunit';
import EmberObject from '@ember/object';
import { addObserver } from '@ember/object/observers';
import { argument } from '@ember-decorators/argument';
import { computed } from '@ember-decorators/object';
import { alias } from '@ember-decorators/object/computed';
import { computed } from '@ember/object';
import { alias } from '@ember/object/computed';

module('Unit | @argument', function() {
test('there are no affects on objects without validation', function(assert) {
Expand Down
Loading