|
| 1 | +import * as util from '../util'; |
| 2 | +import { |
| 3 | + Literal, |
| 4 | + Node, |
| 5 | + TSExternalModuleReference, |
| 6 | +} from '@typescript-eslint/typescript-estree/dist/ts-estree/ts-estree'; |
| 7 | +import { TSESTree } from '@typescript-eslint/typescript-estree'; |
| 8 | + |
| 9 | +type Options = [ |
| 10 | + { |
| 11 | + lib?: 'always' | 'never'; |
| 12 | + path?: 'always' | 'never'; |
| 13 | + types?: 'always' | 'never' | 'prefer-import'; |
| 14 | + } |
| 15 | +]; |
| 16 | +type MessageIds = 'tripleSlashReference'; |
| 17 | + |
| 18 | +export default util.createRule<Options, MessageIds>({ |
| 19 | + name: 'triple-slash-reference', |
| 20 | + meta: { |
| 21 | + type: 'suggestion', |
| 22 | + docs: { |
| 23 | + description: |
| 24 | + 'Sets preference level for triple slash directives versus ES6-style import declarations', |
| 25 | + category: 'Best Practices', |
| 26 | + recommended: false, |
| 27 | + }, |
| 28 | + messages: { |
| 29 | + tripleSlashReference: |
| 30 | + 'Do not use a triple slash reference for {{module}}, use `import` style instead.', |
| 31 | + }, |
| 32 | + schema: [ |
| 33 | + { |
| 34 | + type: 'object', |
| 35 | + properties: { |
| 36 | + lib: { |
| 37 | + enum: ['always', 'never'], |
| 38 | + }, |
| 39 | + path: { |
| 40 | + enum: ['always', 'never'], |
| 41 | + }, |
| 42 | + types: { |
| 43 | + enum: ['always', 'never', 'prefer-import'], |
| 44 | + }, |
| 45 | + }, |
| 46 | + additionalProperties: false, |
| 47 | + }, |
| 48 | + ], |
| 49 | + }, |
| 50 | + defaultOptions: [ |
| 51 | + { |
| 52 | + lib: 'always', |
| 53 | + path: 'never', |
| 54 | + types: 'prefer-import', |
| 55 | + }, |
| 56 | + ], |
| 57 | + create(context, [{ lib, path, types }]) { |
| 58 | + let programNode: Node; |
| 59 | + const sourceCode = context.getSourceCode(); |
| 60 | + const references: ({ |
| 61 | + comment: TSESTree.Comment; |
| 62 | + importName: string; |
| 63 | + })[] = []; |
| 64 | + |
| 65 | + function hasMatchingReference(source: Literal) { |
| 66 | + references.forEach(reference => { |
| 67 | + if (reference.importName === source.value) { |
| 68 | + context.report({ |
| 69 | + node: reference.comment, |
| 70 | + messageId: 'tripleSlashReference', |
| 71 | + data: { |
| 72 | + module: reference.importName, |
| 73 | + }, |
| 74 | + }); |
| 75 | + } |
| 76 | + }); |
| 77 | + } |
| 78 | + return { |
| 79 | + ImportDeclaration(node) { |
| 80 | + if (programNode) { |
| 81 | + const source = node.source as Literal; |
| 82 | + hasMatchingReference(source); |
| 83 | + } |
| 84 | + }, |
| 85 | + TSImportEqualsDeclaration(node) { |
| 86 | + if (programNode) { |
| 87 | + const source = (node.moduleReference as TSExternalModuleReference) |
| 88 | + .expression as Literal; |
| 89 | + hasMatchingReference(source); |
| 90 | + } |
| 91 | + }, |
| 92 | + Program(node) { |
| 93 | + if (lib === 'always' && path === 'always' && types == 'always') { |
| 94 | + return; |
| 95 | + } |
| 96 | + programNode = node; |
| 97 | + const referenceRegExp = /^\/\s*<reference\s*(types|path|lib)\s*=\s*["|'](.*)["|']/; |
| 98 | + const commentsBefore = sourceCode.getCommentsBefore(programNode); |
| 99 | + |
| 100 | + commentsBefore.forEach(comment => { |
| 101 | + if (comment.type !== 'Line') { |
| 102 | + return; |
| 103 | + } |
| 104 | + const referenceResult = referenceRegExp.exec(comment.value); |
| 105 | + |
| 106 | + if (referenceResult) { |
| 107 | + if ( |
| 108 | + (referenceResult[1] === 'types' && types === 'never') || |
| 109 | + (referenceResult[1] === 'path' && path === 'never') || |
| 110 | + (referenceResult[1] === 'lib' && lib === 'never') |
| 111 | + ) { |
| 112 | + context.report({ |
| 113 | + node: comment, |
| 114 | + messageId: 'tripleSlashReference', |
| 115 | + data: { |
| 116 | + module: referenceResult[2], |
| 117 | + }, |
| 118 | + }); |
| 119 | + return; |
| 120 | + } |
| 121 | + if (referenceResult[1] === 'types' && types === 'prefer-import') { |
| 122 | + references.push({ comment, importName: referenceResult[2] }); |
| 123 | + } |
| 124 | + } |
| 125 | + }); |
| 126 | + }, |
| 127 | + }; |
| 128 | + }, |
| 129 | +}); |
0 commit comments