Skip to content

Commit

Permalink
fix: compatibility with typescript-eslint (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin authored Jun 15, 2020
1 parent 7edaa59 commit 28dc160
Show file tree
Hide file tree
Showing 16 changed files with 2,368 additions and 2,148 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ module.exports = {
root: true,
extends: ['@1stg'],
rules: {
'@typescript-eslint/naming-convention': 0,
'@typescript-eslint/no-unnecessary-condition': 0,
'@typescript-eslint/no-unsafe-assignment': 0,
'@typescript-eslint/unbound-method': 0, // See https://github.com/typescript-eslint/typescript-eslint/issues/636
},
}
29 changes: 12 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,27 @@
"type-coverage": "type-coverage --cache --detail --ignore-catch --ignore-files '**/*.d.ts' --strict"
},
"devDependencies": {
"@1stg/lib-config": "^0.5.1",
"@1stg/lib-config": "^0.5.3",
"@1stg/tslint-config": "^0.8.4",
"@types/eslint": "^6.8.0",
"@types/jest": "^25.2.1",
"@types/node": "^13.13.4",
"@types/react": "^16.9.34",
"@types/rebass": "^4.0.5",
"@types/eslint": "^7.2.0",
"@types/jest": "^26.0.0",
"@types/node": "^14.0.13",
"@types/react": "^16.9.36",
"@types/rebass": "^4.0.6",
"@types/unist": "^2.0.3",
"cosmiconfig": "^6.0.0",
"eslint-mdx": "link:packages/eslint-mdx/src",
"eslint-plugin-mdx": "link:packages/eslint-plugin-mdx/src",
"lerna": "^3.20.2",
"lerna": "^3.22.1",
"npm-run-all": "^4.1.5",
"react": "^16.13.1",
"ts-jest": "^25.4.0",
"ts-node": "^8.9.1",
"ts-jest": "^26.1.0",
"ts-node": "^8.10.2",
"tslint": "^6.1.2",
"type-coverage": "^2.4.2",
"type-coverage": "^2.7.2",
"typescript": "^3.9.5",
"yarn-deduplicate": "^2.0.0"
},
"resolutions": {
"@babel/core": "^7.9.6",
"@babel/preset-env": "^7.9.6",
"@types/estree": "^0.0.44",
"typescript": "^3.8.3"
},
"commitlint": {
"extends": [
"@1stg"
Expand All @@ -70,7 +65,7 @@
"coverageThreshold": {
"global": {
"branches": 100,
"functions": 100,
"functions": 95,
"lines": 100,
"statements": 100
}
Expand Down
6 changes: 3 additions & 3 deletions packages/eslint-mdx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
"eslint": ">=5.0.0"
},
"dependencies": {
"espree": "^6.2.1",
"remark-mdx": "^1.6.0",
"espree": "^7.1.0",
"remark-mdx": "^1.6.5",
"remark-parse": "^8.0.2",
"tslib": "^1.11.1",
"tslib": "^2.0.0",
"unified": "^9.0.0"
}
}
14 changes: 7 additions & 7 deletions packages/eslint-mdx/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const normalizeParser = (parser?: ParserOptions['parser']) => {
// try to load FALLBACK_PARSERS automatically
for (const fallback of FALLBACK_PARSERS) {
try {
// eslint-disable-next-line @typescript-eslint/no-require-imports,@typescript-eslint/no-var-requires
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
const fallbackParser: Linter.ParserModule = require(fallback)
/* istanbul ignore next */
const parserFn =
Expand All @@ -59,8 +59,7 @@ export const normalizeParser = (parser?: ParserOptions['parser']) => {
if (parserFn) {
parsers.unshift(parserFn)
}
break
} catch (e) {}
} catch {}
}

return parsers
Expand Down Expand Up @@ -105,10 +104,10 @@ export function restoreNodeLocation<T extends BaseNode>(
}

if (Array.isArray(value)) {
node[key as keyof T] = value.map(
child => restoreNodeLocation(child, startLine, offset),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
) as any
node[key as keyof T] = (value.map(child =>
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
restoreNodeLocation(child, startLine, offset),
) as unknown) as T[keyof T]
} else {
node[key as keyof T] = restoreNodeLocation(
value,
Expand Down Expand Up @@ -148,6 +147,7 @@ export const last = <T>(items: T[] | readonly T[]) =>
items && items[items.length - 1]

export const hasProperties = <T, P extends keyof T = keyof T>(
// eslint-disable-next-line @typescript-eslint/ban-types
obj: {},
properties: Arrayable<P>,
): obj is T => properties.every(property => property in obj)
11 changes: 8 additions & 3 deletions packages/eslint-mdx/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export const DEFAULT_PARSER_OPTIONS: ParserOptions = {
sourceType: 'module',
tokens: true,
filePath: '__placeholder__.mdx',
// required for @typescript-eslint/parser
// reference: https://github.com/typescript-eslint/typescript-eslint/pull/2028
loc: true,
range: true,
}

const JSX_WRAPPER_START = '<$>'
Expand Down Expand Up @@ -211,7 +215,7 @@ export class Parser {
break
} catch (e) {
if (!parseError) {
parseError = e
parseError = e as Error
}
}
}
Expand Down Expand Up @@ -261,8 +265,8 @@ export class Parser {
return node
}

const { expression } = program
.body[0] as import('estree').ExpressionStatement
const { expression } = (program
.body[0] as unknown) as import('@babel/types').ExpressionStatement

if (!isJsxNode(expression) || expression.children.length <= 1) {
return node
Expand Down Expand Up @@ -352,6 +356,7 @@ export class Parser {
// unfortunately, TS complains about incompatible signature
// @ts-ignore
...program[prop].map(item =>
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
restoreNodeLocation(item, startLine, offset),
),
),
Expand Down
8 changes: 4 additions & 4 deletions packages/eslint-plugin-mdx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@
"dependencies": {
"cosmiconfig": "^6.0.0",
"eslint-mdx": "^1.7.0",
"eslint-plugin-react": "^7.19.0",
"remark-mdx": "^1.6.0",
"eslint-plugin-react": "^7.20.0",
"remark-mdx": "^1.6.5",
"remark-parse": "^8.0.2",
"remark-stringify": "^8.0.0",
"tslib": "^1.11.1",
"tslib": "^2.0.0",
"unified": "^9.0.0",
"vfile": "^4.1.0"
"vfile": "^4.1.1"
},
"optionalDependencies": {
"rebass": "^4.0.7"
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin-mdx/src/configs/recommended.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ if (minorVersion >= OVERRIDES_AVAILABLE_VERSION) {
],
},
})
} catch (e) {}
} catch {}
Object.assign(recommended, {
overrides,
})
Expand Down
4 changes: 1 addition & 3 deletions packages/eslint-plugin-mdx/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import * as configs from './configs'

