Skip to content

Commit

Permalink
refactor: apply stricter rules
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Mar 2, 2021
1 parent ae59913 commit b48311c
Show file tree
Hide file tree
Showing 7 changed files with 926 additions and 869 deletions.
4 changes: 1 addition & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ module.exports = {
root: true,
extends: ['@1stg'],
rules: {
'@typescript-eslint/naming-convention': 0,
// `strictNullChecks` is required
'@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
},
}
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
"lint:es": "cross-env PARSER_NO_WATCH=true eslint . --cache --ext js,md,ts -f friendly",
"lint:ts": "tslint -p . -t stylish",
"lint:tsc": "tsc",
"postinstall": "yarn-deduplicate || exit 0",
"postinstall": "yarn-deduplicate --strategy fewer || exit 0",
"prelint": "yarn build:ts",
"prerelease": "yarn build",
"pretest": "yarn clean",
"release": "lerna publish --conventional-commits --create-release github --yes",
"test": "ts-node --skip-ignore node_modules/.bin/jest",
"test": "jest",
"typecov": "type-coverage"
},
"devDependencies": {
"@1stg/lib-config": "^1.1.8",
"@1stg/tslint-config": "^1.0.1",
"@1stg/lib-config": "^1.1.9",
"@1stg/tslint-config": "^1.1.0",
"@types/eslint": "^7.2.6",
"@types/jest": "^26.0.20",
"@types/node": "^14.14.31",
Expand All @@ -48,6 +48,7 @@
"yarn-deduplicate": "^3.1.0"
},
"resolutions": {
"@babel/core": "^7.13.8",
"prettier": "^2.2.1",
"tslib": "^2.1.0"
},
Expand Down
12 changes: 7 additions & 5 deletions packages/eslint-mdx/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ export const isJsxNode = (node: { type: string }): node is JsxNode =>
export const normalizeParser = (parser?: ParserOptions['parser']) => {
if (parser) {
if (typeof parser === 'string') {
// eslint-disable-next-line @typescript-eslint/no-require-imports
parser = require(parser)
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
parser = require(parser) as ParserOptions['parser']
}

if (typeof parser === 'object') {
Expand All @@ -50,12 +50,14 @@ export const normalizeParser = (parser?: ParserOptions['parser']) => {
for (const fallback of FALLBACK_PARSERS) {
try {
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
const fallbackParser: Linter.ParserModule = require(fallback)
const fallbackParser = require(fallback) as Linter.ParserModule
/* istanbul ignore next */
const parserFn =
'parseForESLint' in fallbackParser
? fallbackParser.parseForESLint
: fallbackParser.parse
? // eslint-disable-next-line @typescript-eslint/unbound-method
fallbackParser.parseForESLint
: // eslint-disable-next-line @typescript-eslint/unbound-method
fallbackParser.parse
/* istanbul ignore else */
if (parserFn) {
parsers.unshift(parserFn)
Expand Down
5 changes: 3 additions & 2 deletions packages/eslint-mdx/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,9 @@ export class Parser {
try {
program = parser(code, this._options)
break
} catch (e) {
} catch (err) {
if (!parseError) {
parseError = e as Error
parseError = err as Error
}
}
}
Expand Down Expand Up @@ -387,4 +387,5 @@ export class Parser {

export const parser = new Parser()

// eslint-disable-next-line @typescript-eslint/unbound-method
export const { parse, parseForESLint } = parser
4 changes: 2 additions & 2 deletions packages/eslint-plugin-mdx/src/configs/overrides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { getGlobals } from './helper'
let rebass: typeof import('rebass') | string[]

try {
// eslint-disable-next-line @typescript-eslint/no-require-imports
rebass = require('rebass')
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
rebass = require('rebass') as typeof rebass
} catch {
// `rebass`(or `reflexbox` actually) requires `react` as peerDependency, but not all projects using `mdx` are `React` based, so we fallback to hardcoded `rebass` Components here
/* istanbul ignore next */
Expand Down
14 changes: 7 additions & 7 deletions packages/eslint-plugin-mdx/src/rules/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ export const requirePkg = <T>(
let error: Error
for (const pkg of packages) {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports, @typescript-eslint/no-unsafe-return
return require(pkg)
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
return require(pkg) as T
} catch (err) {
if (!error) {
error = err
error = err as Error
}
}
}
Expand All @@ -53,10 +53,10 @@ export const getRemarkProcessor = (searchFrom: string, isMdx: boolean) => {
}

/* istanbul ignore next */
const { config, filepath }: Partial<CosmiconfigResult> =
searchSync(searchFrom) || {}
const result: Partial<CosmiconfigResult> = searchSync(searchFrom) || {}
/* istanbul ignore next */
const { plugins = [], settings }: Partial<RemarkConfig> = config || {}
const { plugins = [], settings } = (result.config ||
{}) as Partial<RemarkConfig>

try {
// disable this rule automatically since we already have a parser option `extensions`
Expand All @@ -80,7 +80,7 @@ export const getRemarkProcessor = (searchFrom: string, isMdx: boolean) => {
return processor.use(
/* istanbul ignore next */
typeof plugin === 'string'
? requirePkg(plugin, 'remark', filepath)
? requirePkg(plugin, 'remark', result.filepath)
: plugin,
...pluginSettings,
)
Expand Down
Loading

0 comments on commit b48311c

Please sign in to comment.