Deleted no_unsafe_js_yaml eslint rule (#193588)

## Summary

Since js-yaml update has been merged in
https://github.com/elastic/kibana/pull/190678 we don't need
`no_unsafe_js_yaml ` anymore
This commit is contained in:
Elena Shostak 2024-09-20 18:06:15 +02:00 committed by GitHub
parent 4172226750
commit f1a7835cbb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 0 additions and 197 deletions

View file

@ -19,6 +19,5 @@ module.exports = {
no_constructor_args_in_property_initializers: require('./rules/no_constructor_args_in_property_initializers'),
no_this_in_property_initializers: require('./rules/no_this_in_property_initializers'),
no_unsafe_console: require('./rules/no_unsafe_console'),
no_unsafe_js_yaml: require('./rules/no_unsafe_js_yaml'),
},
};

View file

@ -1,91 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/
module.exports = {
meta: {
fixable: 'code',
schema: [],
},
create(context) {
const sourceCode = context.getSourceCode();
const jsYamlIdentifiers = new Set();
const isUnsafeMethod = (node) => node.name === 'load' || node.name === 'dump';
return {
ImportDeclaration(node) {
if (node.source.value === 'js-yaml') {
node.specifiers.forEach((specifier) => {
jsYamlIdentifiers.add(specifier.local.name);
if (specifier.imported && isUnsafeMethod(specifier.imported)) {
context.report({
node: specifier,
message:
'Use `safeLoad` instead of `load` and `safeDump` instead of `dump` from `js-yaml`.',
fix(fixer) {
const replacement =
specifier.imported.name === 'load'
? fixer.replaceText(specifier.imported, 'safeLoad')
: fixer.replaceText(specifier.imported, 'safeDump');
return replacement;
},
});
}
});
}
},
CallExpression(node) {
const callee = node.callee;
if (isUnsafeMethod(callee)) {
const scope = sourceCode.getScope(node);
const variable = scope.variables.find((v) => v.name === callee.name);
if (variable && variable.defs.length) {
const [def] = variable.defs;
if (def?.parent?.source?.value === 'js-yaml') {
context.report({
node: callee,
message:
'Use `safeLoad` instead of `load` and `safeDump` instead of `dump` from `js-yaml`.',
fix(fixer) {
const replacement =
callee.name === 'load'
? fixer.replaceText(callee, 'safeLoad')
: fixer.replaceText(callee, 'safeDump');
return replacement;
},
});
}
}
}
if (
callee.type === 'MemberExpression' &&
isUnsafeMethod(callee.property) &&
jsYamlIdentifiers.has(callee.object.name)
) {
context.report({
node: callee.property,
message:
'Use `safeLoad` instead of `load` and `safeDump` instead of `dump` from `js-yaml`.',
fix(fixer) {
const replacement =
callee.property.name === 'load'
? fixer.replaceText(callee.property, 'safeLoad')
: fixer.replaceText(callee.property, 'safeDump');
return replacement;
},
});
}
},
};
},
};

View file

@ -1,105 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/
const { RuleTester } = require('eslint');
const rule = require('./no_unsafe_js_yaml');
const ruleTester = new RuleTester({
parser: require.resolve('@typescript-eslint/parser'),
parserOptions: {
sourceType: 'module',
ecmaVersion: 2018,
},
});
ruleTester.run('no_unsafe_js_yaml', rule, {
valid: [
"import { safeLoad } from 'js-yaml'; const data = safeLoad(yamlString);",
"import { safeDump } from 'js-yaml'; const yaml = safeDump(data);",
"import * as yaml from 'js-yaml'; const data = yaml.safeLoad(yamlString);",
"import yaml from 'js-yaml'; yaml.safeLoad('yamlString');",
],
invalid: [
{
code: "import { load } from 'js-yaml'; const data = load(yamlString);",
errors: [
{
message:
'Use `safeLoad` instead of `load` and `safeDump` instead of `dump` from `js-yaml`.',
line: 1,
column: 10,
endLine: 1,
endColumn: 14,
},
{
message:
'Use `safeLoad` instead of `load` and `safeDump` instead of `dump` from `js-yaml`.',
line: 1,
column: 46,
endLine: 1,
endColumn: 50,
},
],
output: "import { safeLoad } from 'js-yaml'; const data = safeLoad(yamlString);",
},
{
code: "import { dump } from 'js-yaml'; const yaml = dump(data);",
errors: [
{
message:
'Use `safeLoad` instead of `load` and `safeDump` instead of `dump` from `js-yaml`.',
line: 1,
column: 10,
endLine: 1,
endColumn: 14,
},
{
message:
'Use `safeLoad` instead of `load` and `safeDump` instead of `dump` from `js-yaml`.',
line: 1,
column: 46,
endLine: 1,
endColumn: 50,
},
],
output: "import { safeDump } from 'js-yaml'; const yaml = safeDump(data);",
},
{
code: "import * as yaml from 'js-yaml'; const data = yaml.load(yamlString);",
errors: [
{
message:
'Use `safeLoad` instead of `load` and `safeDump` instead of `dump` from `js-yaml`.',
},
],
output: "import * as yaml from 'js-yaml'; const data = yaml.safeLoad(yamlString);",
},
{
code: "import yaml from 'js-yaml'; yaml.load('someYAMLContent')",
errors: [
{
message:
'Use `safeLoad` instead of `load` and `safeDump` instead of `dump` from `js-yaml`.',
},
],
output: "import yaml from 'js-yaml'; yaml.safeLoad('someYAMLContent')",
},
{
code: "import yaml, { safeDump } from 'js-yaml'; safeDump(data); yaml.load('someYAMLContent');",
errors: [
{
message:
'Use `safeLoad` instead of `load` and `safeDump` instead of `dump` from `js-yaml`.',
},
],
output:
"import yaml, { safeDump } from 'js-yaml'; safeDump(data); yaml.safeLoad('someYAMLContent');",
},
],
});