|
| 1 | +const DEFAULT_MAX_LENGTH = 200; |
| 2 | +const DEFAULT_MIN_LENGTH = 5; |
| 3 | + |
| 4 | +const DEFAULT_CONFIG = { |
| 5 | + max: DEFAULT_MAX_LENGTH, |
| 6 | + min: DEFAULT_MIN_LENGTH, |
| 7 | +}; |
| 8 | + |
| 9 | +function isValidConfigObjectFormat(config) { |
| 10 | + for (const key of Object.keys(config)) { |
| 11 | + const value = config[key]; |
| 12 | + |
| 13 | + if (key !== 'min' && key !== 'max') { |
| 14 | + return false; |
| 15 | + } |
| 16 | + |
| 17 | + if (!Number.isInteger(value)) { |
| 18 | + return false; |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + return true; |
| 23 | +} |
| 24 | + |
| 25 | +function parseConfig(config) { |
| 26 | + const configType = typeof config; |
| 27 | + |
| 28 | + switch (configType) { |
| 29 | + case 'boolean': { |
| 30 | + return config ? DEFAULT_CONFIG : {}; |
| 31 | + } |
| 32 | + case 'object': { |
| 33 | + if (config === null) { |
| 34 | + break; |
| 35 | + } |
| 36 | + |
| 37 | + if (isValidConfigObjectFormat(config)) { |
| 38 | + return config; |
| 39 | + } |
| 40 | + break; |
| 41 | + } |
| 42 | + case 'undefined': { |
| 43 | + return {}; |
| 44 | + } |
| 45 | + // No default |
| 46 | + } |
| 47 | + |
| 48 | + throw new Error( |
| 49 | + 'The ember/template-template-length rule accepts: boolean, or object with integer "min" and/or "max" keys.' |
| 50 | + ); |
| 51 | +} |
| 52 | + |
| 53 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 54 | +module.exports = { |
| 55 | + meta: { |
| 56 | + type: 'suggestion', |
| 57 | + docs: { |
| 58 | + description: 'enforce template size constraints', |
| 59 | + category: 'Stylistic Issues', |
| 60 | + recommendedGjs: false, |
| 61 | + recommendedGts: false, |
| 62 | + url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-template-length.md', |
| 63 | + templateMode: 'both', |
| 64 | + }, |
| 65 | + schema: [ |
| 66 | + { |
| 67 | + oneOf: [ |
| 68 | + { type: 'boolean' }, |
| 69 | + { |
| 70 | + type: 'object', |
| 71 | + properties: { |
| 72 | + min: { type: 'integer' }, |
| 73 | + max: { type: 'integer' }, |
| 74 | + }, |
| 75 | + additionalProperties: false, |
| 76 | + }, |
| 77 | + ], |
| 78 | + }, |
| 79 | + ], |
| 80 | + messages: { |
| 81 | + tooLong: 'Template length of {{length}} exceeds {{max}}', |
| 82 | + tooShort: 'Template length of {{length}} is smaller than {{min}}', |
| 83 | + }, |
| 84 | + originallyFrom: { |
| 85 | + name: 'ember-template-lint', |
| 86 | + rule: 'lib/rules/template-length.js', |
| 87 | + docs: 'docs/rule/template-length.md', |
| 88 | + tests: 'test/unit/rules/template-length-test.js', |
| 89 | + }, |
| 90 | + }, |
| 91 | + |
| 92 | + create(context) { |
| 93 | + const config = parseConfig(context.options[0]); |
| 94 | + |
| 95 | + if (!config.min && !config.max) { |
| 96 | + return {}; |
| 97 | + } |
| 98 | + |
| 99 | + return { |
| 100 | + 'GlimmerTemplate:exit'(node) { |
| 101 | + const length = node.loc.end.line - node.loc.start.line + 1; |
| 102 | + |
| 103 | + if (config.max && length > config.max) { |
| 104 | + context.report({ |
| 105 | + node, |
| 106 | + messageId: 'tooLong', |
| 107 | + data: { length, max: config.max }, |
| 108 | + }); |
| 109 | + } else if (config.min && length < config.min) { |
| 110 | + context.report({ |
| 111 | + node, |
| 112 | + messageId: 'tooShort', |
| 113 | + data: { length, min: config.min }, |
| 114 | + }); |
| 115 | + } |
| 116 | + }, |
| 117 | + }; |
| 118 | + }, |
| 119 | +}; |
0 commit comments