source: imaps-frontend/node_modules/@humanwhocodes/config-array/api.js@ 0c6b92a

main
Last change on this file since 0c6b92a was 0c6b92a, checked in by stefan toskovski <stefantoska84@…>, 5 weeks ago

Pred finalna verzija

  • Property mode set to 100644
File size: 30.4 KB
Line 
1'use strict';
2
3var path = require('path');
4var minimatch = require('minimatch');
5var createDebug = require('debug');
6var objectSchema = require('@humanwhocodes/object-schema');
7
8/**
9 * @fileoverview ConfigSchema
10 * @author Nicholas C. Zakas
11 */
12
13//------------------------------------------------------------------------------
14// Helpers
15//------------------------------------------------------------------------------
16
17const NOOP_STRATEGY = {
18 required: false,
19 merge() {
20 return undefined;
21 },
22 validate() { }
23};
24
25//------------------------------------------------------------------------------
26// Exports
27//------------------------------------------------------------------------------
28
29/**
30 * The base schema that every ConfigArray uses.
31 * @type Object
32 */
33const baseSchema = Object.freeze({
34 name: {
35 required: false,
36 merge() {
37 return undefined;
38 },
39 validate(value) {
40 if (typeof value !== 'string') {
41 throw new TypeError('Property must be a string.');
42 }
43 }
44 },
45 files: NOOP_STRATEGY,
46 ignores: NOOP_STRATEGY
47});
48
49/**
50 * @fileoverview ConfigSchema
51 * @author Nicholas C. Zakas
52 */
53
54//------------------------------------------------------------------------------
55// Helpers
56//------------------------------------------------------------------------------
57
58/**
59 * Asserts that a given value is an array.
60 * @param {*} value The value to check.
61 * @returns {void}
62 * @throws {TypeError} When the value is not an array.
63 */
64function assertIsArray(value) {
65 if (!Array.isArray(value)) {
66 throw new TypeError('Expected value to be an array.');
67 }
68}
69
70/**
71 * Asserts that a given value is an array containing only strings and functions.
72 * @param {*} value The value to check.
73 * @returns {void}
74 * @throws {TypeError} When the value is not an array of strings and functions.
75 */
76function assertIsArrayOfStringsAndFunctions(value, name) {
77 assertIsArray(value);
78
79 if (value.some(item => typeof item !== 'string' && typeof item !== 'function')) {
80 throw new TypeError('Expected array to only contain strings and functions.');
81 }
82}
83
84/**
85 * Asserts that a given value is a non-empty array.
86 * @param {*} value The value to check.
87 * @returns {void}
88 * @throws {TypeError} When the value is not an array or an empty array.
89 */
90function assertIsNonEmptyArray(value) {
91 if (!Array.isArray(value) || value.length === 0) {
92 throw new TypeError('Expected value to be a non-empty array.');
93 }
94}
95
96//------------------------------------------------------------------------------
97// Exports
98//------------------------------------------------------------------------------
99
100/**
101 * The schema for `files` and `ignores` that every ConfigArray uses.
102 * @type Object
103 */
104const filesAndIgnoresSchema = Object.freeze({
105 files: {
106 required: false,
107 merge() {
108 return undefined;
109 },
110 validate(value) {
111
112 // first check if it's an array
113 assertIsNonEmptyArray(value);
114
115 // then check each member
116 value.forEach(item => {
117 if (Array.isArray(item)) {
118 assertIsArrayOfStringsAndFunctions(item);
119 } else if (typeof item !== 'string' && typeof item !== 'function') {
120 throw new TypeError('Items must be a string, a function, or an array of strings and functions.');
121 }
122 });
123
124 }
125 },
126 ignores: {
127 required: false,
128 merge() {
129 return undefined;
130 },
131 validate: assertIsArrayOfStringsAndFunctions
132 }
133});
134
135/**
136 * @fileoverview ConfigArray
137 * @author Nicholas C. Zakas
138 */
139
140
141//------------------------------------------------------------------------------
142// Helpers
143//------------------------------------------------------------------------------
144
145const Minimatch = minimatch.Minimatch;
146const minimatchCache = new Map();
147const negatedMinimatchCache = new Map();
148const debug = createDebug('@hwc/config-array');
149
150const MINIMATCH_OPTIONS = {
151 // matchBase: true,
152 dot: true
153};
154
155const CONFIG_TYPES = new Set(['array', 'function']);
156
157/**
158 * Fields that are considered metadata and not part of the config object.
159 */
160const META_FIELDS = new Set(['name']);
161
162const FILES_AND_IGNORES_SCHEMA = new objectSchema.ObjectSchema(filesAndIgnoresSchema);
163
164/**
165 * Wrapper error for config validation errors that adds a name to the front of the
166 * error message.
167 */
168class ConfigError extends Error {
169
170 /**
171 * Creates a new instance.
172 * @param {string} name The config object name causing the error.
173 * @param {number} index The index of the config object in the array.
174 * @param {Error} source The source error.
175 */
176 constructor(name, index, { cause, message }) {
177
178
179 const finalMessage = message || cause.message;
180
181 super(`Config ${name}: ${finalMessage}`, { cause });
182
183 // copy over custom properties that aren't represented
184 if (cause) {
185 for (const key of Object.keys(cause)) {
186 if (!(key in this)) {
187 this[key] = cause[key];
188 }
189 }
190 }
191
192 /**
193 * The name of the error.
194 * @type {string}
195 * @readonly
196 */
197 this.name = 'ConfigError';
198
199 /**
200 * The index of the config object in the array.
201 * @type {number}
202 * @readonly
203 */
204 this.index = index;
205 }
206}
207
208/**
209 * Gets the name of a config object.
210 * @param {object} config The config object to get the name of.
211 * @returns {string} The name of the config object.
212 */
213function getConfigName(config) {
214 if (config && typeof config.name === 'string' && config.name) {
215 return `"${config.name}"`;
216 }
217
218 return '(unnamed)';
219}
220
221/**
222 * Rethrows a config error with additional information about the config object.
223 * @param {object} config The config object to get the name of.
224 * @param {number} index The index of the config object in the array.
225 * @param {Error} error The error to rethrow.
226 * @throws {ConfigError} When the error is rethrown for a config.
227 */
228function rethrowConfigError(config, index, error) {
229 const configName = getConfigName(config);
230 throw new ConfigError(configName, index, error);
231}
232
233/**
234 * Shorthand for checking if a value is a string.
235 * @param {any} value The value to check.
236 * @returns {boolean} True if a string, false if not.
237 */
238function isString(value) {
239 return typeof value === 'string';
240}
241
242/**
243 * Creates a function that asserts that the config is valid
244 * during normalization. This checks that the config is not nullish
245 * and that files and ignores keys of a config object are valid as per base schema.
246 * @param {Object} config The config object to check.
247 * @param {number} index The index of the config object in the array.
248 * @returns {void}
249 * @throws {ConfigError} If the files and ignores keys of a config object are not valid.
250 */
251function assertValidBaseConfig(config, index) {
252
253 if (config === null) {
254 throw new ConfigError(getConfigName(config), index, { message: 'Unexpected null config.' });
255 }
256
257 if (config === undefined) {
258 throw new ConfigError(getConfigName(config), index, { message: 'Unexpected undefined config.' });
259 }
260
261 if (typeof config !== 'object') {
262 throw new ConfigError(getConfigName(config), index, { message: 'Unexpected non-object config.' });
263 }
264
265 const validateConfig = { };
266
267 if ('files' in config) {
268 validateConfig.files = config.files;
269 }
270
271 if ('ignores' in config) {
272 validateConfig.ignores = config.ignores;
273 }
274
275 try {
276 FILES_AND_IGNORES_SCHEMA.validate(validateConfig);
277 } catch (validationError) {
278 rethrowConfigError(config, index, { cause: validationError });
279 }
280}
281
282/**
283 * Wrapper around minimatch that caches minimatch patterns for
284 * faster matching speed over multiple file path evaluations.
285 * @param {string} filepath The file path to match.
286 * @param {string} pattern The glob pattern to match against.
287 * @param {object} options The minimatch options to use.
288 * @returns
289 */
290function doMatch(filepath, pattern, options = {}) {
291
292 let cache = minimatchCache;
293
294 if (options.flipNegate) {
295 cache = negatedMinimatchCache;
296 }
297
298 let matcher = cache.get(pattern);
299
300 if (!matcher) {
301 matcher = new Minimatch(pattern, Object.assign({}, MINIMATCH_OPTIONS, options));
302 cache.set(pattern, matcher);
303 }
304
305 return matcher.match(filepath);
306}
307
308/**
309 * Normalizes a `ConfigArray` by flattening it and executing any functions
310 * that are found inside.
311 * @param {Array} items The items in a `ConfigArray`.
312 * @param {Object} context The context object to pass into any function
313 * found.
314 * @param {Array<string>} extraConfigTypes The config types to check.
315 * @returns {Promise<Array>} A flattened array containing only config objects.
316 * @throws {TypeError} When a config function returns a function.
317 */
318async function normalize(items, context, extraConfigTypes) {
319
320 const allowFunctions = extraConfigTypes.includes('function');
321 const allowArrays = extraConfigTypes.includes('array');
322
323 async function* flatTraverse(array) {
324 for (let item of array) {
325 if (typeof item === 'function') {
326 if (!allowFunctions) {
327 throw new TypeError('Unexpected function.');
328 }
329
330 item = item(context);
331 if (item.then) {
332 item = await item;
333 }
334 }
335
336 if (Array.isArray(item)) {
337 if (!allowArrays) {
338 throw new TypeError('Unexpected array.');
339 }
340 yield* flatTraverse(item);
341 } else if (typeof item === 'function') {
342 throw new TypeError('A config function can only return an object or array.');
343 } else {
344 yield item;
345 }
346 }
347 }
348
349 /*
350 * Async iterables cannot be used with the spread operator, so we need to manually
351 * create the array to return.
352 */
353 const asyncIterable = await flatTraverse(items);
354 const configs = [];
355
356 for await (const config of asyncIterable) {
357 configs.push(config);
358 }
359
360 return configs;
361}
362
363/**
364 * Normalizes a `ConfigArray` by flattening it and executing any functions
365 * that are found inside.
366 * @param {Array} items The items in a `ConfigArray`.
367 * @param {Object} context The context object to pass into any function
368 * found.
369 * @param {Array<string>} extraConfigTypes The config types to check.
370 * @returns {Array} A flattened array containing only config objects.
371 * @throws {TypeError} When a config function returns a function.
372 */
373function normalizeSync(items, context, extraConfigTypes) {
374
375 const allowFunctions = extraConfigTypes.includes('function');
376 const allowArrays = extraConfigTypes.includes('array');
377
378 function* flatTraverse(array) {
379 for (let item of array) {
380 if (typeof item === 'function') {
381
382 if (!allowFunctions) {
383 throw new TypeError('Unexpected function.');
384 }
385
386 item = item(context);
387 if (item.then) {
388 throw new TypeError('Async config functions are not supported.');
389 }
390 }
391
392 if (Array.isArray(item)) {
393
394 if (!allowArrays) {
395 throw new TypeError('Unexpected array.');
396 }
397
398 yield* flatTraverse(item);
399 } else if (typeof item === 'function') {
400 throw new TypeError('A config function can only return an object or array.');
401 } else {
402 yield item;
403 }
404 }
405 }
406
407 return [...flatTraverse(items)];
408}
409
410/**
411 * Determines if a given file path should be ignored based on the given
412 * matcher.
413 * @param {Array<string|() => boolean>} ignores The ignore patterns to check.
414 * @param {string} filePath The absolute path of the file to check.
415 * @param {string} relativeFilePath The relative path of the file to check.
416 * @returns {boolean} True if the path should be ignored and false if not.
417 */
418function shouldIgnorePath(ignores, filePath, relativeFilePath) {
419
420 // all files outside of the basePath are ignored
421 if (relativeFilePath.startsWith('..')) {
422 return true;
423 }
424
425 return ignores.reduce((ignored, matcher) => {
426
427 if (!ignored) {
428
429 if (typeof matcher === 'function') {
430 return matcher(filePath);
431 }
432
433 // don't check negated patterns because we're not ignored yet
434 if (!matcher.startsWith('!')) {
435 return doMatch(relativeFilePath, matcher);
436 }
437
438 // otherwise we're still not ignored
439 return false;
440
441 }
442
443 // only need to check negated patterns because we're ignored
444 if (typeof matcher === 'string' && matcher.startsWith('!')) {
445 return !doMatch(relativeFilePath, matcher, {
446 flipNegate: true
447 });
448 }
449
450 return ignored;
451
452 }, false);
453
454}
455
456/**
457 * Determines if a given file path is matched by a config based on
458 * `ignores` only.
459 * @param {string} filePath The absolute file path to check.
460 * @param {string} basePath The base path for the config.
461 * @param {Object} config The config object to check.
462 * @returns {boolean} True if the file path is matched by the config,
463 * false if not.
464 */
465function pathMatchesIgnores(filePath, basePath, config) {
466
467 /*
468 * For both files and ignores, functions are passed the absolute
469 * file path while strings are compared against the relative
470 * file path.
471 */
472 const relativeFilePath = path.relative(basePath, filePath);
473
474 return Object.keys(config).filter(key => !META_FIELDS.has(key)).length > 1 &&
475 !shouldIgnorePath(config.ignores, filePath, relativeFilePath);
476}
477
478
479/**
480 * Determines if a given file path is matched by a config. If the config
481 * has no `files` field, then it matches; otherwise, if a `files` field
482 * is present then we match the globs in `files` and exclude any globs in
483 * `ignores`.
484 * @param {string} filePath The absolute file path to check.
485 * @param {string} basePath The base path for the config.
486 * @param {Object} config The config object to check.
487 * @returns {boolean} True if the file path is matched by the config,
488 * false if not.
489 */
490function pathMatches(filePath, basePath, config) {
491
492 /*
493 * For both files and ignores, functions are passed the absolute
494 * file path while strings are compared against the relative
495 * file path.
496 */
497 const relativeFilePath = path.relative(basePath, filePath);
498
499 // match both strings and functions
500 const match = pattern => {
501
502 if (isString(pattern)) {
503 return doMatch(relativeFilePath, pattern);
504 }
505
506 if (typeof pattern === 'function') {
507 return pattern(filePath);
508 }
509
510 throw new TypeError(`Unexpected matcher type ${pattern}.`);
511 };
512
513 // check for all matches to config.files
514 let filePathMatchesPattern = config.files.some(pattern => {
515 if (Array.isArray(pattern)) {
516 return pattern.every(match);
517 }
518
519 return match(pattern);
520 });
521
522 /*
523 * If the file path matches the config.files patterns, then check to see
524 * if there are any files to ignore.
525 */
526 if (filePathMatchesPattern && config.ignores) {
527 filePathMatchesPattern = !shouldIgnorePath(config.ignores, filePath, relativeFilePath);
528 }
529
530 return filePathMatchesPattern;
531}
532
533/**
534 * Ensures that a ConfigArray has been normalized.
535 * @param {ConfigArray} configArray The ConfigArray to check.
536 * @returns {void}
537 * @throws {Error} When the `ConfigArray` is not normalized.
538 */
539function assertNormalized(configArray) {
540 // TODO: Throw more verbose error
541 if (!configArray.isNormalized()) {
542 throw new Error('ConfigArray must be normalized to perform this operation.');
543 }
544}
545
546/**
547 * Ensures that config types are valid.
548 * @param {Array<string>} extraConfigTypes The config types to check.
549 * @returns {void}
550 * @throws {Error} When the config types array is invalid.
551 */
552function assertExtraConfigTypes(extraConfigTypes) {
553 if (extraConfigTypes.length > 2) {
554 throw new TypeError('configTypes must be an array with at most two items.');
555 }
556
557 for (const configType of extraConfigTypes) {
558 if (!CONFIG_TYPES.has(configType)) {
559 throw new TypeError(`Unexpected config type "${configType}" found. Expected one of: "object", "array", "function".`);
560 }
561 }
562}
563
564//------------------------------------------------------------------------------
565// Public Interface
566//------------------------------------------------------------------------------
567
568const ConfigArraySymbol = {
569 isNormalized: Symbol('isNormalized'),
570 configCache: Symbol('configCache'),
571 schema: Symbol('schema'),
572 finalizeConfig: Symbol('finalizeConfig'),
573 preprocessConfig: Symbol('preprocessConfig')
574};
575
576// used to store calculate data for faster lookup
577const dataCache = new WeakMap();
578
579/**
580 * Represents an array of config objects and provides method for working with
581 * those config objects.
582 */
583class ConfigArray extends Array {
584
585 /**
586 * Creates a new instance of ConfigArray.
587 * @param {Iterable|Function|Object} configs An iterable yielding config
588 * objects, or a config function, or a config object.
589 * @param {string} [options.basePath=""] The path of the config file
590 * @param {boolean} [options.normalized=false] Flag indicating if the
591 * configs have already been normalized.
592 * @param {Object} [options.schema] The additional schema
593 * definitions to use for the ConfigArray schema.
594 * @param {Array<string>} [options.configTypes] List of config types supported.
595 */
596 constructor(configs, {
597 basePath = '',
598 normalized = false,
599 schema: customSchema,
600 extraConfigTypes = []
601 } = {}
602 ) {
603 super();
604
605 /**
606 * Tracks if the array has been normalized.
607 * @property isNormalized
608 * @type {boolean}
609 * @private
610 */
611 this[ConfigArraySymbol.isNormalized] = normalized;
612
613 /**
614 * The schema used for validating and merging configs.
615 * @property schema
616 * @type ObjectSchema
617 * @private
618 */
619 this[ConfigArraySymbol.schema] = new objectSchema.ObjectSchema(
620 Object.assign({}, customSchema, baseSchema)
621 );
622
623 /**
624 * The path of the config file that this array was loaded from.
625 * This is used to calculate filename matches.
626 * @property basePath
627 * @type {string}
628 */
629 this.basePath = basePath;
630
631 assertExtraConfigTypes(extraConfigTypes);
632
633 /**
634 * The supported config types.
635 * @property configTypes
636 * @type {Array<string>}
637 */
638 this.extraConfigTypes = Object.freeze([...extraConfigTypes]);
639
640 /**
641 * A cache to store calculated configs for faster repeat lookup.
642 * @property configCache
643 * @type {Map<string, Object>}
644 * @private
645 */
646 this[ConfigArraySymbol.configCache] = new Map();
647
648 // init cache
649 dataCache.set(this, {
650 explicitMatches: new Map(),
651 directoryMatches: new Map(),
652 files: undefined,
653 ignores: undefined
654 });
655
656 // load the configs into this array
657 if (Array.isArray(configs)) {
658 this.push(...configs);
659 } else {
660 this.push(configs);
661 }
662
663 }
664
665 /**
666 * Prevent normal array methods from creating a new `ConfigArray` instance.
667 * This is to ensure that methods such as `slice()` won't try to create a
668 * new instance of `ConfigArray` behind the scenes as doing so may throw
669 * an error due to the different constructor signature.
670 * @returns {Function} The `Array` constructor.
671 */
672 static get [Symbol.species]() {
673 return Array;
674 }
675
676 /**
677 * Returns the `files` globs from every config object in the array.
678 * This can be used to determine which files will be matched by a
679 * config array or to use as a glob pattern when no patterns are provided
680 * for a command line interface.
681 * @returns {Array<string|Function>} An array of matchers.
682 */
683 get files() {
684
685 assertNormalized(this);
686
687 // if this data has been cached, retrieve it
688 const cache = dataCache.get(this);
689
690 if (cache.files) {
691 return cache.files;
692 }
693
694 // otherwise calculate it
695
696 const result = [];
697
698 for (const config of this) {
699 if (config.files) {
700 config.files.forEach(filePattern => {
701 result.push(filePattern);
702 });
703 }
704 }
705
706 // store result
707 cache.files = result;
708 dataCache.set(this, cache);
709
710 return result;
711 }
712
713 /**
714 * Returns ignore matchers that should always be ignored regardless of
715 * the matching `files` fields in any configs. This is necessary to mimic
716 * the behavior of things like .gitignore and .eslintignore, allowing a
717 * globbing operation to be faster.
718 * @returns {string[]} An array of string patterns and functions to be ignored.
719 */
720 get ignores() {
721
722 assertNormalized(this);
723
724 // if this data has been cached, retrieve it
725 const cache = dataCache.get(this);
726
727 if (cache.ignores) {
728 return cache.ignores;
729 }
730
731 // otherwise calculate it
732
733 const result = [];
734
735 for (const config of this) {
736
737 /*
738 * We only count ignores if there are no other keys in the object.
739 * In this case, it acts list a globally ignored pattern. If there
740 * are additional keys, then ignores act like exclusions.
741 */
742 if (config.ignores && Object.keys(config).filter(key => !META_FIELDS.has(key)).length === 1) {
743 result.push(...config.ignores);
744 }
745 }
746
747 // store result
748 cache.ignores = result;
749 dataCache.set(this, cache);
750
751 return result;
752 }
753
754 /**
755 * Indicates if the config array has been normalized.
756 * @returns {boolean} True if the config array is normalized, false if not.
757 */
758 isNormalized() {
759 return this[ConfigArraySymbol.isNormalized];
760 }
761
762 /**
763 * Normalizes a config array by flattening embedded arrays and executing
764 * config functions.
765 * @param {ConfigContext} context The context object for config functions.
766 * @returns {Promise<ConfigArray>} The current ConfigArray instance.
767 */
768 async normalize(context = {}) {
769
770 if (!this.isNormalized()) {
771 const normalizedConfigs = await normalize(this, context, this.extraConfigTypes);
772 this.length = 0;
773 this.push(...normalizedConfigs.map(this[ConfigArraySymbol.preprocessConfig].bind(this)));
774 this.forEach(assertValidBaseConfig);
775 this[ConfigArraySymbol.isNormalized] = true;
776
777 // prevent further changes
778 Object.freeze(this);
779 }
780
781 return this;
782 }
783
784 /**
785 * Normalizes a config array by flattening embedded arrays and executing
786 * config functions.
787 * @param {ConfigContext} context The context object for config functions.
788 * @returns {ConfigArray} The current ConfigArray instance.
789 */
790 normalizeSync(context = {}) {
791
792 if (!this.isNormalized()) {
793 const normalizedConfigs = normalizeSync(this, context, this.extraConfigTypes);
794 this.length = 0;
795 this.push(...normalizedConfigs.map(this[ConfigArraySymbol.preprocessConfig].bind(this)));
796 this.forEach(assertValidBaseConfig);
797 this[ConfigArraySymbol.isNormalized] = true;
798
799 // prevent further changes
800 Object.freeze(this);
801 }
802
803 return this;
804 }
805
806 /**
807 * Finalizes the state of a config before being cached and returned by
808 * `getConfig()`. Does nothing by default but is provided to be
809 * overridden by subclasses as necessary.
810 * @param {Object} config The config to finalize.
811 * @returns {Object} The finalized config.
812 */
813 [ConfigArraySymbol.finalizeConfig](config) {
814 return config;
815 }
816
817 /**
818 * Preprocesses a config during the normalization process. This is the
819 * method to override if you want to convert an array item before it is
820 * validated for the first time. For example, if you want to replace a
821 * string with an object, this is the method to override.
822 * @param {Object} config The config to preprocess.
823 * @returns {Object} The config to use in place of the argument.
824 */
825 [ConfigArraySymbol.preprocessConfig](config) {
826 return config;
827 }
828
829 /**
830 * Determines if a given file path explicitly matches a `files` entry
831 * and also doesn't match an `ignores` entry. Configs that don't have
832 * a `files` property are not considered an explicit match.
833 * @param {string} filePath The complete path of a file to check.
834 * @returns {boolean} True if the file path matches a `files` entry
835 * or false if not.
836 */
837 isExplicitMatch(filePath) {
838
839 assertNormalized(this);
840
841 const cache = dataCache.get(this);
842
843 // first check the cache to avoid duplicate work
844 let result = cache.explicitMatches.get(filePath);
845
846 if (typeof result == 'boolean') {
847 return result;
848 }
849
850 // TODO: Maybe move elsewhere? Maybe combine with getConfig() logic?
851 const relativeFilePath = path.relative(this.basePath, filePath);
852
853 if (shouldIgnorePath(this.ignores, filePath, relativeFilePath)) {
854 debug(`Ignoring ${filePath}`);
855
856 // cache and return result
857 cache.explicitMatches.set(filePath, false);
858 return false;
859 }
860
861 // filePath isn't automatically ignored, so try to find a match
862
863 for (const config of this) {
864
865 if (!config.files) {
866 continue;
867 }
868
869 if (pathMatches(filePath, this.basePath, config)) {
870 debug(`Matching config found for ${filePath}`);
871 cache.explicitMatches.set(filePath, true);
872 return true;
873 }
874 }
875
876 return false;
877 }
878
879 /**
880 * Returns the config object for a given file path.
881 * @param {string} filePath The complete path of a file to get a config for.
882 * @returns {Object} The config object for this file.
883 */
884 getConfig(filePath) {
885
886 assertNormalized(this);
887
888 const cache = this[ConfigArraySymbol.configCache];
889
890 // first check the cache for a filename match to avoid duplicate work
891 if (cache.has(filePath)) {
892 return cache.get(filePath);
893 }
894
895 let finalConfig;
896
897 // next check to see if the file should be ignored
898
899 // check if this should be ignored due to its directory
900 if (this.isDirectoryIgnored(path.dirname(filePath))) {
901 debug(`Ignoring ${filePath} based on directory pattern`);
902
903 // cache and return result - finalConfig is undefined at this point
904 cache.set(filePath, finalConfig);
905 return finalConfig;
906 }
907
908 // TODO: Maybe move elsewhere?
909 const relativeFilePath = path.relative(this.basePath, filePath);
910
911 if (shouldIgnorePath(this.ignores, filePath, relativeFilePath)) {
912 debug(`Ignoring ${filePath} based on file pattern`);
913
914 // cache and return result - finalConfig is undefined at this point
915 cache.set(filePath, finalConfig);
916 return finalConfig;
917 }
918
919 // filePath isn't automatically ignored, so try to construct config
920
921 const matchingConfigIndices = [];
922 let matchFound = false;
923 const universalPattern = /\/\*{1,2}$/;
924
925 this.forEach((config, index) => {
926
927 if (!config.files) {
928
929 if (!config.ignores) {
930 debug(`Anonymous universal config found for ${filePath}`);
931 matchingConfigIndices.push(index);
932 return;
933 }
934
935 if (pathMatchesIgnores(filePath, this.basePath, config)) {
936 debug(`Matching config found for ${filePath} (based on ignores: ${config.ignores})`);
937 matchingConfigIndices.push(index);
938 return;
939 }
940
941 debug(`Skipped config found for ${filePath} (based on ignores: ${config.ignores})`);
942 return;
943 }
944
945 /*
946 * If a config has a files pattern ending in /** or /*, and the
947 * filePath only matches those patterns, then the config is only
948 * applied if there is another config where the filePath matches
949 * a file with a specific extensions such as *.js.
950 */
951
952 const universalFiles = config.files.filter(
953 pattern => universalPattern.test(pattern)
954 );
955
956 // universal patterns were found so we need to check the config twice
957 if (universalFiles.length) {
958
959 debug('Universal files patterns found. Checking carefully.');
960
961 const nonUniversalFiles = config.files.filter(
962 pattern => !universalPattern.test(pattern)
963 );
964
965 // check that the config matches without the non-universal files first
966 if (
967 nonUniversalFiles.length &&
968 pathMatches(
969 filePath, this.basePath,
970 { files: nonUniversalFiles, ignores: config.ignores }
971 )
972 ) {
973 debug(`Matching config found for ${filePath}`);
974 matchingConfigIndices.push(index);
975 matchFound = true;
976 return;
977 }
978
979 // if there wasn't a match then check if it matches with universal files
980 if (
981 universalFiles.length &&
982 pathMatches(
983 filePath, this.basePath,
984 { files: universalFiles, ignores: config.ignores }
985 )
986 ) {
987 debug(`Matching config found for ${filePath}`);
988 matchingConfigIndices.push(index);
989 return;
990 }
991
992 // if we make here, then there was no match
993 return;
994 }
995
996 // the normal case
997 if (pathMatches(filePath, this.basePath, config)) {
998 debug(`Matching config found for ${filePath}`);
999 matchingConfigIndices.push(index);
1000 matchFound = true;
1001 return;
1002 }
1003
1004 });
1005
1006 // if matching both files and ignores, there will be no config to create
1007 if (!matchFound) {
1008 debug(`No matching configs found for ${filePath}`);
1009
1010 // cache and return result - finalConfig is undefined at this point
1011 cache.set(filePath, finalConfig);
1012 return finalConfig;
1013 }
1014
1015 // check to see if there is a config cached by indices
1016 finalConfig = cache.get(matchingConfigIndices.toString());
1017
1018 if (finalConfig) {
1019
1020 // also store for filename for faster lookup next time
1021 cache.set(filePath, finalConfig);
1022
1023 return finalConfig;
1024 }
1025
1026 // otherwise construct the config
1027
1028 finalConfig = matchingConfigIndices.reduce((result, index) => {
1029 try {
1030 return this[ConfigArraySymbol.schema].merge(result, this[index]);
1031 } catch (validationError) {
1032 rethrowConfigError(this[index], index, { cause: validationError});
1033 }
1034 }, {}, this);
1035
1036 finalConfig = this[ConfigArraySymbol.finalizeConfig](finalConfig);
1037
1038 cache.set(filePath, finalConfig);
1039 cache.set(matchingConfigIndices.toString(), finalConfig);
1040
1041 return finalConfig;
1042 }
1043
1044 /**
1045 * Determines if the given filepath is ignored based on the configs.
1046 * @param {string} filePath The complete path of a file to check.
1047 * @returns {boolean} True if the path is ignored, false if not.
1048 * @deprecated Use `isFileIgnored` instead.
1049 */
1050 isIgnored(filePath) {
1051 return this.isFileIgnored(filePath);
1052 }
1053
1054 /**
1055 * Determines if the given filepath is ignored based on the configs.
1056 * @param {string} filePath The complete path of a file to check.
1057 * @returns {boolean} True if the path is ignored, false if not.
1058 */
1059 isFileIgnored(filePath) {
1060 return this.getConfig(filePath) === undefined;
1061 }
1062
1063 /**
1064 * Determines if the given directory is ignored based on the configs.
1065 * This checks only default `ignores` that don't have `files` in the
1066 * same config. A pattern such as `/foo` be considered to ignore the directory
1067 * while a pattern such as `/foo/**` is not considered to ignore the
1068 * directory because it is matching files.
1069 * @param {string} directoryPath The complete path of a directory to check.
1070 * @returns {boolean} True if the directory is ignored, false if not. Will
1071 * return true for any directory that is not inside of `basePath`.
1072 * @throws {Error} When the `ConfigArray` is not normalized.
1073 */
1074 isDirectoryIgnored(directoryPath) {
1075
1076 assertNormalized(this);
1077
1078 const relativeDirectoryPath = path.relative(this.basePath, directoryPath)
1079 .replace(/\\/g, '/');
1080
1081 if (relativeDirectoryPath.startsWith('..')) {
1082 return true;
1083 }
1084
1085 // first check the cache
1086 const cache = dataCache.get(this).directoryMatches;
1087
1088 if (cache.has(relativeDirectoryPath)) {
1089 return cache.get(relativeDirectoryPath);
1090 }
1091
1092 const directoryParts = relativeDirectoryPath.split('/');
1093 let relativeDirectoryToCheck = '';
1094 let result = false;
1095
1096 /*
1097 * In order to get the correct gitignore-style ignores, where an
1098 * ignored parent directory cannot have any descendants unignored,
1099 * we need to check every directory starting at the parent all
1100 * the way down to the actual requested directory.
1101 *
1102 * We aggressively cache all of this info to make sure we don't
1103 * have to recalculate everything for every call.
1104 */
1105 do {
1106
1107 relativeDirectoryToCheck += directoryParts.shift() + '/';
1108
1109 result = shouldIgnorePath(
1110 this.ignores,
1111 path.join(this.basePath, relativeDirectoryToCheck),
1112 relativeDirectoryToCheck
1113 );
1114
1115 cache.set(relativeDirectoryToCheck, result);
1116
1117 } while (!result && directoryParts.length);
1118
1119 // also cache the result for the requested path
1120 cache.set(relativeDirectoryPath, result);
1121
1122 return result;
1123 }
1124
1125}
1126
1127exports.ConfigArray = ConfigArray;
1128exports.ConfigArraySymbol = ConfigArraySymbol;
Note: See TracBrowser for help on using the repository browser.