|
| 1 | +"use strict"; |
| 2 | +const esprima = require("esprima"); |
| 3 | +const {Syntax} = esprima; |
| 4 | +const {getPropertyKey, isMethodCall, isIdentifier, getStringArray} = require("../utils/ASTUtils"); |
| 5 | +const VisitorKeys = require("estraverse").VisitorKeys; |
| 6 | + |
| 7 | +const CALL__SAP_UI_GETCORE = ["sap", "ui", "getCore"]; |
| 8 | + |
| 9 | +/* |
| 10 | + * Static Code Analyzer that extracts library information from the sap.ui.getCore().initLibrary() |
| 11 | + * call in a library.js module. |
| 12 | + */ |
| 13 | +async function analyze(resource) { |
| 14 | + let libInfo = { |
| 15 | + noLibraryCSS: false, |
| 16 | + types: [], |
| 17 | + controls: [], |
| 18 | + elements: [], |
| 19 | + interfaces: [] |
| 20 | + }; |
| 21 | + |
| 22 | + function visit(node) { |
| 23 | + if ( node.type == Syntax.CallExpression |
| 24 | + && node.callee.type === Syntax.MemberExpression |
| 25 | + && isMethodCall(node.callee.object, CALL__SAP_UI_GETCORE) |
| 26 | + && isIdentifier(node.callee.property, "initLibrary") |
| 27 | + && node.arguments.length === 1 |
| 28 | + && node.arguments[0].type === Syntax.ObjectExpression ) { |
| 29 | + node.arguments[0].properties.forEach( (prop) => { |
| 30 | + let key = getPropertyKey(prop); |
| 31 | + let value = prop.value; |
| 32 | + if ( key === "noLibraryCSS" |
| 33 | + && (value.type === Syntax.Literal && typeof value.value === "boolean") ) { |
| 34 | + libInfo.noLibraryCSS = value.value; |
| 35 | + } else if ( key === "types" && value.type == Syntax.ArrayExpression ) { |
| 36 | + libInfo.types = getStringArray(value, true); |
| 37 | + } else if ( key === "interfaces" && value.type == Syntax.ArrayExpression ) { |
| 38 | + libInfo.interfaces = getStringArray(value, true); |
| 39 | + } else if ( key === "controls" && value.type == Syntax.ArrayExpression ) { |
| 40 | + libInfo.controls = getStringArray(value, true); |
| 41 | + } else if ( key === "elements" && value.type == Syntax.ArrayExpression ) { |
| 42 | + libInfo.elements = getStringArray(value, true); |
| 43 | + } |
| 44 | + }); |
| 45 | + |
| 46 | + return true; // abort, we're done |
| 47 | + } |
| 48 | + |
| 49 | + for ( let key of VisitorKeys[node.type] ) { |
| 50 | + let child = node[key]; |
| 51 | + if ( Array.isArray(child) ) { |
| 52 | + if ( child.some(visit) ) { |
| 53 | + return true; |
| 54 | + } |
| 55 | + } else if ( child ) { |
| 56 | + if ( visit(child) ) { |
| 57 | + return true; |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return false; |
| 63 | + } |
| 64 | + |
| 65 | + let code = await resource.getBuffer(); |
| 66 | + visit( esprima.parse(code) ); |
| 67 | + |
| 68 | + return libInfo; |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * Creates a new analyzer and executes it on the given resource. |
| 73 | + * |
| 74 | + * If the resources exists and can be parsed as JavaScript and if an sap.ui.getCore().initLibrary() |
| 75 | + * call is found in the code, then the following information will be set: |
| 76 | + * <ul> |
| 77 | + * <li>noLibraryCSS: false when the noLibraryCSS property had been set in the initLibrary info object)</li> |
| 78 | + * <li>types: string array with the names of the types contained in the library</li> |
| 79 | + * <li>controls: string array with the names of the controls defined in the library</li> |
| 80 | + * <li>elements: string array with the names of the elements defined in the library</li> |
| 81 | + * <li>interfaces: string array with the names of the interfaces defined in the library</li> |
| 82 | + * </ul> |
| 83 | + * |
| 84 | + * Note: only the first initLibrary() call that is found with a DFS on the AST, will be evaluated. |
| 85 | + * |
| 86 | + * @param {Resource} resource library.js resource whose content should be analyzed |
| 87 | + * @returns {Promise<object>} A Promise on the extract info object |
| 88 | + */ |
| 89 | +module.exports = function(resource) { |
| 90 | + if ( resource == null ) { |
| 91 | + return Promise.resolve({}); |
| 92 | + } |
| 93 | + return analyze(resource); |
| 94 | +}; |
0 commit comments