Skip to content

Commit

Permalink
feat: integrate with markuplint-sync (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin authored Apr 29, 2021
1 parent 978bd7f commit 62b23ed
Show file tree
Hide file tree
Showing 10 changed files with 675 additions and 81 deletions.
5 changes: 5 additions & 0 deletions .changeset/empty-books-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-markup": minor
---

feat: integrate with markuplint-sync
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ jobs:

- name: Build, Lint and test
run: |
yarn test
yarn build
yarn lint
yarn test
yarn typecov
env:
EFF_NO_LINK_RULES: true
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@
"@1stg/lib-config": "^1.2.10",
"@changesets/changelog-github": "^0.4.0",
"@changesets/cli": "^2.16.0",
"@markuplint/rule-textlint": "^0.1.0-alpha.5+c47a79f",
"@types/eslint": "^7.2.10",
"@types/htmlhint": "^0.9.2",
"@types/jest": "^26.0.22",
"@types/node": "^14.14.41",
"@types/uuid": "^8.3.0",
"eslint-plugin-mdx": "https://pkg.csb.dev/mdx-js/eslint-mdx/commit/6a3b2aa4/eslint-plugin-mdx",
"npm-run-all": "^4.1.5",
"rimraf": "^3.0.2",
"textlint-rule-max-comma": "^2.0.2",
"ts-jest": "^26.5.5",
"ts-node": "^9.1.1",
"type-coverage": "^2.17.3",
Expand Down
3 changes: 2 additions & 1 deletion packages/markup/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
},
"dependencies": {
"cosmiconfig": "^7.0.0",
"markuplint": "^1.6.1",
"markuplint": "^1.7.0-alpha.5+c47a79f",
"markuplint-sync": "^0.1.1",
"synckit": "^0.1.5",
"tslib": "^2.2.0"
},
Expand Down
6 changes: 2 additions & 4 deletions packages/markup/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,5 @@ export const getPhysicalFilename = (filename: string): string => {

const explorer = cosmiconfigSync('markuplint')

export const resolveConfig = (filename: string) => {
const result = explorer.search(filename)
return result ? result.filepath : undefined
}
export const resolveConfig = (filename: string) =>
explorer.search(filename)?.filepath
63 changes: 50 additions & 13 deletions packages/markup/src/rules/markup.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { Rule } from 'eslint'
import { exec } from 'markuplint'
import { exec as execAsync } from 'markuplint'
import { exec } from 'markuplint-sync'
import { createSyncFn } from 'synckit'

import { getPhysicalFilename, resolveConfig } from '../helpers'

const execSync = createSyncFn<typeof exec>(require.resolve('../worker'))
const execSync = createSyncFn<typeof execAsync>(require.resolve('../worker'))

const brokenCache = new Map<string, true>()

const BROKEN_ERROR_PATTERN = /^`(verify|fix)Sync` finished async. Use `\1` instead$/

export const markup: Rule.RuleModule = {
meta: {
Expand All @@ -13,26 +18,58 @@ export const markup: Rule.RuleModule = {
},
create(context) {
const filename = context.getFilename()
const { text } = context.getSourceCode()
const sourceText = context.getSourceCode().text

const config = resolveConfig(getPhysicalFilename(filename))

const execOptions = {
sourceCodes: sourceText,
names: filename,
config,
}

return {
// eslint-disable-next-line sonar/function-name
// eslint-disable-next-line sonarjs/cognitive-complexity, sonar/function-name
Program() {
const runMarkuplint = (fix?: boolean) =>
execSync({
sourceCodes: text,
names: filename,
config: resolveConfig(getPhysicalFilename(filename)),
if (!config) {
return
}

let broken = brokenCache.get(config)

const runMarkuplint = (fix?: boolean) => {
const options = {
...execOptions,
fix,
})
}

let fixed = 0
if (broken) {
return execSync(options)
}

try {
return exec(options)
} catch (err) {
/* istanbul ignore else */
if (BROKEN_ERROR_PATTERN.test((err as Error).message)) {
brokenCache.set(config, (broken = true))
return execSync(options)
}
// eslint-disable-next-line no-else-return -- https://github.com/istanbuljs/istanbuljs/issues/605
else {
throw err
}
}
}

const resultInfos = runMarkuplint()

if (resultInfos.length === 0) {
return
}

let fixed = 0

for (const { message, line, col } of resultInfos[0].results) {
context.report({
message,
Expand All @@ -45,10 +82,10 @@ export const markup: Rule.RuleModule = {
return null
}
const { fixedCode } = runMarkuplint(true)[0]
return text === fixedCode
return sourceText === fixedCode
? null
: {
range: [0, text.length],
range: [0, sourceText.length],
text: fixedCode,
}
},
Expand Down
14 changes: 4 additions & 10 deletions packages/markup/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,8 @@ import { runAsWorker } from 'synckit'
// eslint-disable-next-line @typescript-eslint/no-floating-promises
runAsWorker(async (options: Parameters<typeof exec>[0]) => {
const results = await exec(options)
return results.map(
({ results, filePath, sourceCode, fixedCode, parser, locale }) => ({
results,
filePath,
sourceCode,
fixedCode,
parser,
locale,
}),
)
return results.map(({ results, fixedCode }) => ({
results,
fixedCode,
}))
})
14 changes: 14 additions & 0 deletions packages/markup/test/fixtures/textlint/.markuplintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
rules: {
textlint: {
option: {
rules: [
{
ruleId: 'max-comma',
rule: require('textlint-rule-max-comma').default,
},
],
},
},
},
}
33 changes: 30 additions & 3 deletions packages/markup/test/rules.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,29 @@ tester.run('markup', markup, {
</html>`,
},
{
code: '',
...options,
code: '<header>Header1</header>',
},
{
code: '',
...options,
code: '<header>Header2</header>',
filename: path.join(__filename, '0_fake_virtual_filename.html'),
},
{
code: '',
...options,
code: '<header>Header3</header>',
filename: path.join(__dirname),
},
{
...options,
code: '<header>Header4</header>',
filename: path.join(__dirname, 'fake.html'),
},
{
...options,
code: '<header>Header5</header>',
filename: path.join(__dirname, 'fixtures/textlint/test.html'),
},
],
invalid: [
{
Expand All @@ -58,5 +71,19 @@ tester.run('markup', markup, {
},
],
},
{
...options,
code: '<p>a,c,d,e,f,g</p>',
filename: path.join(__dirname, 'fixtures/textlint/test.html'),
errors: [
{
message:
'Invalid text: This sentence exceeds the maximum count of comma. Maximum is 4.',
// FIXME: https://github.com/markuplint/markuplint/issues/160
line: 2,
column: 11,
},
],
},
],
})
Loading

0 comments on commit 62b23ed

Please sign in to comment.