|
| 1 | +import type { AST } from 'svelte-eslint-parser'; |
| 2 | +import { createRule } from '../utils'; |
| 3 | + |
| 4 | +export default createRule('no-undefined-print', { |
| 5 | + meta: { |
| 6 | + docs: { |
| 7 | + description: 'Disallow from printing `undefined`', |
| 8 | + category: 'Possible Errors', |
| 9 | + recommended: true |
| 10 | + }, |
| 11 | + schema: [], |
| 12 | + messages: { |
| 13 | + unexpected: 'Unexpected `undefined`.' |
| 14 | + }, |
| 15 | + type: 'problem' |
| 16 | + }, |
| 17 | + create(context) { |
| 18 | + return { |
| 19 | + 'SvelteMustacheTag[kind=text]'(node: AST.SvelteMustacheTag) { |
| 20 | + if (node.expression.type === 'Identifier' && node.expression.name === 'undefined') { |
| 21 | + context.report({ |
| 22 | + node, |
| 23 | + messageId: 'unexpected' |
| 24 | + }); |
| 25 | + } |
| 26 | + |
| 27 | + if (node.expression.type === 'LogicalExpression' && node.expression.operator === '||') { |
| 28 | + const left = node.expression.left; |
| 29 | + const right = node.expression.right; |
| 30 | + |
| 31 | + if (left.type === 'Identifier' && right.type === 'Literal' && right.value === undefined) { |
| 32 | + context.report({ |
| 33 | + node, |
| 34 | + messageId: 'unexpected' |
| 35 | + }); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + if (node.expression.type === 'LogicalExpression' && node.expression.operator === '??') { |
| 40 | + const left = node.expression.left; |
| 41 | + const right = node.expression.right; |
| 42 | + |
| 43 | + if (left.type === 'Identifier' && right.type === 'Literal' && right.value === undefined) { |
| 44 | + context.report({ |
| 45 | + node, |
| 46 | + messageId: 'unexpected' |
| 47 | + }); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + if (node.expression.type === 'ConditionalExpression') { |
| 52 | + const consequent = node.expression.consequent; |
| 53 | + const alternate = node.expression.alternate; |
| 54 | + |
| 55 | + if ( |
| 56 | + (consequent.type === 'Literal' && consequent.value === undefined) || |
| 57 | + (alternate.type === 'Literal' && alternate.value === undefined) |
| 58 | + ) { |
| 59 | + context.report({ |
| 60 | + node, |
| 61 | + messageId: 'unexpected' |
| 62 | + }); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + }; |
| 67 | + } |
| 68 | +}); |
0 commit comments