Skip to content

Commit 1026fd7

Browse files
Merge pull request #21 from NullVoxPopuli/fix-for-all-declaration-types
Fix for all declaration typse, particularly, non-declare
2 parents 975f400 + 1d7528f commit 1026fd7

File tree

5 files changed

+108
-10
lines changed

5 files changed

+108
-10
lines changed

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
auto-install-peers=false

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"test": "vitest"
3939
},
4040
"dependencies": {
41+
"@babel/parser": "^7.28.3",
4142
"fs-extra": "^11.2.0",
4243
"globby": "^14.0.0",
4344
"jscodeshift": "^0.15.1"

pnpm-lock.yaml

Lines changed: 12 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/fixes/typescript.js

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,68 @@
1+
/**
2+
* See: original code for withParser('ts')
3+
*
4+
* https://github.com/facebook/jscodeshift/blob/e877ed287dcb3ef3ee8b20c53ec8b477ab564438/parser/ts.js
5+
*/
6+
import babylon from '@babel/parser';
17
import jscodeshift from 'jscodeshift';
28

3-
const j = jscodeshift.withParser('ts');
9+
function customTSParser() {
10+
/**
11+
* https://github.com/facebook/jscodeshift/blob/e877ed287dcb3ef3ee8b20c53ec8b477ab564438/parser/tsOptions.js
12+
*
13+
* @type {import('@babel/parser').ParserOptions}
14+
*/
15+
const options = {
16+
sourceType: 'module',
17+
allowImportExportEverywhere: true,
18+
allowReturnOutsideFunction: true,
19+
startLine: 1,
20+
tokens: true,
21+
plugins: [
22+
'asyncGenerators',
23+
'decoratorAutoAccessors',
24+
'bigInt',
25+
'classPrivateMethods',
26+
'classPrivateProperties',
27+
'classProperties',
28+
'decorators-legacy',
29+
'doExpressions',
30+
'dynamicImport',
31+
'exportDefaultFrom',
32+
// @ts-ignore -- doesn't exist? why does jscodeshift have it?
33+
'exportExtensions',
34+
'exportNamespaceFrom',
35+
'functionBind',
36+
'functionSent',
37+
'importAttributes',
38+
'importMeta',
39+
'nullishCoalescingOperator',
40+
'numericSeparator',
41+
'objectRestSpread',
42+
'optionalCatchBinding',
43+
'optionalChaining',
44+
['pipelineOperator', { proposal: 'minimal' }],
45+
'throwExpressions',
46+
/**
47+
* Without dts: true, we don't properly parse all types of declaration files
48+
*
49+
* This is missing from the official tsOptions in jscodeshift
50+
*/
51+
['typescript', { dts: true }],
52+
],
53+
};
54+
55+
return {
56+
/**
57+
* @param {string} code
58+
*/
59+
parse(code) {
60+
return babylon.parse(code, options);
61+
},
62+
};
63+
}
64+
65+
const j = jscodeshift.withParser(customTSParser());
466

567
/**
668
* @param {string} contents

src/index.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,35 @@ describe('fixBadDeclarationOutput', () => {
5858
export declare const four: 'literal';"
5959
`);
6060
});
61+
62+
test('it works with non-declare .d.ts', async () => {
63+
const tmp = await mkdirp();
64+
65+
const a = path.join(tmp, 'a.d.ts');
66+
67+
await fs.writeFile(
68+
a,
69+
stripIndent`
70+
/// <reference types="@glint/whatever/module">
71+
/// <reference types="node_modules/@glint/whatever2/module">
72+
/// <reference types="xyz">
73+
74+
export const two: number;
75+
export const three: string;
76+
export const four: 'literal';
77+
`
78+
);
79+
80+
await fixBadDeclarationOutput(`${tmp}/**/*.d.ts`, [['TypeScript#56571', { types: 'all' }]], {
81+
log: true,
82+
});
83+
84+
const aContents = await read(a);
85+
86+
expect(aContents).toMatchInlineSnapshot(`
87+
"export const two: number;
88+
export const three: string;
89+
export const four: 'literal';"
90+
`);
91+
});
6192
});

0 commit comments

Comments
 (0)