Skip to content

Commit

Permalink
Fix eslint rules and apply them to utils, templates, options and igno…
Browse files Browse the repository at this point in the history
…re errors
  • Loading branch information
ivanblinov2k17 committed Oct 17, 2024
1 parent e4fd6fb commit 28296da
Show file tree
Hide file tree
Showing 33 changed files with 2,223 additions and 2,208 deletions.
30 changes: 24 additions & 6 deletions packages/devextreme/js/__internal/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ module.exports = {
],
ignorePatterns: [
// for test
'core/**',
// files, that have significant changes from lint (IIFE or function lose context)
'core/utils/deferred.ts',
'core/utils/m_resize_callbacks.ts',
'core/utils/m_string.ts',
'core/utils/m_view_port.ts',
'core/m_*.ts',
],
overrides: [
// General TS rules.
Expand Down Expand Up @@ -208,6 +203,29 @@ module.exports = {
'@typescript-eslint/explicit-function-return-type': 'warn'
}
},
// Rules for migrated core files.
{
files: [
'**/core/options/m_*.ts',
'**/core/templates/m_*.ts',
'**/core/utils/m_*.ts',
],
parser: '@typescript-eslint/parser',
parserOptions: {
createDefaultProgram: true,
project: './tsconfig.json',
tsconfigRootDir: __dirname,
},
rules: {
'guard-for-in': 'off',
'no-restricted-syntax': 'off',
'func-style': 'off',
'wrap-iife': 'off',
'prefer-arrow-callback': 'off',
'@typescript-eslint/prefer-optional-chain': 'off',
'radix': 'off'
},
},
],
settings: {
'import/resolver': {
Expand Down
194 changes: 98 additions & 96 deletions packages/devextreme/js/__internal/core/utils/m_ajax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,121 +61,123 @@ const postProcess = function (deferred, xhr, dataType) {
}
};

const setHttpTimeout = function(timeout, xhr) {
return timeout && setTimeout(function() {
xhr.customStatus = TIMEOUT;
xhr.abort();
}, timeout);
const setHttpTimeout = function (timeout, xhr) {
return timeout && setTimeout(function () {
xhr.customStatus = TIMEOUT;
xhr.abort();
}, timeout);
};

const sendRequest = function(options) {
const xhr = httpRequest.getXhr();
// @ts-expect-error only void function can be called with new
const d = new Deferred();
const result = d.promise();
const async = isDefined(options.async) ? options.async : true;
const dataType = options.dataType;
const timeout = options.timeout || 0;
let timeoutId;

options.crossDomain = isCrossDomain(options.url);
const needScriptEvaluation = dataType === 'jsonp' || dataType === 'script';

if(options.cache === undefined) {
options.cache = !needScriptEvaluation;
}

const callbackName = getJsonpOptions(options);
const headers = getRequestHeaders(options);
const requestOptions = getRequestOptions(options, headers);
const url = requestOptions.url;
const parameters = requestOptions.parameters;

if(callbackName) {
// @ts-expect-error window[callback] is window type
window[callbackName] = function(data) {
d.resolve(data, SUCCESS, xhr);
};
}
const sendRequest = function (options) {
const xhr = httpRequest.getXhr();
// @ts-expect-error only void function can be called with new
const d = new Deferred();
const result = d.promise();
const async = isDefined(options.async) ? options.async : true;
const { dataType } = options;
const timeout = options.timeout || 0;
let timeoutId;

options.crossDomain = isCrossDomain(options.url);
const needScriptEvaluation = dataType === 'jsonp' || dataType === 'script';

if (options.cache === undefined) {
options.cache = !needScriptEvaluation;
}

if(options.crossDomain && needScriptEvaluation) {
const reject = function() {
d.reject(xhr, ERROR);
};
const resolve = function() {
if(dataType === 'jsonp') return;
d.resolve(null, SUCCESS, xhr);
};

evalCrossDomainScript(url).then(resolve, reject);
return result;
}
const callbackName = getJsonpOptions(options);
const headers = getRequestHeaders(options);
const requestOptions = getRequestOptions(options, headers);
const { url } = requestOptions;
const { parameters } = requestOptions;

if(options.crossDomain && !('withCredentials' in xhr)) {
d.reject(xhr, ERROR);
return result;
}
if (callbackName) {
// @ts-expect-error window[callback] is window type
window[callbackName] = function (data) {
d.resolve(data, SUCCESS, xhr);
};
}

xhr.open(
getMethod(options),
url,
async,
options.username,
options.password);
if (options.crossDomain && needScriptEvaluation) {
const reject = function () {
d.reject(xhr, ERROR);
};
const resolve = function () {
if (dataType === 'jsonp') return;
d.resolve(null, SUCCESS, xhr);
};

if(async) {
xhr.timeout = timeout;
timeoutId = setHttpTimeout(timeout, xhr);
}
evalCrossDomainScript(url).then(resolve, reject);
return result;
}

xhr['onreadystatechange'] = function(e) {
if(xhr.readyState === 4) {
clearTimeout(timeoutId);
if(isStatusSuccess(xhr.status)) {
if(hasContent(xhr.status)) {
postProcess(d, xhr, dataType);
} else {
d.resolve(null, NO_CONTENT, xhr);
}
} else {
d.reject(xhr, xhr.customStatus || ERROR);
}
}
};
if (options.crossDomain && !('withCredentials' in xhr)) {
d.reject(xhr, ERROR);
return result;
}

if(options.upload) {
xhr.upload['onprogress'] = options.upload['onprogress'];
xhr.upload['onloadstart'] = options.upload['onloadstart'];
xhr.upload['onabort'] = options.upload['onabort'];
}
xhr.open(
getMethod(options),
url,
async,
options.username,
options.password,
);

if (async) {
xhr.timeout = timeout;
timeoutId = setHttpTimeout(timeout, xhr);
}

if(options.xhrFields) {
for(const field in options.xhrFields) {
xhr[field] = options.xhrFields[field];
// eslint-disable-next-line @typescript-eslint/no-unused-vars
xhr.onreadystatechange = function (e) {
if (xhr.readyState === 4) {
clearTimeout(timeoutId);
if (isStatusSuccess(xhr.status)) {
if (hasContent(xhr.status)) {
postProcess(d, xhr, dataType);
} else {
d.resolve(null, NO_CONTENT, xhr);
}
} else {
d.reject(xhr, xhr.customStatus || ERROR);
}
}
};

if(options.responseType === 'arraybuffer') {
xhr.responseType = options.responseType;
}
if (options.upload) {
xhr.upload.onprogress = options.upload.onprogress;
xhr.upload.onloadstart = options.upload.onloadstart;
xhr.upload.onabort = options.upload.onabort;
}

for(const name in headers) {
if(Object.prototype.hasOwnProperty.call(headers, name) && isDefined(headers[name])) {
xhr.setRequestHeader(name, headers[name]);
}
if (options.xhrFields) {
for (const field in options.xhrFields) {
xhr[field] = options.xhrFields[field];
}
}

if(options.beforeSend) {
options.beforeSend(xhr);
if (options.responseType === 'arraybuffer') {
xhr.responseType = options.responseType;
}

for (const name in headers) {
if (Object.prototype.hasOwnProperty.call(headers, name) && isDefined(headers[name])) {
xhr.setRequestHeader(name, headers[name]);
}
}

xhr.send(parameters);
if (options.beforeSend) {
options.beforeSend(xhr);
}

result.abort = function() {
xhr.abort();
};
xhr.send(parameters);

return result;
result.abort = function () {
xhr.abort();
};

return result;
};

const Ajax = injector({ sendRequest });
Expand Down
Loading

0 comments on commit 28296da

Please sign in to comment.