|
| 1 | +/** |
| 2 | + * @fileOverview Disallow the use of anonymous functions passed to scheduleOnce and once. |
| 3 | + */ |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +const { getCaller, cleanCaller, isCallingWithApply, isCallingWithCall } = require('../utils/caller'); |
| 7 | +const { collectObjectPatternBindings } = require('../utils/destructed-binding'); |
| 8 | +const { getEmberImportBinding } = require('../utils/imports'); |
| 9 | +const MESSAGE |
| 10 | + = `The uniqueness once offers is based on function uniqueness, each invocation of this line will always create a new |
| 11 | + function instance, resulting in uniqueness checks, that will never be hit. Please replace with a named function. |
| 12 | + Reference: https://emberjs.com/api/ember/2.14/namespaces/Ember.run/methods/scheduleOnce?anchor=scheduleOnce`; |
| 13 | + |
| 14 | +const DISALLOWED_OBJECTS = ['Ember.run', 'run']; |
| 15 | +const SCHEDULE_ONCE = 'scheduleOnce'; |
| 16 | +const RUN_METHODS = [SCHEDULE_ONCE, 'once']; |
| 17 | + |
| 18 | +/** |
| 19 | + * Extracts the method that we are trying to run once from the list of arguments. |
| 20 | + * An optional target parameter can be passed in as the first parameter so we need |
| 21 | + * to check the length of the array to determine where our function is being passed in. |
| 22 | + */ |
| 23 | +function getMethodToRunOnce(args) { |
| 24 | + return args.length > 1 ? args[1] : args[0]; |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * scheduleOnce takes in the queue name as the first parameter. In this function we will remove |
| 29 | + * that parameter from the array to make it look the same as the arguments to once and facilitate |
| 30 | + * extracting the method that we are trying to run once out of the args. |
| 31 | + */ |
| 32 | +function normalizeArguments(caller, args) { |
| 33 | + let mut = args.slice(); |
| 34 | + |
| 35 | + if (isCallingWithCall(caller)) { |
| 36 | + // Whenever the action was called .call we want to remove the context parameter |
| 37 | + mut.shift(); |
| 38 | + } else if (isCallingWithApply(caller)) { |
| 39 | + // Whenever the action was called with .apply we want to get the arguments with which the function |
| 40 | + // would actually get called |
| 41 | + mut = mut[1].elements.slice(); |
| 42 | + } |
| 43 | + |
| 44 | + // scheduleOnce takes in the queue name as the first parameter so we have to remove it have a similar |
| 45 | + // structure as "once" |
| 46 | + if (cleanCaller(caller).indexOf(SCHEDULE_ONCE) > -1) { |
| 47 | + mut.shift(); |
| 48 | + } |
| 49 | + |
| 50 | + return mut; |
| 51 | +} |
| 52 | + |
| 53 | +function isAnonymousFunction(fn) { |
| 54 | + return fn && !fn.name; |
| 55 | +} |
| 56 | + |
| 57 | +function isString(node) { |
| 58 | + return node.type === 'Literal' && typeof node.value === 'string'; |
| 59 | +} |
| 60 | + |
| 61 | +function mergeDisallowedCalls(objects) { |
| 62 | + return objects |
| 63 | + .reduce((calls, obj) => { |
| 64 | + RUN_METHODS.forEach((method) => { |
| 65 | + calls.push(`${obj}.${method}`); |
| 66 | + }); |
| 67 | + |
| 68 | + return calls; |
| 69 | + }, []); |
| 70 | +} |
| 71 | + |
| 72 | +module.exports = { |
| 73 | + docs: { |
| 74 | + description: 'Disallow use of anonymous functions when use in scheduleOnce or once', |
| 75 | + category: 'Best Practices', |
| 76 | + recommended: true |
| 77 | + }, |
| 78 | + meta: { |
| 79 | + message: MESSAGE |
| 80 | + }, |
| 81 | + create(context) { |
| 82 | + let emberImportBinding; |
| 83 | + let disallowedCalls = mergeDisallowedCalls(DISALLOWED_OBJECTS); |
| 84 | + |
| 85 | + return { |
| 86 | + ImportDefaultSpecifier(node) { |
| 87 | + emberImportBinding = getEmberImportBinding(node); |
| 88 | + }, |
| 89 | + |
| 90 | + ObjectPattern(node) { |
| 91 | + if (!emberImportBinding) { |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Retrieves the deconstructed bindings from the Ember import, accounting for aliasing |
| 97 | + * of the import. |
| 98 | + */ |
| 99 | + disallowedCalls = disallowedCalls.concat( |
| 100 | + mergeDisallowedCalls( |
| 101 | + collectObjectPatternBindings(node, { |
| 102 | + [emberImportBinding]: ['run'] |
| 103 | + }) |
| 104 | + ) |
| 105 | + ); |
| 106 | + }, |
| 107 | + |
| 108 | + CallExpression(node) { |
| 109 | + const caller = getCaller(node); |
| 110 | + |
| 111 | + if (!disallowedCalls.includes(cleanCaller(caller))) { |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + const normalizedArguments = normalizeArguments(caller, node.arguments); |
| 116 | + const fnToRunOnce = getMethodToRunOnce(normalizedArguments); |
| 117 | + |
| 118 | + // The fnToRunceOnce is a string it means that it will be resolved on the target at the time once or |
| 119 | + // scheduleOnce is invoked. |
| 120 | + if (isAnonymousFunction(fnToRunOnce) && !isString(fnToRunOnce)) { |
| 121 | + context.report(node, MESSAGE); |
| 122 | + } |
| 123 | + } |
| 124 | + }; |
| 125 | + } |
| 126 | +}; |
0 commit comments