diff --git a/packages/kbn-eslint-plugin-eslint/index.js b/packages/kbn-eslint-plugin-eslint/index.js index dadeb85832da7..1b9c04a2b7918 100644 --- a/packages/kbn-eslint-plugin-eslint/index.js +++ b/packages/kbn-eslint-plugin-eslint/index.js @@ -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'), }, }; diff --git a/packages/kbn-eslint-plugin-eslint/rules/no_unsafe_js_yaml.js b/packages/kbn-eslint-plugin-eslint/rules/no_unsafe_js_yaml.js deleted file mode 100644 index 74dc20df93af6..0000000000000 --- a/packages/kbn-eslint-plugin-eslint/rules/no_unsafe_js_yaml.js +++ /dev/null @@ -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; - }, - }); - } - }, - }; - }, -}; diff --git a/packages/kbn-eslint-plugin-eslint/rules/no_unsafe_js_yaml.test.js b/packages/kbn-eslint-plugin-eslint/rules/no_unsafe_js_yaml.test.js deleted file mode 100644 index 960bc0b0c23d1..0000000000000 --- a/packages/kbn-eslint-plugin-eslint/rules/no_unsafe_js_yaml.test.js +++ /dev/null @@ -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');", - }, - ], -});