• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

streetsidesoftware / cspell / 15425583305

03 Jun 2025 06:59PM UTC coverage: 92.385% (-0.8%) from 93.187%
15425583305

Pull #7414

github

web-flow
Merge 15d41a737 into 6e3c5d0e0
Pull Request #7414: fix: Add init command to command-line.

12827 of 15127 branches covered (84.8%)

182 of 345 new or added lines in 13 files covered. (52.75%)

4 existing lines in 2 files now uncovered.

15796 of 17098 relevant lines covered (92.39%)

30173.04 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

71.43
/packages/cspell-config-lib/src/CSpellConfigFile.ts
1
import type { CSpellSettings } from '@cspell/cspell-types';
2

3
import type { ValueOf1 } from './types.js';
4
import type { RCfgNode } from './UpdateConfig/CfgTree.js';
5

6
export type { CfgNode } from './UpdateConfig/CfgTree.js';
7

8
export interface CSpellConfigFileReference {
9
    readonly url: URL;
10
}
11

12
export interface ICSpellConfigFile {
13
    /**
14
     * The url of the config file, used to resolve imports.
15
     */
16
    readonly url: URL;
17
    /**
18
     * The settings from the config file.
19
     */
20
    readonly settings: CSpellSettings;
21
    /**
22
     * Indicate that the config file is readonly.
23
     */
24
    readonly?: boolean;
25
    /**
26
     * Indicate that the config file is virtual and not associated with a file on disk.
27
     */
28
    virtual?: boolean;
29
    /**
30
     * Indicate that the config file is remote and not associated with a file on disk.
31
     */
32
    remote?: boolean;
33
}
34

35
export abstract class CSpellConfigFile implements ICSpellConfigFile {
36
    constructor(readonly url: URL) {}
106✔
37

38
    /**
39
     * The settings from the config file.
40
     * Note: this is a copy of the settings from the config file. It should be treated as immutable.
41
     * For performance reasons, it might not be frozen.
42
     */
43
    abstract readonly settings: CSpellSettings;
44

45
    /**
46
     * Helper function to add words to the config file.
47
     * @param words - words to add to the config file.
48
     */
49
    abstract addWords(words: string[]): this;
50

51
    /**
52
     * Tell the config file to remove all comments.
53
     * This is useful when the config file is being serialized and comments are not needed.
54
     * @returns this - the config file.
55
     */
56
    abstract removeAllComments(): this;
57

58
    /**
59
     * Configure the jason.schema for the config file.
60
     * @param schema - The schema to set for the config file.
61
     */
62
    abstract setSchema(schema: string): this;
63

64
    /**
65
     *
66
     * @param key - the field to set the comment for.
67
     * @param comment - the comment to set.
68
     * @param inline - if true, the comment will be set as an inline comment.
69
     */
70
    abstract setComment(key: keyof CSpellSettings, comment: string, inline?: boolean): this;
71

72
    get readonly(): boolean {
73
        return this.settings.readonly || this.url.protocol !== 'file:';
13✔
74
    }
75

76
    get virtual(): boolean {
77
        return false;
1✔
78
    }
79

80
    get remote(): boolean {
81
        return this.url.protocol !== 'file:';
3✔
82
    }
83
}
84

85
type S = CSpellSettings;
86

87
export abstract class MutableCSpellConfigFile extends CSpellConfigFile {
88
    /**
89
     * Helper function to add words to the config file.
90
     * @param words - words to add to the config file.
91
     */
92
    abstract addWords(words: string[]): this;
93

94
    abstract setValue<K extends keyof S>(key: K, value: ValueOf1<S, K>): this;
95
    abstract getValue<K extends keyof S>(key: K): ValueOf1<S, K> | undefined;
96
    abstract getNode<K extends keyof S>(key: K): RCfgNode<ValueOf1<S, K>> | undefined;
97
    abstract getNode<K extends keyof S>(
98
        key: K,
99
        defaultValue: Exclude<ValueOf1<S, K>, undefined>,
100
    ): Exclude<RCfgNode<ValueOf1<S, K>>, undefined>;
101
    abstract getNode<K extends keyof S>(
102
        key: K,
103
        defaultValue: ValueOf1<S, K> | undefined,
104
    ): RCfgNode<ValueOf1<S, K>> | undefined;
105
}
106

107
export abstract class ImplCSpellConfigFile extends CSpellConfigFile {
108
    constructor(
109
        readonly url: URL,
78✔
110
        readonly settings: CSpellSettings,
78✔
111
    ) {
112
        super(url);
78✔
113
    }
114

115
    setSchema(_schema: string): this {
UNCOV
116
        return this;
×
117
    }
118

119
    removeAllComments(): this {
NEW
120
        if (this.readonly) {
×
NEW
121
            throw new Error(`Config file is readonly: ${this.url.href}`);
×
122
        }
123
        // do nothing
NEW
124
        return this;
×
125
    }
126

127
    addWords(words: string[]): this {
128
        if (this.readonly) throw new Error(`Config file is readonly: ${this.url.href}`);
5✔
129
        const w = this.settings.words || [];
4✔
130
        this.settings.words = w;
4✔
131
        addUniqueWordsToListAndSort(w, words);
4✔
132
        return this;
4✔
133
    }
134

135
    setComment(_key: keyof CSpellSettings, _comment: string, _inline?: boolean): this {
NEW
136
        if (this.readonly) throw new Error(`Config file is readonly: ${this.url.href}`);
×
137
        // do nothing
NEW
138
        return this;
×
139
    }
140
}
141

142
/**
143
 * Adds words to a list, sorts the list and makes sure it is unique.
144
 * Note: this method is used to try and preserve comments in the config file.
145
 * @param list - list to be modified
146
 * @param toAdd - words to add
147
 */
148
function addUniqueWordsToListAndSort(list: string[], toAdd: string[]): void {
149
    list.push(...toAdd);
8✔
150
    list.sort();
8✔
151
    for (let i = 1; i < list.length; ++i) {
8✔
152
        if (list[i] === list[i - 1]) {
20✔
153
            list.splice(i, 1);
4✔
154
            --i;
4✔
155
        }
156
    }
157
}
158

159
export function satisfiesCSpellConfigFile(obj: unknown): obj is ICSpellConfigFile {
160
    const r: boolean =
UNCOV
161
        obj instanceof CSpellConfigFile ||
×
162
        (!!obj &&
163
            typeof obj === 'object' &&
164
            'url' in obj &&
165
            obj.url instanceof URL &&
166
            'settings' in obj &&
167
            !!obj.settings &&
168
            typeof obj.settings === 'object');
169
    return r;
×
170
}
171

172
export const cspellConfigFileSchema =
173
    'https://raw.githubusercontent.com/streetsidesoftware/cspell/main/cspell.schema.json';
13✔
174

175
export const __testing__ = {
13✔
176
    addUniqueWordsToListAndSort,
177
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc