Skip to content

Commit

Permalink
Bump prettier to v3 and fix lint (#673)
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeAstapov authored Oct 15, 2024
1 parent a885502 commit 71febde
Show file tree
Hide file tree
Showing 19 changed files with 1,787 additions and 470 deletions.
10 changes: 8 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ module.exports = {
sourceType: 'module',
requireConfigFile: false,
babelOptions: {
plugins: [['@babel/plugin-proposal-decorators', { decoratorsBeforeExport: true }]],
plugins: [
['@babel/plugin-proposal-decorators', { decoratorsBeforeExport: true }],
],
},
},
plugins: ['ember'],
extends: ['eslint:recommended', 'plugin:ember/recommended', 'plugin:prettier/recommended'],
extends: [
'eslint:recommended',
'plugin:ember/recommended',
'plugin:prettier/recommended',
],
env: {
browser: true,
},
Expand Down
10 changes: 1 addition & 9 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
# unconventional js
/blueprints/*/files/
/vendor/

# compiled output
/dist/
/tmp/

# dependencies
/bower_components/
/node_modules/

# misc
/coverage/
!.*
.eslintcache
.*/

# ember-try
/.node_modules.ember-try/
/bower.json.ember-try
/package.json.ember-try
10 changes: 8 additions & 2 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
'use strict';

module.exports = {
printWidth: 120,
singleQuote: true,
overrides: [
{
files: '*.{js,ts}',
options: {
singleQuote: true,
},
},
],
};
15 changes: 12 additions & 3 deletions addon/helpers/changeset.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { helper } from '@ember/component/helper';
import { Changeset } from 'ember-changeset';
import { lookupValidator, isChangeset, isPromise, isObject } from 'validated-changeset';
import {
lookupValidator,
isChangeset,
isPromise,
isObject,
} from 'validated-changeset';

export function changeset([obj, validations], options = {}) {
if (!obj) {
Expand All @@ -14,14 +19,18 @@ export function changeset([obj, validations], options = {}) {

if (isObject(validations)) {
if (isPromise(obj)) {
return obj.then((resolved) => Changeset(resolved, lookupValidator(validations), validations, options));
return obj.then((resolved) =>
Changeset(resolved, lookupValidator(validations), validations, options),
);
}

return Changeset(obj, lookupValidator(validations), validations, options);
}

if (isPromise(obj)) {
return Promise.resolve(obj).then((resolved) => Changeset(resolved, validations, {}, options));
return Promise.resolve(obj).then((resolved) =>
Changeset(resolved, validations, {}, options),
);
}

return Changeset(obj, validations, {}, options);
Expand Down
38 changes: 32 additions & 6 deletions addon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import mergeDeep from './utils/merge-deep';
import isObject from './utils/is-object';
import { tracked } from '@glimmer/tracking';
import { get as safeGet, set as safeSet } from '@ember/object';
import { macroCondition, dependencySatisfies, importSync } from '@embroider/macros';
import {
macroCondition,
dependencySatisfies,
importSync,
} from '@embroider/macros';

export { ValidatedChangeset };

Expand Down Expand Up @@ -107,7 +111,11 @@ export class EmberChangeset extends BufferedChangeset {
let content = this[CONTENT];
let changes = this[CHANGES];

let pendingChanges = this.mergeDeep(Object.create(Object.getPrototypeOf(content)), content, { safeGet, safeSet });
let pendingChanges = this.mergeDeep(
Object.create(Object.getPrototypeOf(content)),
content,
{ safeGet, safeSet },
);

return this.mergeDeep(pendingChanges, changes, { safeGet, safeSet });
}
Expand Down Expand Up @@ -204,9 +212,17 @@ export class EmberChangeset extends BufferedChangeset {
/**
* Creates new changesets.
*/
export function changeset(obj, validateFn = defaultValidatorFn, validationMap = {}, options = {}) {
export function changeset(
obj,
validateFn = defaultValidatorFn,
validationMap = {},
options = {},
) {
assert('Underlying object for changeset is missing', Boolean(obj));
assert('Array is not a valid type to pass as the first argument to `changeset`', !Array.isArray(obj));
assert(
'Array is not a valid type to pass as the first argument to `changeset`',
!Array.isArray(obj),
);

if (options.changeset) {
return new options.changeset(obj, validateFn, validationMap, options);
Expand All @@ -220,7 +236,12 @@ export function changeset(obj, validateFn = defaultValidatorFn, validationMap =
* Creates new changesets.
* @function Changeset
*/
export function Changeset(obj, validateFn = defaultValidatorFn, validationMap = {}, options = {}) {
export function Changeset(
obj,
validateFn = defaultValidatorFn,
validationMap = {},
options = {},
) {
const c = changeset(obj, validateFn, validationMap, options);

return new Proxy(c, {
Expand All @@ -244,7 +265,12 @@ export default class ChangesetKlass {
* @class ChangesetKlass
* @constructor
*/
constructor(obj, validateFn = defaultValidatorFn, validationMap = {}, options = {}) {
constructor(
obj,
validateFn = defaultValidatorFn,
validationMap = {},
options = {},
) {
const c = changeset(obj, validateFn, validationMap, options);

return new Proxy(c, {
Expand Down
7 changes: 6 additions & 1 deletion addon/utils/is-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@ import { isArray } from '@ember/array';
* @method isObject
*/
export default function isObject(val) {
return val !== null && typeof val === 'object' && !(val instanceof Date || val instanceof RegExp) && !isArray(val);
return (
val !== null &&
typeof val === 'object' &&
!(val instanceof Date || val instanceof RegExp) &&
!isArray(val)
);
}
27 changes: 22 additions & 5 deletions addon/utils/merge-deep.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ function propertyIsUnsafe(target, key, options) {
return (
propertyIsOnObject(target, key) && // Properties are safe to merge if they don't exist in the target yet,
!(
(Object.prototype.hasOwnProperty.call(target, key) && Object.prototype.propertyIsEnumerable.call(target, key)) // unsafe if they exist up the prototype chain,
(
Object.prototype.hasOwnProperty.call(target, key) &&
Object.prototype.propertyIsEnumerable.call(target, key)
) // unsafe if they exist up the prototype chain,
)
); // and also unsafe if they're nonenumerable.
}
Expand Down Expand Up @@ -112,8 +115,20 @@ function mergeTargetAndSource(target, source, options) {
}

// else safe key on object
if (propertyIsOnObject(target, key) && isMergeableObject(source[key]) && !isChange(source[key])) {
options.safeSet(target, key, mergeDeep(options.safeGet(target, key), options.safeGet(source, key), options));
if (
propertyIsOnObject(target, key) &&
isMergeableObject(source[key]) &&
!isChange(source[key])
) {
options.safeSet(
target,
key,
mergeDeep(
options.safeGet(target, key),
options.safeGet(source, key),
options,
),
);
} else {
let next = source[key];
if (isChange(next)) {
Expand Down Expand Up @@ -156,7 +171,9 @@ export default function mergeDeep(target, source, options = {}) {
let sourceIsArrayLike = isArrayObject(source);

if (targetIsArray && sourceIsArrayLike) {
return objectToArray(mergeTargetAndSource(arrayToObject(target), source, options));
return objectToArray(
mergeTargetAndSource(arrayToObject(target), source, options),
);
}

return source;
Expand All @@ -177,7 +194,7 @@ export default function mergeDeep(target, source, options = {}) {
} catch (e) {
// this is very unlikely to be hit but lets throw an error otherwise
throw new Error(
'Unable to `mergeDeep` with your data. Are you trying to merge two ember-data objects? Please file an issue with ember-changeset.'
'Unable to `mergeDeep` with your data. Are you trying to merge two ember-data objects? Please file an issue with ember-changeset.',
);
}
}
Expand Down
17 changes: 14 additions & 3 deletions addon/validated-changeset.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import mergeDeep from './utils/merge-deep';
import isObject from './utils/is-object';
import { tracked } from '@glimmer/tracking';
import { get as safeGet, set as safeSet } from '@ember/object';
import { macroCondition, dependencySatisfies, importSync } from '@embroider/macros';
import {
macroCondition,
dependencySatisfies,
importSync,
} from '@embroider/macros';

const CHANGES = '_changes';
const PREVIOUS_CONTENT = '_previousContent';
Expand Down Expand Up @@ -103,7 +107,11 @@ export class EmberValidationChangeset extends ValidationChangeset {
let content = this[CONTENT];
let changes = this[CHANGES];

let pendingChanges = this.mergeDeep(Object.create(Object.getPrototypeOf(content)), content, { safeGet, safeSet });
let pendingChanges = this.mergeDeep(
Object.create(Object.getPrototypeOf(content)),
content,
{ safeGet, safeSet },
);

return this.mergeDeep(pendingChanges, changes, { safeGet, safeSet });
}
Expand Down Expand Up @@ -202,7 +210,10 @@ export class EmberValidationChangeset extends ValidationChangeset {
*/
export function changeset(obj) {
assert('Underlying object for changeset is missing', Boolean(obj));
assert('Array is not a valid type to pass as the first argument to `changeset`', !Array.isArray(obj));
assert(
'Array is not a valid type to pass as the first argument to `changeset`',
!Array.isArray(obj),
);

const c = new EmberValidationChangeset(obj);
return c;
Expand Down
45 changes: 35 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@
"ember-template-lint": "^3.10.0",
"ember-try": "^2.0.0",
"eslint": "^8.57.1",
"eslint-config-prettier": "^8.10.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-ember": "^10.6.1",
"eslint-plugin-n": "^15.7.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-prettier": "^5.2.1",
"eslint-plugin-qunit": "^7.3.4",
"lerna-changelog": "^2.2.0",
"loader.js": "^4.7.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.8.8",
"prettier": "^3.3.3",
"qunit": "^2.17.2",
"qunit-dom": "^2.0.0",
"release-it": "^14.11.6",
Expand Down
4 changes: 2 additions & 2 deletions tests/dummy/app/components/changeset-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ let dummyValidations = {
() => {
resolve(ok);
},
400
)
400,
),
);
},
},
Expand Down
5 changes: 4 additions & 1 deletion tests/dummy/app/components/validated-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ export default class ValidatedForm extends Component {

await this.model.save();
} catch (e) {
this.changeset.addError(e.path, { value: this.changeset.get(e.path), validation: e.message });
this.changeset.addError(e.path, {
value: this.changeset.get(e.path),
validation: e.message,
});
}
}

Expand Down
Loading

0 comments on commit 71febde

Please sign in to comment.