1
+ import { Target } from '@markuplint/file-resolver'
2
+ import { Violation } from '@markuplint/ml-config'
1
3
import { Rule } from 'eslint'
2
- import { exec as execAsync } from 'markuplint'
3
- import { exec } from 'markuplint-sync'
4
4
import { createSyncFn } from 'synckit'
5
5
6
- import { getPhysicalFilename , resolveConfig } from '../helpers'
7
-
8
6
const workerPath = require . resolve ( '../worker' )
9
7
10
8
// call `creatSyncFn` lazily for performance, it is already cached inside, related #31
11
9
const _ = {
12
- get execSync ( ) {
13
- return createSyncFn < typeof execAsync > ( workerPath )
10
+ get markuplintSync ( ) {
11
+ return createSyncFn <
12
+ (
13
+ target : Target ,
14
+ fix ?: boolean ,
15
+ ) => Promise < { violations : Violation [ ] ; fixedCode : string } >
16
+ > ( workerPath )
14
17
} ,
15
18
}
16
19
17
- const brokenCache = new Map < string , true > ( )
18
-
19
- const BROKEN_ERROR_PATTERN =
20
- / ^ ` ( v e r i f y | f i x ) S y n c ` f i n i s h e d a s y n c . U s e ` \1` i n s t e a d $ /
21
-
22
20
export const markup : Rule . RuleModule = {
23
21
meta : {
24
22
fixable : 'code' ,
@@ -28,58 +26,25 @@ export const markup: Rule.RuleModule = {
28
26
const filename = context . getFilename ( )
29
27
const sourceText = context . getSourceCode ( ) . text
30
28
31
- const config = resolveConfig ( getPhysicalFilename ( filename ) )
32
-
33
- const execOptions = {
34
- sourceCodes : sourceText ,
35
- names : filename ,
36
- config,
29
+ const markuplintOptions = {
30
+ sourceCode : sourceText ,
31
+ name : filename ,
37
32
}
38
33
34
+ const runMarkuplint = ( fix ?: boolean ) =>
35
+ _ . markuplintSync ( markuplintOptions , fix )
36
+
39
37
return {
40
- // eslint-disable-next-line sonarjs/cognitive-complexity
41
38
Program ( ) {
42
- if ( ! config ) {
43
- return
44
- }
45
-
46
- let broken = brokenCache . get ( config )
47
-
48
- const runMarkuplint = ( fix ?: boolean ) => {
49
- const options = {
50
- ...execOptions ,
51
- fix,
52
- }
53
-
54
- if ( broken ) {
55
- return _ . execSync ( options )
56
- }
57
-
58
- try {
59
- return exec ( options )
60
- } catch ( err ) {
61
- /* istanbul ignore else */
62
- if ( BROKEN_ERROR_PATTERN . test ( ( err as Error ) . message ) ) {
63
- brokenCache . set ( config , ( broken = true ) )
64
- return _ . execSync ( options )
65
- }
66
- // eslint-disable-next-line no-else-return -- https://github.com/istanbuljs/istanbuljs/issues/605
67
- else {
68
- throw err
69
- }
70
- }
71
- }
72
-
73
- const resultInfos = runMarkuplint ( )
39
+ const { violations } = runMarkuplint ( )
74
40
75
- if ( resultInfos . length === 0 ) {
41
+ if ( violations . length === 0 ) {
76
42
return
77
43
}
78
44
79
45
let fixed = 0
80
46
81
- for ( const { ruleId, severity, message, line, col } of resultInfos [ 0 ]
82
- . results ) {
47
+ for ( const { ruleId, severity, message, line, col } of violations ) {
83
48
context . report ( {
84
49
message : JSON . stringify ( { severity, message, ruleId } ) ,
85
50
loc : {
@@ -91,7 +56,7 @@ export const markup: Rule.RuleModule = {
91
56
if ( fixed ++ ) {
92
57
return null
93
58
}
94
- const { fixedCode } = runMarkuplint ( true ) [ 0 ]
59
+ const { fixedCode } = runMarkuplint ( true )
95
60
return sourceText === fixedCode
96
61
? null
97
62
: {
0 commit comments