Skip to content

Commit

Permalink
feat: upgrade htmlhint 1.0 and markuplint 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Mar 6, 2022
1 parent 189e6a9 commit a2c68dd
Show file tree
Hide file tree
Showing 16 changed files with 3,688 additions and 4,077 deletions.
5 changes: 5 additions & 0 deletions .changeset/little-queens-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-plugin-htm': minor
---

feat: upgrade htmlhint 1.0
5 changes: 5 additions & 0 deletions .changeset/little-steaks-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-plugin-utils': minor
---

feat: update entry files extensions
5 changes: 5 additions & 0 deletions .changeset/old-oranges-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-plugin-markup': minor
---

feat: upgrade markuplint 2.0
26 changes: 13 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@
"typecov": "type-coverage"
},
"devDependencies": {
"@1stg/lib-config": "^3.0.0",
"@changesets/changelog-github": "^0.4.0",
"@changesets/cli": "^2.16.0",
"@markuplint/rule-textlint": "^0.2.0-alpha.1",
"@types/eslint": "^7.28.0",
"@types/htmlhint": "^0.9.3",
"@types/jest": "^26.0.24",
"@types/node": "^16.3.1",
"@1stg/lib-config": "^5.0.0",
"@changesets/changelog-github": "^0.4.3",
"@changesets/cli": "^2.21.1",
"@markuplint/rule-textlint": "^2.2.0",
"@types/eslint": "^8.4.1",
"@types/htmlhint": "^1.1.2",
"@types/jest": "^27.4.1",
"@types/node": "^17.0.21",
"textlint-rule-max-comma": "^2.0.2",
"ts-jest": "^27.0.3",
"ts-node": "^10.1.0",
"type-coverage": "^2.18.0",
"typescript": "^4.3.5"
"ts-jest": "^27.1.3",
"ts-node": "^10.7.0",
"type-coverage": "^2.21.0",
"typescript": "^4.6.2"
},
"commitlint": {
"extends": "@1stg"
Expand Down Expand Up @@ -73,7 +73,7 @@
]
},
"typeCoverage": {
"atLeast": 99.6,
"atLeast": 100,
"cache": true,
"detail": true,
"ignoreAsAssertion": true,
Expand Down
10 changes: 5 additions & 5 deletions packages/htm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
"node": ">=10"
},
"main": "lib",
"module": "lib/esm",
"es2015": "lib/es2015",
"fesm5": "lib/esm",
"module": "lib/esm.mjs",
"es2015": "lib/es2015.mjs",
"fesm5": "lib/esm.mjs",
"types": "lib",
"files": [
"lib",
Expand All @@ -35,8 +35,8 @@
},
"dependencies": {
"eslint-plugin-utils": "^0.1.0",
"htmlhint": "^0.15.1",
"tslib": "^2.3.0"
"htmlhint": "^1.1.2",
"tslib": "^2.3.1"
},
"publishConfig": {
"access": "public"
Expand Down
17 changes: 7 additions & 10 deletions packages/htm/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import { registerJsonMessageHandler } from 'eslint-plugin-utils'
import { LintResult } from 'htmlhint'
import { Hint } from 'htmlhint/types'

export * from './parser'
export * as configs from './configs'
export * from './parser'
export * as rules from './rules'

registerJsonMessageHandler(
'htm/html',
({ evidence, rule, message }: LintResult) => ({
ruleId: `htm/${rule.id}`,
message: `${message}
registerJsonMessageHandler('htm/html', ({ evidence, rule, message }: Hint) => ({
ruleId: `htm/${rule.id}`,
message: `${message}
evidence: ${evidence}
reference: ${rule.link}`,
}),
)
reference: ${rule.link ?? '-'}`,
}))
2 changes: 1 addition & 1 deletion packages/htm/test/__snapshots__/fixtures.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Array [
"line": 1,
"message": "Doctype must be declared first.
evidence: <!--comment--><!DOCTYPE html>
reference: https://github.com/thedaviddias/HTMLHint/wiki/doctype-first",
reference: https://htmlhint.com/docs/user-guide/rules/doctype-first",
"nodeType": null,
"ruleId": "htm/doctype-first",
"severity": 1,
Expand Down
15 changes: 7 additions & 8 deletions packages/markup/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
"node": ">=10"
},
"main": "lib",
"module": "lib/esm",
"es2015": "lib/es2015",
"fesm5": "lib/esm",
"module": "lib/esm.mjs",
"es2015": "lib/es2015.mjs",
"fesm5": "lib/esm.mjs",
"types": "lib",
"files": [
"lib",
Expand All @@ -35,12 +35,11 @@
"eslint": ">=5.0.0"
},
"dependencies": {
"cosmiconfig": "^7.0.0",
"cosmiconfig": "^7.0.1",
"eslint-plugin-utils": "^0.1.0",
"markuplint": "^1.10.1",
"markuplint-sync": "^0.3.2",
"synckit": "^0.3.4",
"tslib": "^2.3.0"
"markuplint": "^2.2.0",
"synckit": "^0.6.0",
"tslib": "^2.3.1"
},
"publishConfig": {
"access": "public"
Expand Down
31 changes: 0 additions & 31 deletions packages/markup/src/helpers.ts

This file was deleted.

12 changes: 4 additions & 8 deletions packages/markup/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import { registerJsonMessageHandler } from 'eslint-plugin-utils'
import { VerifiedResult } from '@markuplint/ml-config'
import { Violation } from '@markuplint/ml-config'
import { Linter } from 'eslint'
import { registerJsonMessageHandler } from 'eslint-plugin-utils'

export * from './helpers'
export * as configs from './configs'
export * from './parser'
export * from './rules'
export * as configs from './configs'
export * as rules from './rules'

const SEVERITIES = ['info', 'warning', 'error'] as const

registerJsonMessageHandler(
'markup/markup',
(
{ ruleId, severity, message }: VerifiedResult,
{ severity: eslintSeverity },
) => ({
({ ruleId, severity, message }: Violation, { severity: eslintSeverity }) => ({
ruleId: `markup/${ruleId}`,
message,
severity: Math.max(
Expand Down
73 changes: 19 additions & 54 deletions packages/markup/src/rules/markup.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
import { Target } from '@markuplint/file-resolver'
import { Violation } from '@markuplint/ml-config'
import { Rule } from 'eslint'
import { exec as execAsync } from 'markuplint'
import { exec } from 'markuplint-sync'
import { createSyncFn } from 'synckit'

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

const workerPath = require.resolve('../worker')

// call `creatSyncFn` lazily for performance, it is already cached inside, related #31
const _ = {
get execSync() {
return createSyncFn<typeof execAsync>(workerPath)
get markuplintSync() {
return createSyncFn<
(
target: Target,
fix?: boolean,
) => Promise<{ violations: Violation[]; fixedCode: string }>
>(workerPath)
},
}

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

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

export const markup: Rule.RuleModule = {
meta: {
fixable: 'code',
Expand All @@ -28,58 +26,25 @@ export const markup: Rule.RuleModule = {
const filename = context.getFilename()
const sourceText = context.getSourceCode().text

const config = resolveConfig(getPhysicalFilename(filename))

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

const runMarkuplint = (fix?: boolean) =>
_.markuplintSync(markuplintOptions, fix)

return {
// eslint-disable-next-line sonarjs/cognitive-complexity
Program() {
if (!config) {
return
}

let broken = brokenCache.get(config)

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

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()
const { violations } = runMarkuplint()

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

let fixed = 0

for (const { ruleId, severity, message, line, col } of resultInfos[0]
.results) {
for (const { ruleId, severity, message, line, col } of violations) {
context.report({
message: JSON.stringify({ severity, message, ruleId }),
loc: {
Expand All @@ -91,7 +56,7 @@ export const markup: Rule.RuleModule = {
if (fixed++) {
return null
}
const { fixedCode } = runMarkuplint(true)[0]
const { fixedCode } = runMarkuplint(true)
return sourceText === fixedCode
? null
: {
Expand Down
14 changes: 5 additions & 9 deletions packages/markup/src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { exec } from 'markuplint'
import { Target } from '@markuplint/file-resolver'
import { mlTestFile } from 'markuplint'
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, fixedCode }) => ({
results,
fixedCode,
}))
})
runAsWorker((target: Target, fix?: boolean) =>
mlTestFile(target, undefined, undefined, undefined, fix),
)
2 changes: 1 addition & 1 deletion packages/markup/test/__snapshots__/fixtures.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ Array [
Object {
"column": 1,
"line": 1,
"message": "Required doctype",
"message": "Require doctype",
"nodeType": null,
"ruleId": "markup/doctype",
"severity": 2,
Expand Down
28 changes: 16 additions & 12 deletions packages/markup/test/rules.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ tester.run('markup', markup, {
valid: [
{
...options,
code: /* HTML */ `<!DOCTYPE html>
code: /* HTML */ `
<!DOCTYPE html>
<html>
<head>
<title>Any Page</title>
Expand All @@ -24,7 +25,8 @@ tester.run('markup', markup, {
<h1>Any Page</h1>
<p>Anonymous</p>
</body>
</html>`,
</html>
`,
},
{
...options,
Expand Down Expand Up @@ -54,20 +56,22 @@ tester.run('markup', markup, {
invalid: [
{
...options,
code: /* HTML */ `<html>
<head>
<title>Any Page</title>
</head>
<body>
<h1>Any Page</h1>
<p>Anonymous</p>
</body>
</html>`,
code: /* HTML */ `
<html>
<head>
<title>Any Page</title>
</head>
<body>
<h1>Any Page</h1>
<p>Anonymous</p>
</body>
</html>
`,
errors: [
{
message: JSON.stringify({
severity: 'error',
message: 'Required doctype',
message: 'Require doctype',
ruleId: 'doctype',
}),
line: 1,
Expand Down
Loading

0 comments on commit a2c68dd

Please sign in to comment.