ā¬ ļø Back to Table of Contents
š triple-slash-reference¶
š Analysis Summary¶
| Metric | Count |
|---|---|
| š§ Functions | 2 |
| š¦ Imports | 4 |
| š Type Aliases | 2 |
š Table of Contents¶
š ļø File Location:¶
š packages/eslint-plugin/src/rules/triple-slash-reference.ts
š¤ Default Export¶
| Property | Value |
|---|---|
name |
'triple-slash-reference' |
meta.type |
'suggestion' |
meta.docs.description |
'Disallow certain triple slash directives in favor of ES6-style import declarations' |
meta.docs.recommended |
'recommended' |
meta.messages.tripleSlashReference |
'Do not use a triple slash reference for {{module}}, use import style instead.' |
meta.schema |
[ { type: 'object', additionalProperties: false, properties: { lib: { type: 'string', description: 'What to enforce f... |
defaultOptions |
[ { lib: 'always', path: 'never', types: 'prefer-import', }, ] |
Entry point: create ā documented under Functions.
š¦ Imports¶
| Name | Source |
|---|---|
TSESTree |
@typescript-eslint/utils |
AST_NODE_TYPES |
@typescript-eslint/utils |
AST_TOKEN_TYPES |
@typescript-eslint/utils |
createRule |
../util |
Functions¶
create(context: any, [{ lib, path, types }]: any): { ImportDeclaration(node: any): void; Program(node: any): v⦶
Parameters:
contextany[{ lib, path, types }]any
Returns: { ImportDeclaration(node: any): void; Program(node: any): void; TSImportEqualsDeclaration(node: any): void; }
Calls:
references.forEachcontext.reporthasMatchingReferencecontext.sourceCode.getCommentsBeforecommentsBefore.forEachreferenceRegExp.execreferences.push
Code
create(context, [{ lib, path, types }]) {
let programNode: TSESTree.Node | undefined;
const references: {
comment: TSESTree.Comment;
importName: string;
}[] = [];
function hasMatchingReference(source: TSESTree.Literal): void {
references.forEach(reference => {
if (reference.importName === source.value) {
context.report({
node: reference.comment,
messageId: 'tripleSlashReference',
data: {
module: reference.importName,
},
});
}
});
}
return {
ImportDeclaration(node): void {
if (programNode) {
hasMatchingReference(node.source);
}
},
Program(node): void {
if (lib === 'always' && path === 'always' && types === 'always') {
return;
}
programNode = node;
const referenceRegExp =
/^\/\s*<reference\s*(types|path|lib)\s*=\s*["|'](.*)["|']/;
const commentsBefore =
context.sourceCode.getCommentsBefore(programNode);
commentsBefore.forEach(comment => {
if (comment.type !== AST_TOKEN_TYPES.Line) {
return;
}
const referenceResult = referenceRegExp.exec(comment.value);
if (referenceResult) {
if (
(referenceResult[1] === 'types' && types === 'never') ||
(referenceResult[1] === 'path' && path === 'never') ||
(referenceResult[1] === 'lib' && lib === 'never')
) {
context.report({
node: comment,
messageId: 'tripleSlashReference',
data: {
module: referenceResult[2],
},
});
return;
}
if (referenceResult[1] === 'types' && types === 'prefer-import') {
references.push({ comment, importName: referenceResult[2] });
}
}
});
},
TSImportEqualsDeclaration(node): void {
if (programNode) {
const reference = node.moduleReference;
if (reference.type === AST_NODE_TYPES.TSExternalModuleReference) {
hasMatchingReference(reference.expression);
}
}
},
};
}
Internal helpers¶
Declared inside another function in this file.
hasMatchingReference(source: TSESTree.Literal): void¶
Parameters:
sourceTSESTree.Literal
Returns: void
Calls:
references.forEachcontext.report
Code
Type Aliases¶
Options¶
type Options = [
{
lib?: 'always' | 'never';
path?: 'always' | 'never';
types?: 'always' | 'never' | 'prefer-import';
},
];
MessageIds¶
Generated by Syntax Scribe