Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 'Symbol.toStringTag' into every publicly exported class (#3170)
- Loading branch information
1 parent
9a04b4c
commit 81ca778
Showing
11 changed files
with
132 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
'use strict'; | ||
|
||
module.exports = function requireToStringTag(context) { | ||
const sourceCode = context.getSourceCode(); | ||
|
||
return { | ||
'ExportNamedDeclaration > ClassDeclaration': (classNode) => { | ||
const properties = classNode.body.body; | ||
if (properties.some(isToStringTagProperty)) { | ||
return; | ||
} | ||
|
||
const jsDoc = context.getJSDocComment(classNode)?.value; | ||
// FIXME: use proper TSDoc parser instead of includes once we fix TSDoc comments | ||
if (jsDoc?.includes('@internal') === true) { | ||
return; | ||
} | ||
|
||
context.report({ | ||
node: classNode, | ||
message: | ||
'All classes in public API required to have [Symbol.toStringTag] method', | ||
}); | ||
}, | ||
}; | ||
|
||
function isToStringTagProperty(propertyNode) { | ||
if ( | ||
propertyNode.type !== 'MethodDefinition' || | ||
propertyNode.kind !== 'get' | ||
) { | ||
return false; | ||
} | ||
const keyText = sourceCode.getText(propertyNode.key); | ||
return keyText === 'Symbol.toStringTag'; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,6 +79,10 @@ export class Lexer { | |
} | ||
return token; | ||
} | ||
|
||
get [Symbol.toStringTag]() { | ||
return 'Lexer'; | ||
} | ||
} | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { expect } from 'chai'; | ||
import { describe, it } from 'mocha'; | ||
|
||
import { identityFunc } from '../../jsutils/identityFunc'; | ||
|
||
import { parse } from '../../language/parser'; | ||
|
||
import { GraphQLSchema } from '../../type/schema'; | ||
|
||
import { TypeInfo } from '../../utilities/TypeInfo'; | ||
|
||
import { | ||
ASTValidationContext, | ||
SDLValidationContext, | ||
ValidationContext, | ||
} from '../ValidationContext'; | ||
|
||
describe('ValidationContext', () => { | ||
it('can be Object.toStringified', () => { | ||
const schema = new GraphQLSchema({}); | ||
const typeInfo = new TypeInfo(schema); | ||
const ast = parse('{ foo }'); | ||
const onError = identityFunc; | ||
|
||
const astContext = new ASTValidationContext(ast, onError); | ||
expect(Object.prototype.toString.call(astContext)).to.equal( | ||
'[object ASTValidationContext]', | ||
); | ||
|
||
const sdlContext = new SDLValidationContext(ast, schema, onError); | ||
expect(Object.prototype.toString.call(sdlContext)).to.equal( | ||
'[object SDLValidationContext]', | ||
); | ||
|
||
const context = new ValidationContext(schema, ast, typeInfo, onError); | ||
expect(Object.prototype.toString.call(context)).to.equal( | ||
'[object ValidationContext]', | ||
); | ||
}); | ||
}); |