UNPKG

eslint-plugin-sonarjs

Version:
188 lines (187 loc) • 7.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ModuleInitializerTracker = void 0; const ast_js_1 = require("../helpers/ast.js"); const helpers_js_1 = require("./helpers.js"); const SAP_UI_DEFINE = 'sap.ui.define'; const MAX_SCOPE_INITIALIZER_ARGUMENTS = 3; /** * Track direct loader factories and writes to their ambient API paths. * * `define([], function () {})` can be excluded without a nested function. * `sap.ui.define([], function () { function nested() {} })` can be excluded. * A write such as `sap.ui = other` keeps both functions checked. */ class ModuleInitializerTracker { constructor(hasRuntimeBinding) { this.hasRuntimeBinding = hasRuntimeBinding; this.scopeInitializers = new Map(); this.writeTargets = []; } invalidateWithWrite(target, remainingPaths) { const value = (0, helpers_js_1.unwrapTypeScriptValue)(target); const path = (0, helpers_js_1.getModuleLoaderPath)(value, true); if (path) { this.invalidateLoaderPath(value, path, remainingPaths); return; } switch (value.type) { case 'ArrayPattern': this.invalidateArrayPattern(value, remainingPaths); break; case 'ObjectPattern': this.invalidateObjectPattern(value, remainingPaths); break; case 'AssignmentPattern': this.invalidateWithWrite(value.left, remainingPaths); break; case 'RestElement': this.invalidateWithWrite(value.argument, remainingPaths); break; } } invalidateLoaderPath(value, path, remainingPaths) { if (this.hasRuntimeBinding(value, path[0] ?? '')) { return; } for (const [key, candidate] of remainingPaths) { if (isPathPrefix(path, candidate)) { remainingPaths.delete(key); } } } invalidateArrayPattern(pattern, remainingPaths) { for (const element of pattern.elements) { if (remainingPaths.size === 0) { break; } if (element) { this.invalidateWithWrite(element, remainingPaths); } } } invalidateObjectPattern(pattern, remainingPaths) { for (const property of pattern.properties) { if (remainingPaths.size === 0) { break; } const target = property.type === 'RestElement' ? property.argument : property.value; this.invalidateWithWrite(target, remainingPaths); } } recordCall(call) { const amdFactory = (0, helpers_js_1.getModuleFactory)(call, this.hasRuntimeBinding); if (amdFactory) { this.scopeInitializers.set(amdFactory, ['define']); } const initializer = getScopeInitializer(call, this.hasRuntimeBinding); if (initializer) { this.scopeInitializers.set(initializer.factory, initializer.path); } } recordWrite(target) { this.writeTargets.push(target); } /** Resolve the file-wide write guard after visiting the whole file. */ getExcludedFactories(hasNestedFunction) { const excluded = new Set(); if (this.scopeInitializers.size === 0) { return excluded; } const eligibleFactories = new Map(); const remainingPaths = new Map(); for (const [factory, path] of this.scopeInitializers) { if (path[0] === 'define' || hasNestedFunction(factory)) { const key = path.join('.'); eligibleFactories.set(factory, key); remainingPaths.set(key, path); } } for (const target of this.writeTargets) { if (remainingPaths.size === 0) { break; } this.invalidateWithWrite(target, remainingPaths); } for (const [factory, key] of eligibleFactories) { if (remainingPaths.has(key)) { excluded.add(factory); } } return excluded; } } exports.ModuleInitializerTracker = ModuleInitializerTracker; /** * `sap.ui.define([], function () {})` -> factory and ['sap', 'ui', 'define'] * `sap.ui['define']([], function () {})` -> undefined */ function getScopeInitializer(call, hasRuntimeBinding) { const { callee } = call; if (('optional' in call && call.optional) || call.arguments.length < 2 || call.arguments.length > MAX_SCOPE_INITIALIZER_ARGUMENTS + 1 || (callee.type === 'Identifier' ? callee.name !== 'require' : callee.type !== 'MemberExpression' || callee.computed || callee.optional || callee.property.type !== 'Identifier' || !['define', 'require', 'register'].includes(callee.property.name))) { return undefined; } const path = (0, helpers_js_1.getModuleLoaderPath)(callee, false); const name = path?.join('.') ?? ''; if (!path || ![SAP_UI_DEFINE, 'require', 'sap.ui.require', 'System.register'].includes(name)) { return undefined; } const args = call.arguments.map(helpers_js_1.unwrapTypeScriptValue); if (name === SAP_UI_DEFINE && (0, helpers_js_1.isStringLiteral)(args[0])) { args.shift(); } const [dependencies, factory, extra] = args; if (args.length < 2 || args.length > MAX_SCOPE_INITIALIZER_ARGUMENTS || dependencies?.type !== 'ArrayExpression' || !dependencies.elements.every(helpers_js_1.isStringLiteral) || !isSynchronousFunction(factory)) { return undefined; } if (!isValidScopeInitializerExtra(name, extra, dependencies.elements.length) || hasRuntimeBinding(call, path[0])) { return undefined; } return { factory, path }; } /** * `sap.ui.define([], function () {}, true)` -> true * `System.register(['dep'], function () {}, [{}])` -> true * `sap.ui.define([], function () {}, 0)` -> false */ function isValidScopeInitializerExtra(name, extra, dependencyCount) { if (!extra) { return true; } if (name === SAP_UI_DEFINE) { return extra.type === 'Literal' && typeof extra.value === 'boolean'; } if (name === 'System.register') { return (extra.type === 'ArrayExpression' && extra.elements.length === dependencyCount && extra.elements.every(element => element ? (0, helpers_js_1.unwrapTypeScriptValue)(element).type === 'ObjectExpression' : false)); } return (0, ast_js_1.isFunctionNode)(extra); } /** * `function () {}` -> true * `async function () {}` -> false * `() => {}` -> false */ function isSynchronousFunction(node) { return node?.type === 'FunctionExpression' && !node.async && !node.generator; } /** `['sap', 'ui', undefined]` matches `['sap', 'ui', 'define']`. */ function isPathPrefix(prefix, path) { return (prefix.length <= path.length && prefix.every((part, index) => part === undefined || part === path[index])); }