-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Changing load/dump in source files #190641
Merged
kc13greiner
merged 14 commits into
elastic:main
from
kc13greiner:feature/change_load_dump
Aug 21, 2024
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
0525b00
Changing load/dump in source files
kc13greiner 260680b
update usage of load/dump from js-yaml to use safeLoad and safeDump
SiddharthMantri 9a420ab
fix lint errors
SiddharthMantri d2fefb8
Updates 4 additional references
jeramysoucy 407ba6a
Merge branch 'main' into feature/change_load_dump
jeramysoucy e9d20a4
Reverts updates to buildkite, already using v4
jeramysoucy b8eaa17
Merge branch 'main' into feature/change_load_dump
jeramysoucy c2b18f9
Fixes open API write and bundle to skip invalid values rather than th…
jeramysoucy 415d452
Adds comment in remove props test about safeDump and the skipInvalid …
jeramysoucy 89f18e8
Merge branch 'main' into feature/change_load_dump
jeramysoucy 49ee5a4
Added no_unsafe_js_yaml eslint rule
elena-shostak f165d5a
[CI] Auto-commit changed files from 'node scripts/eslint --no-cache -…
kibanamachine 04a56a3
adding skips so ES lint wont auto-change these
kc13greiner 3c3f076
make adjustments to kbn-openapi-bundler
maximpn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
packages/kbn-eslint-plugin-eslint/rules/no_unsafe_js_yaml.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* 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 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 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; | ||
}, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
104 changes: 104 additions & 0 deletions
104
packages/kbn-eslint-plugin-eslint/rules/no_unsafe_js_yaml.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* 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 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 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');", | ||
}, | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding
skipInvalid: true
allows the files to be written even if they contain invalid types (e.g.undefined
). The result is that the invalid types will not be written to the file rather than the default behavior of throwing an exception.Previous use of the
dump
function would write files containing invalid types. It looks like we relied on parsing the file contents to handling removing the invalid values, which may no longer be necessary.