-
Notifications
You must be signed in to change notification settings - Fork 8.2k
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
[optimizer] validate the syntax of bundled node_modules #59972
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
73 changes: 73 additions & 0 deletions
73
...ker/__snapshots__/parse_path.test.ts.snap → ...mon/__snapshots__/parse_path.test.ts.snap
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
194 changes: 194 additions & 0 deletions
194
packages/kbn-optimizer/src/common/disallowed_syntax_plugin/disallowed_syntax.ts
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,194 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import estree from 'estree'; | ||
|
||
export interface DisallowedSyntaxCheck { | ||
name: string; | ||
nodeType: estree.Node['type'] | Array<estree.Node['type']>; | ||
test?: (n: any) => boolean | void; | ||
} | ||
|
||
export const checks: DisallowedSyntaxCheck[] = [ | ||
/** | ||
* es2015 | ||
*/ | ||
// https://github.com/estree/estree/blob/master/es2015.md#functions | ||
{ | ||
name: '[es2015] generator function', | ||
nodeType: ['FunctionDeclaration', 'FunctionExpression'], | ||
test: (n: estree.FunctionDeclaration | estree.FunctionExpression) => !!n.generator, | ||
}, | ||
// https://github.com/estree/estree/blob/master/es2015.md#forofstatement | ||
{ | ||
name: '[es2015] for-of statement', | ||
nodeType: 'ForOfStatement', | ||
}, | ||
// https://github.com/estree/estree/blob/master/es2015.md#variabledeclaration | ||
{ | ||
name: '[es2015] let/const variable declaration', | ||
nodeType: 'VariableDeclaration', | ||
test: (n: estree.VariableDeclaration) => n.kind === 'let' || n.kind === 'const', | ||
}, | ||
// https://github.com/estree/estree/blob/master/es2015.md#expressions | ||
{ | ||
name: '[es2015] `super`', | ||
nodeType: 'Super', | ||
}, | ||
// https://github.com/estree/estree/blob/master/es2015.md#expressions | ||
{ | ||
name: '[es2015] ...spread', | ||
nodeType: 'SpreadElement', | ||
}, | ||
// https://github.com/estree/estree/blob/master/es2015.md#arrowfunctionexpression | ||
{ | ||
name: '[es2015] arrow function expression', | ||
nodeType: 'ArrowFunctionExpression', | ||
}, | ||
// https://github.com/estree/estree/blob/master/es2015.md#yieldexpression | ||
{ | ||
name: '[es2015] `yield` expression', | ||
nodeType: 'YieldExpression', | ||
}, | ||
// https://github.com/estree/estree/blob/master/es2015.md#templateliteral | ||
{ | ||
name: '[es2015] template literal', | ||
nodeType: 'TemplateLiteral', | ||
}, | ||
// https://github.com/estree/estree/blob/master/es2015.md#patterns | ||
{ | ||
name: '[es2015] destructuring', | ||
nodeType: ['ObjectPattern', 'ArrayPattern', 'AssignmentPattern'], | ||
}, | ||
// https://github.com/estree/estree/blob/master/es2015.md#classes | ||
{ | ||
name: '[es2015] class', | ||
nodeType: [ | ||
'ClassDeclaration', | ||
'ClassExpression', | ||
'ClassBody', | ||
'MethodDefinition', | ||
'MetaProperty', | ||
], | ||
}, | ||
|
||
/** | ||
* es2016 | ||
*/ | ||
{ | ||
name: '[es2016] exponent operator', | ||
nodeType: 'BinaryExpression', | ||
test: (n: estree.BinaryExpression) => n.operator === '**', | ||
}, | ||
{ | ||
name: '[es2016] exponent assignment', | ||
nodeType: 'AssignmentExpression', | ||
test: (n: estree.AssignmentExpression) => n.operator === '**=', | ||
}, | ||
|
||
/** | ||
* es2017 | ||
*/ | ||
// https://github.com/estree/estree/blob/master/es2017.md#function | ||
{ | ||
name: '[es2017] async function', | ||
nodeType: ['FunctionDeclaration', 'FunctionExpression'], | ||
test: (n: estree.FunctionDeclaration | estree.FunctionExpression) => n.async, | ||
}, | ||
// https://github.com/estree/estree/blob/master/es2017.md#awaitexpression | ||
{ | ||
name: '[es2017] await expression', | ||
nodeType: 'AwaitExpression', | ||
}, | ||
|
||
/** | ||
* es2018 | ||
*/ | ||
// https://github.com/estree/estree/blob/master/es2018.md#statements | ||
{ | ||
name: '[es2018] for-await-of statements', | ||
nodeType: 'ForOfStatement', | ||
test: (n: estree.ForOfStatement) => n.await, | ||
}, | ||
// https://github.com/estree/estree/blob/master/es2018.md#expressions | ||
{ | ||
name: '[es2018] object spread properties', | ||
nodeType: 'ObjectExpression', | ||
test: (n: estree.ObjectExpression) => n.properties.some(p => p.type === 'SpreadElement'), | ||
}, | ||
// https://github.com/estree/estree/blob/master/es2018.md#template-literals | ||
{ | ||
name: '[es2018] tagged template literal with invalid escape', | ||
nodeType: 'TemplateElement', | ||
test: (n: estree.TemplateElement) => n.value.cooked === null, | ||
}, | ||
// https://github.com/estree/estree/blob/master/es2018.md#patterns | ||
{ | ||
name: '[es2018] rest properties', | ||
nodeType: 'ObjectPattern', | ||
test: (n: estree.ObjectPattern) => n.properties.some(p => p.type === 'RestElement'), | ||
}, | ||
|
||
/** | ||
* es2019 | ||
*/ | ||
// https://github.com/estree/estree/blob/master/es2019.md#catchclause | ||
{ | ||
name: '[es2019] catch clause without a binding', | ||
nodeType: 'CatchClause', | ||
test: (n: estree.CatchClause) => !n.param, | ||
}, | ||
|
||
/** | ||
* es2020 | ||
*/ | ||
// https://github.com/estree/estree/blob/master/es2020.md#bigintliteral | ||
{ | ||
name: '[es2020] bigint literal', | ||
nodeType: 'Literal', | ||
test: (n: estree.Literal) => typeof n.value === 'bigint', | ||
}, | ||
|
||
/** | ||
* webpack transforms import/export in order to support tree shaking and async imports | ||
* | ||
* // https://github.com/estree/estree/blob/master/es2020.md#importexpression | ||
* { | ||
* name: '[es2020] import expression', | ||
* nodeType: 'ImportExpression', | ||
* }, | ||
* // https://github.com/estree/estree/blob/master/es2020.md#exportalldeclaration | ||
* { | ||
* name: '[es2020] export all declaration', | ||
* nodeType: 'ExportAllDeclaration', | ||
* }, | ||
* | ||
*/ | ||
]; | ||
|
||
export const checksByNodeType = new Map<estree.Node['type'], DisallowedSyntaxCheck[]>(); | ||
for (const check of checks) { | ||
const nodeTypes = Array.isArray(check.nodeType) ? check.nodeType : [check.nodeType]; | ||
for (const nodeType of nodeTypes) { | ||
if (!checksByNodeType.has(nodeType)) { | ||
checksByNodeType.set(nodeType, []); | ||
} | ||
checksByNodeType.get(nodeType)!.push(check); | ||
} | ||
} |
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.
I would be surprised if we were to be the first project to have such needs. Isn't there no oss tools performing such checks atm?
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.
I looked around and didn't find anything, but I would be shocked if we could find something that would integrate with the webpack parser so tightly in order to avoid parsing the bundles all over again.
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.
Yea, from a quick glance, every webpack plugin integration (ie https://github.com/bitjourney/check-es-version-webpack-plugin/blob/master/index.js) are also using acorn at the end and parses the whole files again. None are doing the fine-grained checks you implemented though.