Skip to content

Commit 33ec2a2

Browse files
committed
chore: migrate code base to TypeScript [ci skip]
1 parent fd3e746 commit 33ec2a2

File tree

11 files changed

+521
-871
lines changed

11 files changed

+521
-871
lines changed

.eslintrc.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,39 @@
11
module.exports = {
22
root: true,
3+
settings: {
4+
'import/resolver': {
5+
typescript: {
6+
alwaysTryTypes: true,
7+
},
8+
},
9+
},
310
extends: [
411
'plugin:jest/recommended',
512
'1stg/react',
613
'plugin:@rxts/mdx/recommended',
714
],
15+
rules: {
16+
'@typescript-eslint/no-explicit-any': 0,
17+
'@typescript-eslint/array-type': [
18+
0,
19+
{
20+
default: 'array-simple',
21+
},
22+
],
23+
},
24+
overrides: [
25+
{
26+
files: ['*.ts', '*.tsx'],
27+
rules: {
28+
'react/prop-types': 0,
29+
},
30+
},
31+
{
32+
files: ['*.d.ts'],
33+
rules: {
34+
'import/order': 0,
35+
'import/no-unresolved': 0,
36+
},
37+
},
38+
],
839
}

config/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
// eslint-disable-next-line @typescript-eslint/no-var-requires
12
const path = require('path')
23

3-
require('@babel/register')
4+
require('ts-node').register({
5+
project: path.resolve('src/tsconfig.json'),
6+
transpileOnly: true,
7+
})
48

59
module.exports = require(path.resolve('src'))

package.json

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,38 @@
1111
],
1212
"scripts": {
1313
"prepublishOnly": "yarn build",
14-
"build": "babel src -d dist --copy-files",
14+
"build": "tsc -P src",
1515
"test": "jest",
16-
"lint": "eslint . --ext js,mdx"
16+
"lint": "eslint . --ext ts,mdx"
1717
},
1818
"peerDependencies": {
1919
"eslint": ">=4.7.0"
2020
},
2121
"dependencies": {
22-
"@mdx-js/mdx": "^1.1.0"
22+
"@mdx-js/mdx": "^1.1.0",
23+
"eslint-utils": "^1.4.0",
24+
"eslint-visitor-keys": "^1.0.0",
25+
"remark-mdx": "^1.1.0",
26+
"remark-parse": "^7.0.0",
27+
"remark-stringify": "^7.0.1",
28+
"unified": "^8.3.2"
2329
},
2430
"devDependencies": {
25-
"@babel/cli": "^7.5.5",
26-
"@babel/core": "^7.5.5",
27-
"@babel/preset-env": "^7.5.5",
28-
"@babel/register": "^7.5.5",
2931
"@rxts/eslint-plugin-mdx": "file:config",
32+
"@types/eslint": "^4.16.6",
33+
"@types/eslint-visitor-keys": "^1.0.0",
34+
"@types/estree": "^0.0.39",
35+
"@types/node": "^12.6.8",
36+
"@types/unist": "^2.0.3",
3037
"eslint": "^6.1.0",
31-
"eslint-config-1stg": "^4.2.3",
38+
"eslint-config-1stg": "~5.4.1",
3239
"eslint-plugin-jest": "^22.14.0",
3340
"husky": "^3.0.1",
3441
"jest": "^24.8.0",
3542
"prettier": "1.18.2",
3643
"prettier-config-1stg": "^0.1.0",
37-
"react": "^16.8.6"
44+
"react": "^16.8.6",
45+
"ts-node": "^8.3.0",
46+
"typescript": "^3.5.3"
3847
}
3948
}

shim.d.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
declare module 'eslint-utils' {}
2+
3+
declare module 'espree' {
4+
import * as estree from 'estree'
5+
6+
// The version of espree that's loaded, like "v3.4.0".
7+
export const version: string
8+
9+
// Parse the given text as javascript into an abstract syntax tree.
10+
export function parse(text: string, opts?: Options): estree.Program
11+
12+
// Walk the given text and produce an array of tokens.
13+
export function tokenize(text: string, opts?: Options): Token[]
14+
15+
interface Token {
16+
type: string
17+
value: string
18+
start: number
19+
end: number
20+
}
21+
22+
// A map from node type to the properties on that type that contain more nodes.
23+
export const VisitorKeys: { [kind: string]: string[] }
24+
25+
const node: estree.Node
26+
// A map of node types to themselves.
27+
export const Syntax: { [type: string]: typeof node.type }
28+
29+
// Options for parsing.
30+
interface Options {
31+
// attach range information to each node
32+
range?: boolean
33+
34+
// attach line/column location information to each node
35+
loc?: boolean
36+
37+
// create a top-level comments array containing all comments
38+
comment?: boolean
39+
40+
// attach comments to the closest relevant node as leadingComments and
41+
// trailingComments
42+
attachComment?: boolean
43+
44+
// create a top-level tokens array containing all tokens
45+
tokens?: true
46+
47+
// set to 3, 5 (default), 6, 7, or 8 to specify the version of ECMAScript
48+
// syntax you want to use.
49+
// You can also set to 2015 (same as 6), 2016 (same as 7), or 2017 (same as 8)
50+
// to use the year-based naming.
51+
ecmaVersion?: number
52+
53+
// specify which type of script you're parsing (script or module, default is script)
54+
sourceType?: 'script' | 'module'
55+
56+
// specify additional language features
57+
ecmaFeatures?: {
58+
// enable JSX parsing
59+
jsx?: boolean
60+
61+
// enable return in global scope
62+
globalReturn?: boolean
63+
64+
// enable implied strict mode (if ecmaVersion >= 5)
65+
impliedStrict?: boolean
66+
67+
// allow experimental object rest/spread
68+
experimentalObjectRestSpread?: boolean
69+
}
70+
}
71+
}
72+
73+
declare module 'remark-mdx' {
74+
import * as unified from 'unified'
75+
76+
const mdx: unified.Attacher
77+
78+
export = mdx
79+
}

