Skip to content

fix(eslint-plugin): ensure consistent TSMappedType AST shape #11086

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions packages/typescript-estree/src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2870,14 +2870,14 @@ export class Converter {
constraint: this.convertChild(node.typeParameter.constraint),
key: this.convertChild(node.typeParameter.name),
nameType: this.convertChild(node.nameType) ?? null,
optional:
node.questionToken &&
(node.questionToken.kind === SyntaxKind.QuestionToken ||
getTextForTokenKind(node.questionToken.kind)),
readonly:
node.readonlyToken &&
(node.readonlyToken.kind === SyntaxKind.ReadonlyKeyword ||
getTextForTokenKind(node.readonlyToken.kind)),
optional: node.questionToken
? node.questionToken.kind === SyntaxKind.QuestionToken ||
getTextForTokenKind(node.questionToken.kind)
: false,
readonly: node.readonlyToken
? node.readonlyToken.kind === SyntaxKind.ReadonlyKeyword ||
getTextForTokenKind(node.readonlyToken.kind)
: undefined,
typeAnnotation: node.type && this.convertChild(node.type),
},
'typeParameter',
Expand Down
74 changes: 74 additions & 0 deletions packages/typescript-estree/tests/lib/convert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,5 +449,79 @@ describe('convert', () => {
expect(tsMappedType.typeParameter).toBeUndefined();
expect(Object.keys(tsMappedType)).toContain('typeParameter');
});

describe('TSMappedType AST shape consistency', () => {
const getMappedTypeFromCode = (code: string): TSESTree.TSMappedType => {
const ast = convertCode(code);
const instance = new Converter(ast);
const program = instance.convertProgram();
const varDec = program.body[0] as TSESTree.VariableDeclaration;
const typeAnnotation = varDec.declarations[0]?.id?.typeAnnotation;
if (!typeAnnotation) {
throw new Error('Type annotation is undefined');
}
return typeAnnotation.typeAnnotation as TSESTree.TSMappedType;
};

it('should have optional=false when no optional modifier is present', () => {
const mappedType = getMappedTypeFromCode('var a: { [k in any]: any };');

expect(mappedType).toHaveProperty('optional', false);
});

it('should have optional=true when optional modifier is present', () => {
const mappedType = getMappedTypeFromCode(
'var b: { [k in any]?: any };',
);

expect(mappedType).toHaveProperty('optional', true);
});

it('should have readonly=undefined when no readonly modifier is present', () => {
const mappedType = getMappedTypeFromCode('var a: { [k in any]: any };');

expect(mappedType).toHaveProperty('readonly', undefined);
});

it('should have readonly=true when readonly modifier is present', () => {
const mappedType = getMappedTypeFromCode(
'var c: { readonly [k in any]: any };',
);

expect(mappedType).toHaveProperty('readonly', true);
});

it('should have readonly="+" when +readonly modifier is present', () => {
const mappedType = getMappedTypeFromCode(
'var d: { +readonly [k in any]: any };',
);

expect(mappedType).toHaveProperty('readonly', '+');
});

it('should have readonly="-" when -readonly modifier is present', () => {
const mappedType = getMappedTypeFromCode(
'var e: { -readonly [k in any]: any };',
);

expect(mappedType).toHaveProperty('readonly', '-');
});

it('should have optional="+" when +? modifier is present', () => {
const mappedType = getMappedTypeFromCode(
'var f: { [k in any]+?: any };',
);

expect(mappedType).toHaveProperty('optional', '+');
});

it('should have optional="-" when -? modifier is present', () => {
const mappedType = getMappedTypeFromCode(
'var g: { [k in any]-?: any };',
);

expect(mappedType).toHaveProperty('optional', '-');
});
});
});
});