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

refactor: Use stricter TS, ESLint configs #2507

Merged
merged 1 commit into from
Apr 30, 2022
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
4 changes: 4 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"no-unused-expressions": 2,
"no-useless-call": 2,
"no-use-before-define": [2, "nofunc"],
"no-constant-binary-expression": 2,
"no-void": 2,

"jsdoc/require-jsdoc": 0,
Expand Down Expand Up @@ -83,6 +84,9 @@
"project": "./tsconfig.eslint.json"
},
"rules": {
"dot-notation": 0,
"curly": [2, "multi-line"],

"@typescript-eslint/prefer-for-of": 0,
"@typescript-eslint/member-ordering": 0,
"@typescript-eslint/explicit-function-return-type": 0,
Expand Down
6 changes: 3 additions & 3 deletions src/api/attributes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ describe('$(...)', () => {
'pear'
) as Cheerio<Element>;

expect($el[0].attribs.class).toBe('pear');
expect($el[0].attribs).toHaveProperty('class', 'pear');
expect($el[1].attribs).toBeUndefined();
expect($el[2].attribs.class).toBe('pear');
expect($el[2].attribs).toHaveProperty('class', 'pear');
});

it('(key, value) : should return an empty object for an empty object', () => {
Expand Down Expand Up @@ -204,7 +204,7 @@ describe('$(...)', () => {
it('(valid key) : valid prop should get value', () => {
expect(checkbox.prop('checked')).toBe(true);
checkbox.css('display', 'none');
expect(checkbox.prop('style').display).toBe('none');
expect(checkbox.prop('style')).toHaveProperty('display', 'none');
expect(checkbox.prop('style')).toHaveLength(1);
expect(checkbox.prop('style')).toContain('display');
expect(checkbox.prop('tagName')).toBe('INPUT');
Expand Down
29 changes: 16 additions & 13 deletions src/api/attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function getAttr(
// Mimic DOM with default value for radios/checkboxes
if (
elem.name === 'input' &&
(elem.attribs.type === 'radio' || elem.attribs.type === 'checkbox') &&
(elem.attribs['type'] === 'radio' || elem.attribs['type'] === 'checkbox') &&
name === 'value'
) {
return 'on';
Expand Down Expand Up @@ -387,13 +387,14 @@ export function prop<T extends AnyNode>(
throw new Error('Bad combination of arguments.');
}
return domEach(this, (el, i) => {
if (isTag(el))
if (isTag(el)) {
setProp(
el,
name,
value.call(el, i, getProp(el, name, this.options.xmlMode)),
this.options.xmlMode
);
}
});
}

Expand Down Expand Up @@ -599,9 +600,10 @@ export function data<T extends AnyNode>(
// Set the value (with attr map support)
if (typeof name === 'object' || value !== undefined) {
domEach(this, (el) => {
if (isTag(el))
if (isTag(el)) {
if (typeof name === 'object') setData(el, name);
else setData(el, name, value as unknown);
}
});
return this;
}
Expand Down Expand Up @@ -777,7 +779,7 @@ export function hasClass<T extends AnyNode>(
className: string
): boolean {
return this.toArray().some((elem) => {
const clazz = isTag(elem) && elem.attribs.class;
const clazz = isTag(elem) && elem.attribs['class'];
let idx = -1;

if (clazz && className.length) {
Expand Down Expand Up @@ -825,7 +827,7 @@ export function addClass<T extends AnyNode, R extends ArrayLike<T>>(
if (typeof value === 'function') {
return domEach(this, (el, i) => {
if (isTag(el)) {
const className = el.attribs.class || '';
const className = el.attribs['class'] || '';
addClass.call([el], value.call(el, i, className));
}
});
Expand Down Expand Up @@ -891,8 +893,9 @@ export function removeClass<T extends AnyNode, R extends ArrayLike<T>>(
// Handle if value is a function
if (typeof name === 'function') {
return domEach(this, (el, i) => {
if (isTag(el))
removeClass.call([el], name.call(el, i, el.attribs.class || ''));
if (isTag(el)) {
removeClass.call([el], name.call(el, i, el.attribs['class'] || ''));
}
});
}

Expand All @@ -905,9 +908,9 @@ export function removeClass<T extends AnyNode, R extends ArrayLike<T>>(

if (removeAll) {
// Short circuit the remove all case as this is the nice one
el.attribs.class = '';
el.attribs['class'] = '';
} else {
const elClasses = splitNames(el.attribs.class);
const elClasses = splitNames(el.attribs['class']);
let changed = false;

for (let j = 0; j < numClasses; j++) {
Expand All @@ -925,7 +928,7 @@ export function removeClass<T extends AnyNode, R extends ArrayLike<T>>(
}
}
if (changed) {
el.attribs.class = elClasses.join(' ');
el.attribs['class'] = elClasses.join(' ');
}
}
});
Expand Down Expand Up @@ -969,7 +972,7 @@ export function toggleClass<T extends AnyNode, R extends ArrayLike<T>>(
if (isTag(el)) {
toggleClass.call(
[el],
value.call(el, i, el.attribs.class || '', stateVal),
value.call(el, i, el.attribs['class'] || '', stateVal),
stateVal
);
}
Expand All @@ -989,7 +992,7 @@ export function toggleClass<T extends AnyNode, R extends ArrayLike<T>>(
// If selected element isn't a tag, move on
if (!isTag(el)) continue;

const elementClasses = splitNames(el.attribs.class);
const elementClasses = splitNames(el.attribs['class']);

// Check if class already exists
for (let j = 0; j < numClasses; j++) {
Expand All @@ -1005,7 +1008,7 @@ export function toggleClass<T extends AnyNode, R extends ArrayLike<T>>(
}
}

el.attribs.class = elementClasses.join(' ');
el.attribs['class'] = elementClasses.join(' ');
}

return this;
Expand Down
4 changes: 2 additions & 2 deletions src/api/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ function setCss(
styles[prop] = val;
}

el.attribs.style = stringify(styles);
el.attribs['style'] = stringify(styles);
} else if (typeof prop === 'object') {
Object.keys(prop).forEach((k, i) => {
setCss(el, k, prop[k], i);
Expand Down Expand Up @@ -146,7 +146,7 @@ function getCss(
): Record<string, string> | string | undefined {
if (!el || !isTag(el)) return;

const styles = parse(el.attribs.style);
const styles = parse(el.attribs['style']);
if (typeof prop === 'string') {
return styles[prop];
}
Expand Down
Loading