11// LICENSE : MIT
2- "use strict" ;
32import { RuleHelper } from "textlint-rule-helper" ;
4-
53import { parse } from "@babel/parser" ;
6- /**
7- * RegExp#flags polyfill
8- */
9- if ( RegExp . prototype . flags === undefined ) {
10- Object . defineProperty ( RegExp . prototype , "flags" , {
11- configurable : true ,
12- get : function ( ) {
13- return this . toString ( ) . match ( / [ g i m u y ] * $ / ) [ 0 ] ;
14- }
15- } ) ;
16- }
4+ import { fromYAMLFilePath , fromYAML } from "prh" ;
5+ import path from "node:path" ;
6+ import os from "node:os" ;
177
18- const prh = require ( "prh" ) ;
19- const path = require ( "path" ) ;
20- const untildify = require ( "untildify" ) ;
8+ const homeDirectory = os . homedir ( ) ;
219
10+ const untildify = ( filePath ) => {
11+ return homeDirectory ? filePath . replace ( / ^ ~ (? = $ | \/ | \\ ) / , homeDirectory ) : filePath ;
12+ } ;
2213const defaultOptions = {
2314 checkLink : false ,
2415 checkBlockQuote : false ,
@@ -39,10 +30,10 @@ function createPrhEngine(rulePaths, baseDir) {
3930 if ( rulePaths . length === 0 ) {
4031 return null ;
4132 }
42- const expandedRulePaths = rulePaths . map ( rulePath => untildify ( rulePath ) ) ;
43- const prhEngine = prh . fromYAMLFilePath ( path . resolve ( baseDir , expandedRulePaths [ 0 ] ) ) ;
44- expandedRulePaths . slice ( 1 ) . forEach ( ruleFilePath => {
45- const config = prh . fromYAMLFilePath ( path . resolve ( baseDir , ruleFilePath ) ) ;
33+ const expandedRulePaths = rulePaths . map ( ( rulePath ) => untildify ( rulePath ) ) ;
34+ const prhEngine = fromYAMLFilePath ( path . resolve ( baseDir , expandedRulePaths [ 0 ] ) ) ;
35+ expandedRulePaths . slice ( 1 ) . forEach ( ( ruleFilePath ) => {
36+ const config = fromYAMLFilePath ( path . resolve ( baseDir , ruleFilePath ) ) ;
4637 prhEngine . merge ( config ) ;
4738 } ) ;
4839 return prhEngine ;
@@ -53,24 +44,24 @@ function createPrhEngineFromContents(yamlContents) {
5344 return null ;
5445 }
5546 const dummyFilePath = "" ;
56- const prhEngine = prh . fromYAML ( dummyFilePath , yamlContents [ 0 ] ) ;
57- yamlContents . slice ( 1 ) . forEach ( content => {
58- const config = prh . fromYAML ( dummyFilePath , content ) ;
47+ const prhEngine = fromYAML ( dummyFilePath , yamlContents [ 0 ] ) ;
48+ yamlContents . slice ( 1 ) . forEach ( ( content ) => {
49+ const config = fromYAML ( dummyFilePath , content ) ;
5950 prhEngine . merge ( config ) ;
6051 } ) ;
6152 return prhEngine ;
6253}
6354
6455function mergePrh ( ...engines ) {
65- const engines_ = engines . filter ( engine => ! ! engine ) ;
56+ const engines_ = engines . filter ( ( engine ) => ! ! engine ) ;
6657 const mainEngine = engines_ [ 0 ] ;
67- engines_ . slice ( 1 ) . forEach ( engine => {
58+ engines_ . slice ( 1 ) . forEach ( ( engine ) => {
6859 mainEngine . merge ( engine ) ;
6960 } ) ;
7061 return mainEngine ;
7162}
7263
73- const assertOptions = options => {
64+ const assertOptions = ( options ) => {
7465 if ( typeof options . ruleContents === "undefined" && typeof options . rulePaths === "undefined" ) {
7566 throw new Error ( `textlint-rule-prh require Rule Options.
7667Please set .textlintrc:
@@ -107,19 +98,19 @@ const createIgnoreNodeTypes = (options, Syntax) => {
10798 * @param {ChangeSet } changeSet
10899 * @param {string } str
109100 * @param {function({
110- matchStartIndex: number,
111- matchEndIndex: number,
112- actual: string
113- expected: string
114- })}onChangeOfMatch
101+ matchStartIndex: number,
102+ matchEndIndex: number,
103+ actual: string
104+ expected: string
105+ })}onChangeOfMatch
115106 */
116107const forEachChange = ( changeSet , str , onChangeOfMatch ) => {
117- const sortedDiffs = changeSet . diffs . sort ( function ( a , b ) {
108+ const sortedDiffs = changeSet . diffs . sort ( function ( a , b ) {
118109 return a . index - b . index ;
119110 } ) ;
120111 let delta = 0 ;
121- sortedDiffs . forEach ( function ( diff ) {
122- const result = diff . expected . replace ( / \$ ( [ 0 - 9 ] { 1 , 2 } ) / g, function ( match , g1 ) {
112+ sortedDiffs . forEach ( function ( diff ) {
113+ const result = diff . expected . replace ( / \$ ( [ 0 - 9 ] { 1 , 2 } ) / g, function ( match , g1 ) {
123114 const index = parseInt ( g1 ) ;
124115 if ( index === 0 || diff . matches . length - 1 < index ) {
125116 return match ;
@@ -144,7 +135,7 @@ const forEachChange = (changeSet, str, onChangeOfMatch) => {
144135 delta += result . length - diff . matches [ 0 ] . length ;
145136 } ) ;
146137} ;
147- const getConfigBaseDir = context => {
138+ const getConfigBaseDir = ( context ) => {
148139 if ( typeof context . getConfigBaseDir === "function" ) {
149140 return context . getConfigBaseDir ( ) || process . cwd ( ) ;
150141 }
@@ -236,7 +227,7 @@ function reporter(context, userOptions = {}) {
236227 if ( ! lang ) {
237228 return ;
238229 }
239- const checkLang = codeCommentTypes . some ( type => {
230+ const checkLang = codeCommentTypes . some ( ( type ) => {
240231 return type === node . lang ;
241232 } ) ;
242233 if ( ! checkLang ) {
@@ -245,7 +236,7 @@ function reporter(context, userOptions = {}) {
245236 const rawText = getSource ( node ) ;
246237 const codeText = getUntrimmedCode ( node , rawText ) ;
247238 const sourceBlockDiffIndex = rawText !== node . value ? rawText . indexOf ( codeText ) : 0 ;
248- const reportComment = comment => {
239+ const reportComment = ( comment ) => {
249240 // to get position from index
250241 // https://github.com/prh/prh/issues/29
251242 const dummyFilePath = "" ;
@@ -286,7 +277,7 @@ function reporter(context, userOptions = {}) {
286277 if ( ! comments ) {
287278 return ;
288279 }
289- comments . forEach ( comment => {
280+ comments . forEach ( ( comment ) => {
290281 reportComment ( comment ) ;
291282 } ) ;
292283 } catch ( error ) {
@@ -299,7 +290,7 @@ function reporter(context, userOptions = {}) {
299290 } ;
300291}
301292
302- module . exports = {
293+ export default {
303294 linter : reporter ,
304295 fixer : reporter
305296} ;
0 commit comments