|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import estree from 'estree'; |
| 21 | + |
| 22 | +export interface DisallowedSyntaxCheck { |
| 23 | + name: string; |
| 24 | + nodeType: estree.Node['type'] | Array<estree.Node['type']>; |
| 25 | + test?: (n: any) => boolean | void; |
| 26 | +} |
| 27 | + |
| 28 | +export const checks: DisallowedSyntaxCheck[] = [ |
| 29 | + /** |
| 30 | + * es2015 |
| 31 | + */ |
| 32 | + // https://github.com/estree/estree/blob/master/es2015.md#functions |
| 33 | + { |
| 34 | + name: '[es2015] generator function', |
| 35 | + nodeType: ['FunctionDeclaration', 'FunctionExpression'], |
| 36 | + test: (n: estree.FunctionDeclaration | estree.FunctionExpression) => !!n.generator, |
| 37 | + }, |
| 38 | + // https://github.com/estree/estree/blob/master/es2015.md#forofstatement |
| 39 | + { |
| 40 | + name: '[es2015] for-of statement', |
| 41 | + nodeType: 'ForOfStatement', |
| 42 | + }, |
| 43 | + // https://github.com/estree/estree/blob/master/es2015.md#variabledeclaration |
| 44 | + { |
| 45 | + name: '[es2015] let/const variable declaration', |
| 46 | + nodeType: 'VariableDeclaration', |
| 47 | + test: (n: estree.VariableDeclaration) => n.kind === 'let' || n.kind === 'const', |
| 48 | + }, |
| 49 | + // https://github.com/estree/estree/blob/master/es2015.md#expressions |
| 50 | + { |
| 51 | + name: '[es2015] `super`', |
| 52 | + nodeType: 'Super', |
| 53 | + }, |
| 54 | + // https://github.com/estree/estree/blob/master/es2015.md#expressions |
| 55 | + { |
| 56 | + name: '[es2015] ...spread', |
| 57 | + nodeType: 'SpreadElement', |
| 58 | + }, |
| 59 | + // https://github.com/estree/estree/blob/master/es2015.md#arrowfunctionexpression |
| 60 | + { |
| 61 | + name: '[es2015] arrow function expression', |
| 62 | + nodeType: 'ArrowFunctionExpression', |
| 63 | + }, |
| 64 | + // https://github.com/estree/estree/blob/master/es2015.md#yieldexpression |
| 65 | + { |
| 66 | + name: '[es2015] `yield` expression', |
| 67 | + nodeType: 'YieldExpression', |
| 68 | + }, |
| 69 | + // https://github.com/estree/estree/blob/master/es2015.md#templateliteral |
| 70 | + { |
| 71 | + name: '[es2015] template literal', |
| 72 | + nodeType: 'TemplateLiteral', |
| 73 | + }, |
| 74 | + // https://github.com/estree/estree/blob/master/es2015.md#patterns |
| 75 | + { |
| 76 | + name: '[es2015] destructuring', |
| 77 | + nodeType: ['ObjectPattern', 'ArrayPattern', 'AssignmentPattern'], |
| 78 | + }, |
| 79 | + // https://github.com/estree/estree/blob/master/es2015.md#classes |
| 80 | + { |
| 81 | + name: '[es2015] class', |
| 82 | + nodeType: [ |
| 83 | + 'ClassDeclaration', |
| 84 | + 'ClassExpression', |
| 85 | + 'ClassBody', |
| 86 | + 'MethodDefinition', |
| 87 | + 'MetaProperty', |
| 88 | + ], |
| 89 | + }, |
| 90 | + |
| 91 | + /** |
| 92 | + * es2016 |
| 93 | + */ |
| 94 | + { |
| 95 | + name: '[es2016] exponent operator', |
| 96 | + nodeType: 'BinaryExpression', |
| 97 | + test: (n: estree.BinaryExpression) => n.operator === '**', |
| 98 | + }, |
| 99 | + { |
| 100 | + name: '[es2016] exponent assignment', |
| 101 | + nodeType: 'AssignmentExpression', |
| 102 | + test: (n: estree.AssignmentExpression) => n.operator === '**=', |
| 103 | + }, |
| 104 | + |
| 105 | + /** |
| 106 | + * es2017 |
| 107 | + */ |
| 108 | + // https://github.com/estree/estree/blob/master/es2017.md#function |
| 109 | + { |
| 110 | + name: '[es2017] async function', |
| 111 | + nodeType: ['FunctionDeclaration', 'FunctionExpression'], |
| 112 | + test: (n: estree.FunctionDeclaration | estree.FunctionExpression) => n.async, |
| 113 | + }, |
| 114 | + // https://github.com/estree/estree/blob/master/es2017.md#awaitexpression |
| 115 | + { |
| 116 | + name: '[es2017] await expression', |
| 117 | + nodeType: 'AwaitExpression', |
| 118 | + }, |
| 119 | + |
| 120 | + /** |
| 121 | + * es2018 |
| 122 | + */ |
| 123 | + // https://github.com/estree/estree/blob/master/es2018.md#statements |
| 124 | + { |
| 125 | + name: '[es2018] for-await-of statements', |
| 126 | + nodeType: 'ForOfStatement', |
| 127 | + test: (n: estree.ForOfStatement) => n.await, |
| 128 | + }, |
| 129 | + // https://github.com/estree/estree/blob/master/es2018.md#expressions |
| 130 | + { |
| 131 | + name: '[es2018] object spread properties', |
| 132 | + nodeType: 'ObjectExpression', |
| 133 | + test: (n: estree.ObjectExpression) => n.properties.some(p => p.type === 'SpreadElement'), |
| 134 | + }, |
| 135 | + // https://github.com/estree/estree/blob/master/es2018.md#template-literals |
| 136 | + { |
| 137 | + name: '[es2018] tagged template literal with invalid escape', |
| 138 | + nodeType: 'TemplateElement', |
| 139 | + test: (n: estree.TemplateElement) => n.value.cooked === null, |
| 140 | + }, |
| 141 | + // https://github.com/estree/estree/blob/master/es2018.md#patterns |
| 142 | + { |
| 143 | + name: '[es2018] rest properties', |
| 144 | + nodeType: 'ObjectPattern', |
| 145 | + test: (n: estree.ObjectPattern) => n.properties.some(p => p.type === 'RestElement'), |
| 146 | + }, |
| 147 | + |
| 148 | + /** |
| 149 | + * es2019 |
| 150 | + */ |
| 151 | + // https://github.com/estree/estree/blob/master/es2019.md#catchclause |
| 152 | + { |
| 153 | + name: '[es2019] catch clause without a binding', |
| 154 | + nodeType: 'CatchClause', |
| 155 | + test: (n: estree.CatchClause) => !n.param, |
| 156 | + }, |
| 157 | + |
| 158 | + /** |
| 159 | + * es2020 |
| 160 | + */ |
| 161 | + // https://github.com/estree/estree/blob/master/es2020.md#bigintliteral |
| 162 | + { |
| 163 | + name: '[es2020] bigint literal', |
| 164 | + nodeType: 'Literal', |
| 165 | + test: (n: estree.Literal) => typeof n.value === 'bigint', |
| 166 | + }, |
| 167 | + |
| 168 | + /** |
| 169 | + * webpack transforms import/export in order to support tree shaking and async imports |
| 170 | + * |
| 171 | + * // https://github.com/estree/estree/blob/master/es2020.md#importexpression |
| 172 | + * { |
| 173 | + * name: '[es2020] import expression', |
| 174 | + * nodeType: 'ImportExpression', |
| 175 | + * }, |
| 176 | + * // https://github.com/estree/estree/blob/master/es2020.md#exportalldeclaration |
| 177 | + * { |
| 178 | + * name: '[es2020] export all declaration', |
| 179 | + * nodeType: 'ExportAllDeclaration', |
| 180 | + * }, |
| 181 | + * |
| 182 | + */ |
| 183 | +]; |
| 184 | + |
| 185 | +export const checksByNodeType = new Map<estree.Node['type'], DisallowedSyntaxCheck[]>(); |
| 186 | +for (const check of checks) { |
| 187 | + const nodeTypes = Array.isArray(check.nodeType) ? check.nodeType : [check.nodeType]; |
| 188 | + for (const nodeType of nodeTypes) { |
| 189 | + if (!checksByNodeType.has(nodeType)) { |
| 190 | + checksByNodeType.set(nodeType, []); |
| 191 | + } |
| 192 | + checksByNodeType.get(nodeType)!.push(check); |
| 193 | + } |
| 194 | +} |
0 commit comments