|
| 1 | +/* |
| 2 | +Copyright 2021 Adobe. All rights reserved. |
| 3 | +This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. You may obtain a copy |
| 5 | +of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +Unless required by applicable law or agreed to in writing, software distributed under |
| 7 | +the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 8 | +OF ANY KIND, either express or implied. See the License for the specific language |
| 9 | +governing permissions and limitations under the License. |
| 10 | +*/ |
| 11 | + |
| 12 | +const availableOperations = require('../permissions.json').map(perm => perm.operation) |
| 13 | + |
| 14 | +module.exports = { |
| 15 | + create: function (context) { |
| 16 | + return { |
| 17 | + Program: (node) => { |
| 18 | + const classDeclarations = node.body.filter(bodyNode => bodyNode.type === 'ClassDeclaration') |
| 19 | + if (classDeclarations.length === 0) { |
| 20 | + context.report({ |
| 21 | + node: node, |
| 22 | + message: "Command file didn't contain command", |
| 23 | + }) |
| 24 | + } |
| 25 | + classDeclarations.forEach(classDeclaration => { |
| 26 | + const className = classDeclaration.id.name |
| 27 | + const propertiesAssignedToClass = node.body.map(bodyNode => { |
| 28 | + if (bodyNode.type === 'ExpressionStatement' && bodyNode.expression.type === 'AssignmentExpression' && |
| 29 | + bodyNode.expression.left.type === 'MemberExpression' && bodyNode.expression.left.object && bodyNode.expression.left.object.name === className && |
| 30 | + bodyNode.expression.left.property) { |
| 31 | + return { |
| 32 | + name: bodyNode.expression.left.property.name, |
| 33 | + node: bodyNode.expression.right, |
| 34 | + } |
| 35 | + } |
| 36 | + return {} |
| 37 | + }).filter(property => property.name) |
| 38 | + |
| 39 | + // if skipOrgIdCheck is defined, we don't need permissions |
| 40 | + if (!propertiesAssignedToClass.find(prop => prop.name === 'skipOrgIdCheck')) { |
| 41 | + const permissionInfo = propertiesAssignedToClass.find(prop => prop.name === 'permissionInfo') |
| 42 | + if (!permissionInfo) { |
| 43 | + context.report({ |
| 44 | + node: classDeclaration, |
| 45 | + message: 'Class {{ className }} does not define permissionInfo.', |
| 46 | + data: { |
| 47 | + className, |
| 48 | + }, |
| 49 | + }) |
| 50 | + } else if (permissionInfo.node.type !== 'ObjectExpression') { |
| 51 | + context.report({ |
| 52 | + node: permissionInfo.node, |
| 53 | + message: 'Class {{ className }} has permissionInfo but it is not an object.', |
| 54 | + data: { |
| 55 | + className, |
| 56 | + }, |
| 57 | + }) |
| 58 | + } else { |
| 59 | + const operation = permissionInfo.node.properties.find(prop => prop.key.name === 'operation') |
| 60 | + if (operation && operation.value) { |
| 61 | + if (operation.value.type !== 'Literal') { |
| 62 | + context.report({ |
| 63 | + node: permissionInfo.node, |
| 64 | + message: 'Class {{ className }} has permissionInfo with operation {{ operation }} but it is not a literal.', |
| 65 | + data: { |
| 66 | + className, |
| 67 | + operation, |
| 68 | + }, |
| 69 | + }) |
| 70 | + } else if (!availableOperations.includes(operation.value.value)) { |
| 71 | + context.report({ |
| 72 | + node: permissionInfo.node, |
| 73 | + message: 'Class {{ className }} has permissionInfo with operation {{ operation }} but that operation is not defined.', |
| 74 | + data: { |
| 75 | + className, |
| 76 | + operation: operation.value.value, |
| 77 | + }, |
| 78 | + }) |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + }) |
| 84 | + }, |
| 85 | + } |
| 86 | + }, |
| 87 | +} |
0 commit comments