requireIndexerName.js.flow 803 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import {
  2. getParameterName,
  3. } from '../utilities';
  4. const schema = [
  5. {
  6. enum: ['always', 'never'],
  7. type: 'string',
  8. },
  9. ];
  10. const create = (context) => {
  11. const always = (context.options[0] || 'always') === 'always';
  12. if (always) {
  13. return {
  14. ObjectTypeIndexer(node) {
  15. const id = getParameterName(node, context);
  16. const rawKeyType = context.getSourceCode().getText(node.key);
  17. if (id === null) {
  18. context.report({
  19. fix(fixer) {
  20. return fixer.replaceText(node.key, `key: ${rawKeyType}`);
  21. },
  22. message: 'All indexers must be declared with key name.',
  23. node,
  24. });
  25. }
  26. },
  27. };
  28. }
  29. return {};
  30. };
  31. export default {
  32. create,
  33. meta: {
  34. fixable: 'code',
  35. },
  36. schema,
  37. };