Skip to content

Commit

Permalink
Add 'Symbol.toStringTag' into every exported class
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Jun 11, 2021
1 parent 9a04b4c commit 2987b6f
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .eslintrc.yml
Expand Up @@ -22,6 +22,7 @@ rules:

internal-rules/only-ascii: error
internal-rules/no-dir-import: error
internal-rules/require-to-string-tag: off

##############################################################################
# `eslint-plugin-istanbul` rule list based on `v0.1.2`
Expand Down Expand Up @@ -610,8 +611,12 @@ overrides:
'@typescript-eslint/space-before-function-paren': off
'@typescript-eslint/space-infix-ops': off
'@typescript-eslint/type-annotation-spacing': off
- files: 'src/**'
rules:
internal-rules/require-to-string-tag: error
- files: 'src/**/__*__/**'
rules:
internal-rules/require-to-string-tag: off
node/no-unpublished-import: [error, { allowModules: ['chai', 'mocha'] }]
import/no-restricted-paths: off
import/no-extraneous-dependencies: [error, { devDependencies: true }]
Expand Down
2 changes: 2 additions & 0 deletions resources/eslint-internal-rules/index.js
Expand Up @@ -2,10 +2,12 @@

const onlyASCII = require('./only-ascii.js');
const noDirImport = require('./no-dir-import.js');
const requireToStringTag = require('./require-to-string-tag.js');

module.exports = {
rules: {
'only-ascii': onlyASCII,
'no-dir-import': noDirImport,
'require-to-string-tag': requireToStringTag,
},
};
25 changes: 25 additions & 0 deletions resources/eslint-internal-rules/require-to-string-tag.js
@@ -0,0 +1,25 @@
'use strict';

module.exports = function requireToStringTag(context) {
const sourceCode = context.getSourceCode();

return {
'ExportNamedDeclaration > ClassDeclaration': (classNode) => {
const hasToStringTag = classNode.body.body.some((property) => {
if (property.type !== 'MethodDefinition' || property.kind !== 'get') {
return false;
}
const keyText = sourceCode.getText(property.key);
return keyText === 'Symbol.toStringTag';
});

if (!hasToStringTag) {
context.report({
node: classNode,
message:
'All exported classes required to have [Symbol.toStringTag] method',
});
}
},
};
};
8 changes: 8 additions & 0 deletions src/language/ast.ts
Expand Up @@ -42,6 +42,10 @@ export class Location {
toJSON(): { start: number; end: number } {
return { start: this.start, end: this.end };
}

get [Symbol.toStringTag]() {
return 'Location';
}
}

/**
Expand Down Expand Up @@ -121,6 +125,10 @@ export class Token {
column: this.column,
};
}

get [Symbol.toStringTag]() {
return 'Token';
}
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/language/lexer.ts
Expand Up @@ -79,6 +79,10 @@ export class Lexer {
}
return token;
}

get [Symbol.toStringTag]() {
return 'Lexer';
}
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/language/parser.ts
Expand Up @@ -1520,6 +1520,10 @@ export class Parser {
} while (this.expectOptionalToken(delimiterKind));
return nodes;
}

get [Symbol.toStringTag]() {
return 'Parser';
}
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/utilities/TypeInfo.ts
Expand Up @@ -292,6 +292,10 @@ export class TypeInfo {
break;
}
}

get [Symbol.toStringTag]() {
return 'TypeInfo';
}
}

type GetFieldDefFn = (
Expand Down
12 changes: 12 additions & 0 deletions src/validation/ValidationContext.ts
Expand Up @@ -131,6 +131,10 @@ export class ASTValidationContext {
}
return fragments;
}

get [Symbol.toStringTag]() {
return 'ASTValidationContext';
}
}

export type ASTValidationRule = (context: ASTValidationContext) => ASTVisitor;
Expand All @@ -150,6 +154,10 @@ export class SDLValidationContext extends ASTValidationContext {
getSchema(): Maybe<GraphQLSchema> {
return this._schema;
}

get [Symbol.toStringTag]() {
return 'SDLValidationContext';
}
}

export type SDLValidationRule = (context: SDLValidationContext) => ASTVisitor;
Expand Down Expand Up @@ -253,6 +261,10 @@ export class ValidationContext extends ASTValidationContext {
getEnumValue(): Maybe<GraphQLEnumValue> {
return this._typeInfo.getEnumValue();
}

get [Symbol.toStringTag]() {
return 'ValidationContext';
}
}

export type ValidationRule = (context: ValidationContext) => ASTVisitor;

0 comments on commit 2987b6f

Please sign in to comment.