jscodeshift removes 'override' keyword from ClassMethod when setting returnType
625dennis opened this issue · comments
Given an input:
class Bar extends Foo {
override foo();
}
and a transform:
import { Transform } from 'jscodeshift';
const transform: Transform = (file, api) => {
const jscodeshift = api.jscodeshift;
const root = jscodeshift.withParser('ts')(file.source);
root.find(j.ClassMethod).forEach(path => {
path.value.returnType = j.tsTypeAnnotation(j.tsTypeReference(j.identifier('string')));
});
return root.toSource({ parser: 'ts' });
};
export default transform;
This will output the class with typed ClassMethod definition but without override:
class Bar extends Foo {
foo(): string;
}
I expect override to be carried over. One thing I noticed was the jscodeshift
types and ast-types
types for methods do not include an override
boolean property, but during runtime ClassMethod
object will contain override: true
property if the method signature includes override
keyword.
I can't repro on ASTExplorer when using the TypeScript parser: https://astexplorer.net/#/gist/5874ee54ffac67d5b889eda5b82ee883/cd476bad72ae8c6f5164483f93172bd8699590af
ASTExplorer is using an older version of JSCodeshift though, so I wonder if this is a regression in a newer version?
Ah, good catch @ElonVolo! Now I can repro. https://astexplorer.net/#/gist/5874ee54ffac67d5b889eda5b82ee883/5bb310ff6c635a7963f2c340975c50612550c3d8
Looks like j.withParser('ts')
will use @babel/parser
with typescript
option as the parser https://github.com/facebook/jscodeshift/blob/514f8c3e4e2e2801713beff93a04f8f8a771fe96/parser/ts.js . https://astexplorer.net/#/gist/a5f747da23a3539448fb63ffe9f04c43/5be0fb3874deba008cd4251e2756b8adb9289bea
Results in same output, but this AST matches what I get in my debug env.
Recast doesn't include an override
flag in the printMethod
function https://github.com/benjamn/recast/blob/25351f6a84cb7c83e77717346487a788a3465204/lib/printer.ts#L2721