Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use processor to display remark reports directly #281

Merged
merged 1 commit into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
const tsNode = require('ts-node')

tsNode.register({
compilerOptions: {
module: 'commonjs',
pretty: true,
},
transpileOnly: true,
})
tsNode.register()

module.exports = {
root: true,
Expand Down
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,13 @@
"prettier": "@1stg/prettier-config",
"remarkConfig": {
"plugins": [
"@1stg/remark-config"
"@1stg/remark-config",
[
"lint-no-duplicate-headings",
[
2
]
]
]
},
"renovate": {
Expand Down
5 changes: 4 additions & 1 deletion packages/eslint-plugin-mdx/src/configs/base.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export const base = {
import { Linter } from 'eslint'

export const base: Linter.Config = {
parser: 'eslint-mdx',
plugins: ['mdx'],
processor: 'mdx/remark',
}
4 changes: 3 additions & 1 deletion packages/eslint-plugin-mdx/src/configs/overrides.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Linter } from 'eslint'

import { base } from './base'
import { getGlobals } from './helper'

Expand All @@ -12,7 +14,7 @@ try {
rebass = ['Box', 'Flex', 'Text', 'Heading', 'Link', 'Button', 'Image', 'Card']
}

export const overrides = {
export const overrides: Linter.Config = {
...base,
globals: getGlobals(rebass, {
React: false,
Expand Down
3 changes: 2 additions & 1 deletion packages/eslint-plugin-mdx/src/configs/recommended.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Linter } from 'eslint'
import { version } from 'eslint/package.json'

import { base } from './base'

const minorVersion = +version.split('.').slice(0, 2).join('.')

export const recommended = {
export const recommended: Linter.Config = {
...base,
rules: {
'mdx/no-jsx-html-comments': 2,
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin-mdx/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * as configs from './configs'

export * from './configs/helper'
export * from './processors'
export * from './rules'
4 changes: 4 additions & 0 deletions packages/eslint-plugin-mdx/src/processors/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { remark } from './remark'

// related to https://github.com/benmosher/eslint-plugin-import/issues/2002
export const processors = { remark }
36 changes: 36 additions & 0 deletions packages/eslint-plugin-mdx/src/processors/remark.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Linter } from 'eslint'

import { RemarkLintMessage } from '../rules'

export const remark = {
postprocess(lintMessages: Linter.LintMessage[][]): Linter.LintMessage[] {
const messages: Linter.LintMessage[] = []
for (const lintMessageList of lintMessages) {
messages.push(
...lintMessageList.map(lintMessage => {
const {
message,
ruleId: eslintRuleId,
severity: eslintSeverity,
} = lintMessage
if (eslintRuleId !== 'mdx/remark') {
return lintMessage
}

const { source, ruleId, reason, severity } = JSON.parse(
message,
) as RemarkLintMessage

return {
...lintMessage,
ruleId: `${source}-${ruleId}`,
message: reason,
severity: Math.max(eslintSeverity, severity) as Linter.Severity,
}
}),
)
}
return messages
},
supportsAutofix: true,
}
28 changes: 19 additions & 9 deletions packages/eslint-plugin-mdx/src/rules/remark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { DEFAULT_EXTENSIONS, MARKDOWN_EXTENSIONS } from 'eslint-mdx'
import vfile from 'vfile'

import { getRemarkProcessor } from './helper'
import { RemarkLintMessage } from './types'

export const remark: Rule.RuleModule = {
meta: {
Expand All @@ -14,9 +15,6 @@ export const remark: Rule.RuleModule = {
category: 'Stylistic Issues',
recommended: true,
},
messages: {
remarkReport: '{{ source }}:{{ ruleId }} - {{ reason }}',
},
fixable: 'code',
},
create(context) {
Expand All @@ -34,6 +32,7 @@ export const remark: Rule.RuleModule = {
options.markdownExtensions || [],
).includes(extname)
return {
// eslint-disable-next-line sonarjs/cognitive-complexity
Program(node) {
/* istanbul ignore if */
if (!isMdx && !isMarkdown) {
Expand All @@ -59,15 +58,26 @@ export const remark: Rule.RuleModule = {
source,
reason,
ruleId,
fatal,
location: { start, end },
} of file.messages) {
// https://github.com/remarkjs/remark-lint/issues/65#issuecomment-220800231
/* istanbul ignore next */
const severity = fatal ? 2 : fatal == null ? 0 : 1
/* istanbul ignore if */
if (!severity) {
// should never happen, just for robustness
continue
}
const message: RemarkLintMessage = {
reason,
source,
ruleId,
severity,
}
context.report({
messageId: 'remarkReport',
data: {
reason,
source,
ruleId,
},
// related to https://github.com/eslint/eslint/issues/14198
message: JSON.stringify(message),
loc: {
// ! eslint ast column is 0-indexed, but unified is 1-indexed
start: {
Expand Down
8 changes: 8 additions & 0 deletions packages/eslint-plugin-mdx/src/rules/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Linter } from 'eslint'
// eslint-disable-next-line node/no-extraneous-import
import { ExpressionStatement, Node } from 'estree'
import { Attacher } from 'unified'
Expand All @@ -18,3 +19,10 @@ export interface RemarkConfig {
settings: Record<string, string>
plugins: Array<RemarkPlugin | [RemarkPlugin, ...unknown[]]>
}

export interface RemarkLintMessage {
reason: string
source: string
ruleId: string
severity: Linter.Severity
}
49 changes: 43 additions & 6 deletions test/__snapshots__/fixtures.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ Array [
"text": "# Hello, world!",
},
"line": 27,
"message": "remark-lint:no-multiple-toplevel-headings - Don’t use multiple top level headings (3:1)",
"messageId": "remarkReport",
"message": "Don’t use multiple top level headings (3:1)",
"nodeType": "Program",
"ruleId": "mdx/remark",
"ruleId": "remark-lint-no-multiple-toplevel-headings",
"severity": 1,
},
Object {
Expand Down Expand Up @@ -56,10 +55,9 @@ Array [
"text": "# Here's a text gradient shortcode!",
},
"line": 35,
"message": "remark-lint:no-multiple-toplevel-headings - Don’t use multiple top level headings (3:1)",
"messageId": "remarkReport",
"message": "Don’t use multiple top level headings (3:1)",
"nodeType": "Program",
"ruleId": "mdx/remark",
"ruleId": "remark-lint-no-multiple-toplevel-headings",
"severity": 1,
},
Object {
Expand Down Expand Up @@ -133,6 +131,45 @@ Array [
]
`;

exports[`fixtures should match all snapshots: processor.mdx 1`] = `
Array [
Object {
"column": 1,
"endColumn": 6,
"endLine": 3,
"fix": Object {
"range": Array [
7,
12,
],
"text": "# abc",
},
"line": 3,
"message": "Do not use headings with similar content (1:1)",
"nodeType": "Program",
"ruleId": "remark-lint-no-duplicate-headings",
"severity": 2,
},
Object {
"column": 1,
"endColumn": 6,
"endLine": 3,
"fix": Object {
"range": Array [
7,
12,
],
"text": "# abc",
},
"line": 3,
"message": "Don’t use multiple top level headings (1:1)",
"nodeType": "Program",
"ruleId": "remark-lint-no-multiple-toplevel-headings",
"severity": 1,
},
]
`;

exports[`fixtures should match all snapshots: remark.md 1`] = `Array []`;

exports[`fixtures should match all snapshots: remark.mdx 1`] = `Array []`;
3 changes: 3 additions & 0 deletions test/fixtures/processor.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# abc

# abc
1 change: 1 addition & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "./node_modules/@1stg/tsconfig/lib.json",
"compilerOptions": {
"declarationMap": false,
"strictFunctionTypes": false,
"strictNullChecks": false
}
Expand Down
7 changes: 7 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
"compilerOptions": {
"noEmit": true
},
"ts-node": {
"compilerOptions": {
"module": "commonjs",
"pretty": true
},
"transpileOnly": true
},
"references": [
{
"path": "packages/eslint-mdx"
Expand Down