File size: 3,468 Bytes
bc20498 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("../utils");
const svelte_store_1 = require("./reference-helpers/svelte-store");
exports.default = (0, utils_1.createRule)('derived-has-same-inputs-outputs', {
meta: {
docs: {
description: 'derived store should use same variable names between values and callback',
category: 'Stylistic Issues',
recommended: false,
conflictWithPrettier: false
},
schema: [],
messages: {
unexpected: "The argument name should be '{{name}}'."
},
type: 'suggestion'
},
create(context) {
/** check node type */
function isIdentifierOrArrayExpression(node) {
return ['Identifier', 'ArrayExpression'].includes(node.type);
}
/** check node type */
function isFunctionExpression(node) {
return ['ArrowFunctionExpression', 'FunctionExpression'].includes(node.type);
}
/**
* Check for identifier type.
* e.g. derived(a, ($a) => {});
*/
function checkIdentifier(context, args, fn) {
const fnParam = fn.params[0];
if (fnParam.type !== 'Identifier')
return;
const expectedName = `$${args.name}`;
if (expectedName !== fnParam.name) {
context.report({
node: fn,
loc: fnParam.loc,
messageId: 'unexpected',
data: { name: expectedName }
});
}
}
/**
* Check for array type.
* e.g. derived([ a, b ], ([ $a, $b ]) => {})
*/
function checkArrayExpression(context, args, fn) {
const fnParam = fn.params[0];
if (fnParam.type !== 'ArrayPattern')
return;
const argNames = args.elements.map((element) => {
return element && element.type === 'Identifier' ? element.name : null;
});
fnParam.elements.forEach((element, index) => {
const argName = argNames[index];
if (element && element.type === 'Identifier' && argName) {
const expectedName = `$${argName}`;
if (expectedName !== element.name) {
context.report({
node: fn,
loc: element.loc,
messageId: 'unexpected',
data: { name: expectedName }
});
}
}
});
}
return {
Program() {
for (const { node } of (0, svelte_store_1.extractStoreReferences)(context, ['derived'])) {
const [args, fn] = node.arguments;
if (!args || !isIdentifierOrArrayExpression(args))
continue;
if (!fn || !isFunctionExpression(fn))
continue;
if (!fn.params || fn.params.length === 0)
continue;
if (args.type === 'Identifier')
checkIdentifier(context, args, fn);
else
checkArrayExpression(context, args, fn);
}
}
};
}
});
|