export { configs }
export * as configs from './configs'

export * from './configs/helper'
export * from './rules'
8 changes: 4 additions & 4 deletions packages/eslint-plugin-mdx/src/rules/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import unified, { Processor } from 'unified'

import { RemarkConfig } from './types'

export const requirePkg = (
export const requirePkg = <T>(
plugin: string,
prefix: string,
filePath?: string,
) => {
): T => {
if (filePath && /^\.\.?([/\\]|$)/.test(plugin)) {
plugin = path.resolve(path.dirname(filePath), plugin)
}
Expand All @@ -27,7 +27,7 @@ export const requirePkg = (
let error: Error
for (const pkg of packages) {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports, @typescript-eslint/no-unsafe-return
return require(pkg)
} catch (err) {
if (!error) {
Expand Down Expand Up @@ -62,7 +62,7 @@ export const getRemarkProcessor = (searchFrom: string) => {
// disable this rule automatically since we already have a parser option `extensions`
// eslint-disable-next-line node/no-extraneous-require
plugins.push([require.resolve('remark-lint-file-extension'), false])
} catch (e) {
} catch {
// just ignore if the package does not exist
}

Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-plugin-mdx/src/rules/no-jsx-html-comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ export const noJsxHtmlComments: Rule.RuleModule = {
jsxHtmlComments: 'html style comments are invalid in jsx: {{ origin }}',
},
fixable: 'code',
schema: [],
},
create(context) {
return {
ExpressionStatement(node: ExpressionStatementWithParent) {
const invalidNodes: Array<import('unist').Node> =
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
context.parserServices.JSXElementsWithHTMLComments

if (
Expand Down Expand Up @@ -59,4 +59,4 @@ export const noJsxHtmlComments: Rule.RuleModule = {
},
}
},
} as Rule.RuleModule
}
8 changes: 5 additions & 3 deletions packages/eslint-plugin-mdx/src/rules/no-unescaped-entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ const EXPRESSION = 'Literal, JSXText'
export const noUnescapedEntities: Rule.RuleModule = {
...reactNoUnescapedEntities,
create(context) {
const configuration = context.options[0] || {}
const entities: EscapeEntity[] = configuration.forbid || DEFAULTS
const configuration: {
forbid?: EscapeEntity[]
} = context.options[0] || {}
const entities = configuration.forbid || DEFAULTS
return {
// eslint-disable-next-line sonarjs/cognitive-complexity
[EXPRESSION](node: NodeWithParent) {
Expand All @@ -53,7 +55,7 @@ export const noUnescapedEntities: Rule.RuleModule = {
if (parent.parent.type === 'Program') {
break
} else {
parent = parent.parent
parent = (parent as NodeWithParent).parent
}
}

Expand Down
13 changes: 7 additions & 6 deletions packages/eslint-plugin-mdx/src/rules/remark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,22 @@ export const remark: Rule.RuleModule = {
remarkReport: '{{ source }}:{{ ruleId }} - {{ reason }}',
},
fixable: 'code',
schema: [],
},
create(context) {
const filename = context.getFilename()
const extname = path.extname(filename)
const sourceCode = context.getSourceCode()
const extensions = DEFAULT_EXTENSIONS.concat(
context.parserOptions.extensions || [],
MARKDOWN_EXTENSIONS,
context.parserOptions.markdownExtensions || [],
const extensions = new Set(
DEFAULT_EXTENSIONS.concat(
context.parserOptions.extensions || [],
MARKDOWN_EXTENSIONS,
context.parserOptions.markdownExtensions || [],
),
)
return {
Program(node) {
/* istanbul ignore if */
if (!extensions.includes(extname)) {
if (!extensions.has(extname)) {
return
}
const sourceText = sourceCode.getText(node)
Expand Down
15 changes: 7 additions & 8 deletions test/fixtures.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { basename } from 'path'

import { CLIEngine } from 'eslint'
import { ESLint } from 'eslint'

const cli = new CLIEngine({
const cli = new ESLint({
ignore: false,
fix: true,
})

describe('fixtures', () => {
it('should match all snapshots', () => {
cli
.executeOnFiles(['test/fixtures/*.{md,mdx}'])
.results.forEach(({ filePath, output, source }) =>
expect(output || source).toMatchSnapshot(basename(filePath)),
)
it('should match all snapshots', async () => {
const results = await cli.lintFiles(['test/fixtures/*.{md,mdx}'])
return results.forEach(({ filePath, output, source }) =>
expect(output || source).toMatchSnapshot(basename(filePath)),
)
})
})
1 change: 1 addition & 0 deletions test/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { RuleTester } from 'eslint'
export function noop<T extends unknown[] = unknown[], R = unknown>(
..._args: T
): R {
// eslint-disable-next-line unicorn/no-useless-undefined
return undefined
}

Expand Down
3 changes: 3 additions & 0 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
"extends": "@1stg/tslint-config/eslint",
"linterOptions": {
"exclude": ["node_modules/**", "**/lib/**", "**/*.d.ts"]
},
"rules": {
"no-useless-cast": false
}
}
Loading

0 comments on commit 28dc160

Please sign in to comment.