src/index.js

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import path from 'path'
2+
3+
export { parseForESLint } from './parser'
4+
5+
export const configs = {
6+
recommended: {
7+
overrides: [
8+
{
9+
files: '*.mdx',
10+
parser: path.resolve(__dirname, 'parser'),
11+
plugins: ['@rxts/mdx'],
12+
rules: {
13+
'react/react-in-jsx-scope': 0,
14+
},
15+
},
16+
],
17+
},
18+
}

src/parser.ts

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// eslint-disable-next-line @typescript-eslint/no-triple-slash-reference
2+
/// <reference path="../shim.d.ts" />
3+
4+
import { parse as esParse } from 'espree'
5+
import remarkMdx from 'remark-mdx'
6+
import remarkParse from 'remark-parse'
7+
import remarkStringify from 'remark-stringify'
8+
import unified from 'unified'
9+
10+
import { Position, Parent } from 'unist'
11+
import { AST, Linter } from 'eslint'
12+
import { Statement, ModuleDeclaration } from 'estree'
13+
14+
const transNodePos = (position: Position) => {
15+
const start = position.start.offset
16+
const end = position.end.offset
17+
return {
18+
range: [start, end] as AST.Range,
19+
loc: {
20+
...position,
21+
},
22+
start,
23+
end,
24+
}
25+
}
26+
27+
const getRawText = (code: string, position: Position) =>
28+
code.slice(position.start.offset, position.end.offset)
29+
30+
export const parseForESLint = (
31+
code: string,
32+
options: Linter.ParserOptions,
33+
): Linter.ESLintParseResult => {
34+
const root = unified()
35+
.use<any>(remarkParse)
36+
.use<any>(remarkStringify)
37+
.use(remarkMdx)
38+
.parse(code) as Parent
39+
40+
const tokens: AST.Token[] = []
41+
42+
return {
43+
ast: {
44+
...transNodePos(root.position),
45+
comments: [],
46+
body: root.children.reduce<Array<Statement | ModuleDeclaration>>(
47+
(nodes, { position, type }) => {
48+
if (!['export', 'import', 'jsx'].includes(type)) {
49+
return nodes
50+
}
51+
52+
const rawText = getRawText(code, position)
53+
54+
let node = {
55+
...transNodePos(position),
56+
value: rawText,
57+
}
58+
59+
const { tokens: esTokens, ...AST } = esParse(
60+
rawText,
61+
options,
62+
) as AST.Program
63+
64+
const offset = node.start - AST.range[0]
65+
66+
node = {
67+
...AST,
68+
...node,
69+
}
70+
71+
nodes.push(node as any)
72+
tokens.push(
73+
...esTokens.map(token => {
74+
const {
75+
loc: { start: tokenStart, end: tokenEnd },
76+
} = token
77+
const start = token.range[0] + offset
78+
const end = token.range[1] + offset
79+
const startLine = node.loc.start.line + tokenStart.line - 1
80+
const startColumn = node.loc.start.column + tokenStart.column - 1
81+
return {
82+
...token,
83+
start,
84+
end,
85+
range: [start, end],
86+
loc: {
87+
start: {
88+
line: startLine,
89+
column: startColumn,
90+
},
91+
end: {
92+
line: startLine + tokenEnd.line - 1,
93+
column: startLine + tokenEnd.column - 1,
94+
},
95+
},
96+
} as AST.Token
97+
}),
98+
)
99+
100+
return nodes
101+
},
102+
[],
103+
),
104+
type: 'Program',
105+
sourceType: 'module',
106+
tokens,
107+
},
108+
scopeManager: null,
109+
visitorKeys: null,
110+
}
111+
}

src/tsconfig.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "../tsconfig.json",
3+
"compilerOptions": {
4+
"declaration": true,
5+
"module": "commonjs",
6+
"outDir": "../dist"
7+
}
8+
}

test/test.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Test } from './test';
1+
import { Test } from './test'
22

33
# Hello
44

tsconfig.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"compilerOptions": {
3+
"allowSyntheticDefaultImports": true,
4+
"baseUrl": ".",
5+
"esModuleInterop": true,
6+
"jsx": "preserve",
7+
"lib": ["esnext", "dom"],
8+
"module": "esnext",
9+
"moduleResolution": "node",
10+
"sourceMap": true,
11+
"strict": true,
12+
"strictNullChecks": false,
13+
"target": "es5"
14+
}
15+
}

0 commit comments

Comments
 (0)