get-convert-path.js 5.67 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/**
 * @author Toru Nagashima
 * @copyright 2016 Toru Nagashima. All rights reserved.
 * See LICENSE file in root directory for full license.
 */
"use strict"

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const Minimatch = require("minimatch").Minimatch

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------

/**
 * @param {any} x - An any value.
 * @returns {any} Always `x`.
 */
function identity(x) {
    return x
}

/**
 * Converts old-style value to new-style value.
 *
 * @param {any} x - The value to convert.
 * @returns {({include: string[], exclude: string[], replace: string[]})[]} Normalized value.
 */
function normalizeValue(x) {
    if (Array.isArray(x)) {
        return x
    }

    return Object.keys(x).map(pattern => ({
        include: [pattern],
        exclude: [],
        replace: x[pattern],
    }))
}

/**
 * Ensures the given value is a string array.
 *
 * @param {any} x - The value to ensure.
 * @returns {string[]} The string array.
 */
function toStringArray(x) {
    if (Array.isArray(x)) {
        return x.map(String)
    }
    return []
}

/**
 * Creates the function which checks whether a file path is matched with the given pattern or not.
 *
 * @param {string[]} includePatterns - The glob patterns to include files.
 * @param {string[]} excludePatterns - The glob patterns to exclude files.
 * @returns {function} Created predicate function.
 */
function createMatch(includePatterns, excludePatterns) {
    const include = includePatterns.map(pattern => new Minimatch(pattern))
    const exclude = excludePatterns.map(pattern => new Minimatch(pattern))

    return (filePath) =>
        include.some(m => m.match(filePath)) &&
        !exclude.some(m => m.match(filePath))
}

/**
 * Creates a function which replaces a given path.
 *
 * @param {RegExp} fromRegexp - A `RegExp` object to replace.
 * @param {string} toStr - A new string to replace.
 * @returns {function} A function which replaces a given path.
 */
function defineConvert(fromRegexp, toStr) {
    return (filePath) =>
        filePath.replace(fromRegexp, toStr)
}

/**
 * Combines given converters.
 * The result function converts a given path with the first matched converter.
 *
 * @param {{match: function, convert: function}} converters - A list of converters to combine.
 * @returns {function} A function which replaces a given path.
 */
function combine(converters) {
    return (filePath) => {
        for (const converter of converters) {
            if (converter.match(filePath)) {
                return converter.convert(filePath)
            }
        }
        return filePath
    }
}

/**
 * Parses `convertPath` property from a given option object.
 *
 * @param {object|undefined} option - An option object to get.
 * @returns {function|null} A function which converts a path., or `null`.
 */
function parse(option) {
    if (!option ||
        !option.convertPath ||
        typeof option.convertPath !== "object"
    ) {
        return null
    }

    const converters = []
    for (const pattern of normalizeValue(option.convertPath)) {
        const include = toStringArray(pattern.include)
        const exclude = toStringArray(pattern.exclude)
        const fromRegexp = new RegExp(String(pattern.replace[0]))
        const toStr = String(pattern.replace[1])

        converters.push({
            match: createMatch(include, exclude),
            convert: defineConvert(fromRegexp, toStr),
        })
    }

    return combine(converters)
}

//------------------------------------------------------------------------------
// Public Interface
//------------------------------------------------------------------------------

/**
 * Gets "convertPath" setting.
 *
 * 1. This checks `options` property, then returns it if exists.
 * 2. This checks `settings.node` property, then returns it if exists.
 * 3. This returns a function of identity.
 *
 * @param {RuleContext} context - The rule context.
 * @returns {function} A function which converts a path.
 */
module.exports = function getConvertPath(context) {
    return (
        parse(context.options && context.options[0]) ||
        parse(context.settings && context.settings.node) ||
        identity
    )
}

/**
 * JSON Schema for `convertPath` option.
 */
module.exports.schema = {
    anyOf: [
        {
            type: "object",
            properties: {},
            patternProperties: {
                "^.+$": {
                    type: "array",
                    items: {type: "string"},
                    minItems: 2,
                    maxItems: 2,
                },
            },
            additionalProperties: false,
        },
        {
            type: "array",
            items: {
                type: "object",
                properties: {
                    include: {
                        type: "array",
                        items: {type: "string"},
                        minItems: 1,
                        uniqueItems: true,
                    },
                    exclude: {
                        type: "array",
                        items: {type: "string"},
                        uniqueItems: true,
                    },
                    replace: {
                        type: "array",
                        items: {type: "string"},
                        minItems: 2,
                        maxItems: 2,
                    },
                },
                additionalProperties: false,
                required: ["include", "replace"],
            },
            minItems: 1,
        },
    ],
}