Skip to content

Commit 2987b6f

Browse files
committed
Add 'Symbol.toStringTag' into every exported class
1 parent 9a04b4c commit 2987b6f

File tree

8 files changed

+64
-0
lines changed

8 files changed

+64
-0
lines changed

.eslintrc.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ rules:
2222

2323
internal-rules/only-ascii: error
2424
internal-rules/no-dir-import: error
25+
internal-rules/require-to-string-tag: off
2526

2627
##############################################################################
2728
# `eslint-plugin-istanbul` rule list based on `v0.1.2`
@@ -610,8 +611,12 @@ overrides:
610611
'@typescript-eslint/space-before-function-paren': off
611612
'@typescript-eslint/space-infix-ops': off
612613
'@typescript-eslint/type-annotation-spacing': off
614+
- files: 'src/**'
615+
rules:
616+
internal-rules/require-to-string-tag: error
613617
- files: 'src/**/__*__/**'
614618
rules:
619+
internal-rules/require-to-string-tag: off
615620
node/no-unpublished-import: [error, { allowModules: ['chai', 'mocha'] }]
616621
import/no-restricted-paths: off
617622
import/no-extraneous-dependencies: [error, { devDependencies: true }]

resources/eslint-internal-rules/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

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

67
module.exports = {
78
rules: {
89
'only-ascii': onlyASCII,
910
'no-dir-import': noDirImport,
11+
'require-to-string-tag': requireToStringTag,
1012
},
1113
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
3+
module.exports = function requireToStringTag(context) {
4+
const sourceCode = context.getSourceCode();
5+
6+
return {
7+
'ExportNamedDeclaration > ClassDeclaration': (classNode) => {
8+
const hasToStringTag = classNode.body.body.some((property) => {
9+
if (property.type !== 'MethodDefinition' || property.kind !== 'get') {
10+
return false;
11+
}
12+
const keyText = sourceCode.getText(property.key);
13+
return keyText === 'Symbol.toStringTag';
14+
});
15+
16+
if (!hasToStringTag) {
17+
context.report({
18+
node: classNode,
19+
message:
20+
'All exported classes required to have [Symbol.toStringTag] method',
21+
});
22+
}
23+
},
24+
};
25+
};

src/language/ast.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ export class Location {
4242
toJSON(): { start: number; end: number } {
4343
return { start: this.start, end: this.end };
4444
}
45+
46+
get [Symbol.toStringTag]() {
47+
return 'Location';
48+
}
4549
}
4650

4751
/**
@@ -121,6 +125,10 @@ export class Token {
121125
column: this.column,
122126
};
123127
}
128+
129+
get [Symbol.toStringTag]() {
130+
return 'Token';
131+
}
124132
}
125133

126134
/**

src/language/lexer.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ export class Lexer {
7979
}
8080
return token;
8181
}
82+
83+
get [Symbol.toStringTag]() {
84+
return 'Lexer';
85+
}
8286
}
8387

8488
/**

src/language/parser.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,6 +1520,10 @@ export class Parser {
15201520
} while (this.expectOptionalToken(delimiterKind));
15211521
return nodes;
15221522
}
1523+
1524+
get [Symbol.toStringTag]() {
1525+
return 'Parser';
1526+
}
15231527
}
15241528

15251529
/**

src/utilities/TypeInfo.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,10 @@ export class TypeInfo {
292292
break;
293293
}
294294
}
295+
296+
get [Symbol.toStringTag]() {
297+
return 'TypeInfo';
298+
}
295299
}
296300

297301
type GetFieldDefFn = (

src/validation/ValidationContext.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ export class ASTValidationContext {
131131
}
132132
return fragments;
133133
}
134+
135+
get [Symbol.toStringTag]() {
136+
return 'ASTValidationContext';
137+
}
134138
}
135139

136140
export type ASTValidationRule = (context: ASTValidationContext) => ASTVisitor;
@@ -150,6 +154,10 @@ export class SDLValidationContext extends ASTValidationContext {
150154
getSchema(): Maybe<GraphQLSchema> {
151155
return this._schema;
152156
}
157+
158+
get [Symbol.toStringTag]() {
159+
return 'SDLValidationContext';
160+
}
153161
}
154162

155163
export type SDLValidationRule = (context: SDLValidationContext) => ASTVisitor;
@@ -253,6 +261,10 @@ export class ValidationContext extends ASTValidationContext {
253261
getEnumValue(): Maybe<GraphQLEnumValue> {
254262
return this._typeInfo.getEnumValue();
255263
}
264+
265+
get [Symbol.toStringTag]() {
266+
return 'ValidationContext';
267+
}
256268
}
257269

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

0 commit comments

Comments
 (0)