ilib-lint 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/README.md +413 -66
  2. package/docs/AnsiConsoleFormatter.html +25 -131
  3. package/docs/ConfigBasedFormatter.html +368 -0
  4. package/docs/DirItem.html +718 -0
  5. package/docs/DirItem.js.html +162 -0
  6. package/docs/FileType.html +493 -0
  7. package/docs/FileType.js.html +215 -0
  8. package/docs/FormatterManager.html +744 -0
  9. package/docs/FormatterManager.js.html +195 -0
  10. package/docs/ParserManager.html +607 -0
  11. package/docs/ParserManager.js.html +168 -0
  12. package/docs/PluginManager.html +451 -49
  13. package/docs/PluginManager.js.html +206 -16
  14. package/docs/Project.html +2069 -0
  15. package/docs/Project.js.html +400 -0
  16. package/docs/ResourceICUPlurals.html +15 -9
  17. package/docs/ResourceMatcher.html +2192 -0
  18. package/docs/ResourceQuoteStyle.html +15 -9
  19. package/docs/ResourceRegExpChecker.html +4 -4
  20. package/docs/ResourceUniqueKeys.html +64 -8
  21. package/docs/RuleManager.html +1592 -0
  22. package/docs/RuleManager.js.html +352 -0
  23. package/docs/RuleSet.html +247 -51
  24. package/docs/RuleSet.js.html +49 -32
  25. package/docs/SourceFile.html +164 -258
  26. package/docs/SourceFile.js.html +67 -75
  27. package/docs/XliffParser.html +28 -28
  28. package/docs/XliffPlugin.html +21 -181
  29. package/docs/formatters_AnsiConsoleFormatter.js.html +11 -17
  30. package/docs/formatters_ConfigBasedFormatter.js.html +160 -0
  31. package/docs/global.html +122 -46
  32. package/docs/ilibLint.md +1497 -493
  33. package/docs/index.html +5 -2
  34. package/docs/plugins_XliffParser.js.html +18 -8
  35. package/docs/plugins_XliffPlugin.js.html +10 -20
  36. package/docs/rules_ResourceICUPlurals.js.html +11 -6
  37. package/docs/rules_ResourceMatcher.js.html +253 -0
  38. package/docs/rules_ResourceQuoteStyle.js.html +19 -8
  39. package/docs/rules_ResourceRegExpChecker.js.html +4 -5
  40. package/docs/rules_ResourceSourceChecker.js.html +252 -0
  41. package/docs/rules_ResourceTargetChecker.js.html +252 -0
  42. package/docs/rules_ResourceUniqueKeys.js.html +12 -5
  43. package/docs/rules_SourceFileChecker.js.html +252 -0
  44. package/docs/scripts/collapse.js +19 -0
  45. package/docs/scripts/commonNav.js +28 -0
  46. package/docs/scripts/search.js +16 -0
  47. package/docs/styles/jsdoc.css +13 -2
  48. package/docs/styles/prettify.css +1 -0
  49. package/docs/walk.js.html +49 -37
  50. package/package.json +15 -13
  51. package/src/DirItem.js +90 -0
  52. package/src/FileType.js +144 -0
  53. package/src/FormatterManager.js +124 -0
  54. package/src/ParserManager.js +97 -0
  55. package/src/PluginManager.js +201 -14
  56. package/src/Project.js +328 -0
  57. package/src/RuleManager.js +281 -0
  58. package/src/RuleSet.js +44 -30
  59. package/src/SourceFile.js +62 -73
  60. package/src/formatters/AnsiConsoleFormatter.js +6 -15
  61. package/src/formatters/ConfigBasedFormatter.js +88 -0
  62. package/src/index.js +128 -76
  63. package/src/plugins/XliffParser.js +13 -6
  64. package/src/plugins/XliffPlugin.js +5 -18
  65. package/src/rules/ResourceICUPlurals.js +6 -4
  66. package/src/rules/ResourceMatcher.js +181 -0
  67. package/src/rules/ResourceQuoteStyle.js +14 -6
  68. package/src/rules/{ResourceRegExpChecker.js → ResourceSourceChecker.js} +9 -8
  69. package/src/rules/ResourceTargetChecker.js +180 -0
  70. package/src/rules/ResourceUniqueKeys.js +7 -3
  71. package/src/rules/SourceFileChecker.js +180 -0
  72. package/src/rules/utils.js +31 -1
  73. package/src/walk.js +44 -35
  74. package/src/Formatter.js +0 -66
  75. package/src/FormatterFactory.js +0 -41
  76. package/src/Parser.js +0 -53
  77. package/src/ParserFactory.js +0 -41
  78. package/src/Plugin.js +0 -99
  79. package/src/Result.js +0 -62
  80. package/src/Rule.js +0 -162
package/README.md CHANGED
@@ -4,15 +4,25 @@ A static analysis linter for many types of source files that looks for i18n prob
4
4
 
5
5
  This i18n linter differs from other static linters in the following ways:
6
6
 
7
- * it can recognize the locale of files from the path name of files or from within
8
- the file itself, and applies locale-sensitive rules. For example, you can apply
9
- a rule that checks that the translations in a resource file of a plural resource
10
- contain the correct set of plural categories for the target language.
11
- * it can apply different rulesets to different sets of files. This is useful for
7
+ * It can apply different rulesets to different sets of files. This is useful for
12
8
  a number of reasons:
13
9
  * when linting a mono-repo that has different subprojects inside of it
14
10
  and each subproject needs different rules applied to its files
15
- * when different file types need different rulesets
11
+ * when different sets of files need different rulesets, even within the same file type
12
+ * It can handle any file type
13
+ * most linters are specific to a programming language and its related files. This linter
14
+ can read any type of file and apply the appropriate set of rules.
15
+ * Rules can be locale-sensitive
16
+ * most linters apply the same rules blindly to all files, regardless of the locale
17
+ * for resource files, it can apply the appropriate locale for each resource individually
18
+ * It can recognize the locale of files from the path name of files
19
+ * this allows it to apply the locale-sensitive rules automatically. For example, you can apply
20
+ a rule that checks that the translations in a resource file of a plural resource
21
+ contain the correct set of plural categories for the target language.
22
+ * It can load plugins
23
+ * Parsers - you can add parsers for new programming languages or resource file types
24
+ * Formatters - you can make the output look exactly the way you want
25
+ * Rules - you can add new rules declaratively or programmatically
16
26
 
17
27
  ## Installation
18
28
 
@@ -38,31 +48,14 @@ is written with ESM modules.
38
48
  ## Quick Start
39
49
 
40
50
  Running ilib-lint is easy. Just change your directory to the top level directory
41
- of your project and run it with no parameters and no configuration file. When
42
- there are no parameters and no configuration file, it will do all default
43
- behaviours, which for some projects is sufficient.
51
+ of your project and run it with no parameters and no configuration file. It will
52
+ do all default behaviours and apply the default rules, which for some projects
53
+ is sufficient:
44
54
 
45
- The default behaviours are:
46
-
47
- * Start in the current directory and recursively find all files
48
- and subdirectories underneath there
49
- * All built-in rules will be added to the current rule set
50
- * There are a few built-in
51
- types of files handled and default rulesets that apply to them.
52
- * For each rule in a ruleset, it will use the default settings for that rule
53
- * It will use the default set of locales (the top 20 locales on the internet
54
- by traffic) with "en-US" being the source locale
55
- * It will apply each rule in the current ruleset that applies to
56
- each type of file. If a file type does not have any rulesets that apply to it,
57
- it will be skipped.
58
- * the locale of a file will be gleaned from its path name if possible
59
- * for some types of resource files, the locale is documented in
60
- the file itself. (eg. xliff files)
61
- * Output will be printed on the standard output in human readable form
62
55
  ```
63
56
  $ cd myproject
64
57
  $ ilib-lint
65
- ilib-lint - Copyright (c) 2022 JEDLsoft, All rights reserved.
58
+ ilib-lint - Copyright (c) 2022-2023 JEDLsoft, All rights reserved.
66
59
  WARN: i18n/ru_RU.properties(45): translation should use the appropriate
67
60
  quote style
68
61
  myproject.dialog1.body.text = Нажмите кнопку "Справка", чтобы получить
@@ -72,6 +65,29 @@ be «text»
72
65
  $
73
66
  ```
74
67
 
68
+ ## Default Behaviours
69
+
70
+ The default behaviour is to recursively search the current directory for all
71
+ xliff files, and then apply all of the built-in resource rules to those files
72
+ and report human-readable results to the standard output.
73
+
74
+ The default behaviours are:
75
+
76
+ * Start in the current directory and recursively find all xliff files
77
+ underneath there. The xliff file type is built-in to the linter.
78
+ * All built-in rules will be added to the current rule set, and it will
79
+ instantiate each rule with its default settings.
80
+ * It will use the default set of locales (the top 20 locales on the internet
81
+ by traffic) with "en-US" being the source locale
82
+ * For each file found, it applies each rule in the ruleset.
83
+ If a file type does not have any rulesets that apply to it,
84
+ it will be skipped.
85
+ * the locale of a file can sometimes be gleaned from its path name
86
+ * for some types of resource files, the locale is documented in
87
+ the file itself. (eg. xliff or other resource files)
88
+ * Output will be printed on the standard output in human readable form
89
+
90
+
75
91
  ## Command-line Parameters
76
92
 
77
93
  ilib-lint accepts the following command-line parameters:
@@ -83,8 +99,8 @@ ilib-lint accepts the following command-line parameters:
83
99
  with status 2 if there are errors, and status 0 if there are warnings. This
84
100
  flag allows you to squelch the warnings and only fail a script if there are
85
101
  actual errors.
86
- * locales - Locales you want your app to support. Value is a comma-separated
87
- list of BCP-47 style locale tags.
102
+ * locales - Locales you want your app to support globally. Value is a comma-separated
103
+ list of BCP-47 style locale tags. File types can override this list.
88
104
  Default: the top 20 locales on the internet by traffic.
89
105
  * sourceLocale - locale of the source files or the source locale for resource
90
106
  files. Default: "en-US"
@@ -101,43 +117,104 @@ exit status:
101
117
  * 1 - warnings found
102
118
  * 2 - errors found
103
119
 
104
- When the `--errorsOnly` flag is given, the program will return 0 if only
105
- warnings can be found or if no issues are found.
120
+ When the `--errorsOnly` flag is given, the program will return 0 unless at least
121
+ one error was found.
106
122
 
107
123
  ## Configuration
108
124
 
109
- If the named paths contain a file called `ilib-lint-config.json`, it
110
- will be read and processed to configure the ilib-lint tool for that path. The
111
- `ilib-lint-config.json` file can have any of the following properties:
125
+ The paths to process are given on the command-line. If no path is specified
126
+ on the command-line, the tool will default to the current directory.
127
+ If any named path contains a file called `ilib-lint-config.json`, that
128
+ file will be read and processed to configure a project within the ilib-lint tool
129
+ with that path as the root directory for the project.
130
+
131
+ This json config file will be parsed as [JSON5](https://json5.org), which means
132
+ it can contain comments and other nice features that make it easier for humans
133
+ to read and write.
134
+
135
+ The `ilib-lint-config.json` file can have any of the following properties:
112
136
 
113
- * name (String) - name of this project
137
+ * name (String, required) - name of this project
114
138
  * locales (Array of strings) - that name the default set of locales for the
115
139
  whole project if they are not configured by each path
116
140
  * sourceLocale (String) - name the locale for source strings in this app.
117
141
  Default if not specified is "en-US".
118
- * paths (Object) - a set of configurations for various paths that are given
119
- by a [micromatch](https://github.com/micromatch/micromatch) glob expression.
120
- Each glob expression property should be an object that contains settings
121
- that apply to files that match the glob expression. The settings can be
122
- be any of:
123
- * locales (Array of strings) - a set of locales that override
124
- the global locales list
125
- * rules - (Object) a set of rules to use with this set of files.
126
- Each rule name maps either to a boolean (true means turn it
127
- on, and false means off) or to a string or object that gives
128
- options for the rule. (Each rule can be different)
129
- * rulesets (Array of strings) - list the names of rule sets to
130
- turn on. Rulesets are groups of rules that are typically
131
- bundled together for particular purpose, such as rules for a
132
- particular library in a particular programming languages. For
133
- example, you might have a "android-kotlin" ruleset that turns
134
- on a number of rules that are typical for checking Android
135
- apps written in Kotlin. If a ruleset is given, the
136
- individual rules in that rule set can still be overridden using
137
- the "rules" property above. Rulesets are just a short-hand
138
- to turn on many of them at once.
139
- * template (string) - a template that can be used to parse the
142
+ * excludes (Array of String) - an array of micromatch expressions for files
143
+ or folders in the project to exclude from the recursive search.
144
+ * rules (Array of Object) - an array of declarative regular-expression-based rules to use
145
+ with this project. Resource rules are applied to resources loaded from a
146
+ resource file. Source file rules are applied to regular programming source
147
+ files. Each item in the rules array should be an
148
+ object that contains the following properties, all of which are required:
149
+ * type (String) - the type of this rule. This may be any of the
150
+ following:
151
+ * resource-matcher - check resources in a resource file. The
152
+ regular expressions that match in the
153
+ source strings must also match in the target string
154
+ * resource-source - check resources in a resource file. If
155
+ the regular expressions match in the source string of a
156
+ resource, a result will be generated
157
+ * resource-target - check resources in a resource file. If
158
+ the regular expressions match in the target string of a
159
+ resource, a result will be generated
160
+ * sourcefile - Check the text in a source file, such as a
161
+ java file or a python file. Regular expressions that match
162
+ in the source file will generate results
163
+ * name (String) - a unique dash-separated name of this rule.
164
+ eg. "resource-url-match",
165
+ * description (String) - a description of what this rule is trying
166
+ to do. eg. "Ensure that URLs that appear in the source string are
167
+ also used in the translated string"
168
+ * note (String) - string to use when the regular expression check fails.
169
+ eg. "URL '{matchString}' from the source string does not appear in
170
+ the target string"
171
+ Note that you can use `{matchString}` to show the user the string
172
+ that the regular expression matched in the source but not in the target.
173
+ * regexps (Array.<String>) - an array of regular expressions to match
174
+ in the source and target strings. If any one of those expressions
175
+ matches in the source, but not the target, the rule will create
176
+ a Result that will be formatted for the user.
177
+ * formatters (Array of Object) - a set of declarative formatters. Each array element is
178
+ an object that contains the following properties:
179
+ * name - a unique name for this formatter
180
+ * description - a description of this formatter to show to users
181
+ * template - a template string that shows how the various fields of a Result instance should be
182
+ formatted, plus two extras that come from the rule: ruleName and ruleDescription
183
+ * highlightStart - string to use as the highlight starting marker in the highlight string
184
+ * highlightEnd - string to use as the highlight ending marker in the highlight string
185
+ * rulesets (Object) - configure named sets of rules. Some rules can be shared between
186
+ file types and others are more specific to the file type. As such, it is sometimes
187
+ convenient to to name a set of rules and refer to the whole set by its name instead
188
+ of listing them all out. The properties of the rulesets object are the names of the
189
+ sets, and the values is also a Object that configures each rule that should be
190
+ included in that set. The rules are turned on with a value "true" or with a
191
+ rule-specific option. They are turned off with a falsy value.
192
+ * filetypes (Object) - a set of configurations for various file types. The file types
193
+ are given dash-separated names such as "python-source-files" so that they can be referred
194
+ to in the
195
+ paths object below. Properties in the filetypes object are the name of the file type,
196
+ and the values are an object that gives the settings for that file type. The value
197
+ object can contain any of the following properties:
198
+ * template (String, required) - a template that can be used to parse the
140
199
  file name for the locale of that file.
200
+ * locales (Array of String) - a set of locales that override
201
+ the global locales list. If not specified, the file type uses the
202
+ global set of locales.
203
+ * ruleset (String, Array of String, or Object) - name the rule set or
204
+ list of rule sets to use with files of this type if the value is
205
+ a string or an array of strings. When the value is a list of strings,
206
+ the rules are a superset of all of the rules in the named rule sets.
207
+ If the value is an object, then it is considered to be an on-the-fly
208
+ unnamed rule set defined directly.
209
+ * paths (Object) - this maps sets of files to file types. The properties in this
210
+ object are [micromatch](https://github.com/micromatch/micromatch) glob expressions
211
+ that select a subset of files within the current project. The glob expressions
212
+ can only be relative to the root of the project.
213
+ The value of each glob expression property should be either a string that names
214
+ a file type for files that match the glob expression, or an on-the-fly unnamed
215
+ definition of the file type. If you specify the file type directly, it cannot be
216
+ shared with other mappings, so it is usually a good idea to define a named file type
217
+ in the "filetypes" property first.
141
218
 
142
219
  The `ilib-lint-config.json` file can be written in [JSON5](https://github.com/json5/json5)
143
220
  syntax, which means it can contain comments and other enhancements.
@@ -148,34 +225,86 @@ Here is an example of a configuration file:
148
225
 
149
226
  ```json
150
227
  {
228
+ // the name is required and should be unique amongst all your projects
151
229
  "name": "tester",
230
+ // this is the global set of locales that applies unless something else overrides it
152
231
  "locales": [
153
232
  "en-US",
154
233
  "de-DE",
155
234
  "ja-JP",
156
235
  "ko-KR"
157
236
  ],
158
- "excludes": {
237
+ // list of plugins to load
238
+ "plugins": [
239
+ "react"
240
+ ],
241
+ // default micromatch expressions to exclude from recursive dir searches
242
+ "excludes": [
159
243
  "node_modules/**",
160
244
  ".git/**",
161
245
  "test/**"
246
+ ],
247
+ // declarative definitions of new rules
248
+ "rules": [
249
+ // test that named parameters like {param} appear in both the source and target
250
+ {
251
+ "type": "resource-matcher",
252
+ "name": "resource-named-params",
253
+ "description": "Ensure that named parameters that appear in the source string are also used in the translated string",
254
+ "note": "The named parameter '{matchString}' from the source string does not appear in the target string",
255
+ "regexps": [ "\\{\\w+\\}" ]
256
+ }
257
+ ],
258
+ "formatters": [
259
+ {
260
+ "name": "minimal",
261
+ "description": "A minimalistic formatter that only outputs the path and the highlight",
262
+ "template": "{pathName}\n{highlight}\n",
263
+ "highlightStart": ">>",
264
+ "highlightEnd": "<<"
265
+ }
266
+ ],
267
+ // named rule sets to be used with the file types
268
+ "rulesets": {
269
+ "react-rules": {
270
+ // this is the declarative rule defined above
271
+ "resource-named-params": true,
272
+ // the "localeOnly" is an option that the quote matcher supports
273
+ // so this both includes the rule in the rule set and instantiates
274
+ // it with the "localeOnly" option
275
+ "resource-quote-matcher": "localeOnly"
276
+ }
162
277
  },
163
- "paths": {
164
- "src/**/*.json": {
278
+ // defines common settings for a particular types of file
279
+ "filetypes": {
280
+ "json": {
165
281
  // override the general locales
166
282
  "locales": [
167
283
  "en-US",
168
284
  "de-DE",
169
285
  "ja-JP"
170
- ],
286
+ ]
287
+ },
288
+ "javascript": {
171
289
  "rulesets": [
172
- "javascript-ilib"
290
+ "react-rules"
173
291
  ]
174
292
  },
175
- // check the translations from the translation vendor before
176
- // we use them in our project
293
+ "jsx": {
294
+ "rulesets": [
295
+ "react-rules"
296
+ ]
297
+ }
298
+ },
299
+ // this maps micromatch path expressions to a file type definition
300
+ "paths": {
301
+ // use the file type defined above
302
+ "src/**/*.json": "json",
303
+ "src/**/*.js": "javascript",
304
+ "src/**/*.jsx": "jsx",
305
+ // define a file type on the fly
177
306
  "**/*.xliff": {
178
- "rules": {
307
+ "ruleset": {
179
308
  "formatjs-plural-syntax": true,
180
309
  "formatjs-plural-categories": true,
181
310
  "formatjs-match-substitution-params": true,
@@ -204,9 +333,220 @@ The built-in rules are:
204
333
  - resource-unique-key - Ensure that the keys are unique within a locale across
205
334
  all resource files
206
335
 
336
+ ## Writing Plugins
337
+
338
+ The linter tool can support plugins that provide parsers, formatters, or rules,
339
+ or any of them at the same time.
340
+
341
+ ## Common Code
342
+
343
+ All plugins should import and use the classes in the
344
+ [i18nlint-common](https://github.com/ilib-js/i18nlint-common) package.
345
+ This defines the super classes for each plugin type, as well as a number
346
+ of utility classes and functions.
347
+
348
+ ### Linter Plugins
349
+
350
+ Linter plugins are simple wrappers around the parser, formatter, and rule
351
+ plugins, which allow the plugin to define multiple plugins. For example, many
352
+ plugins define multiple related rules at the same time which check for
353
+ different aspects of a string.
354
+
355
+ The linter plugin should override and implement these three methods:
356
+
357
+ - getParsers - return an array of classes that inherit from the Parser class
358
+ - getRules - return an array of classes that inherit from the Rule class
359
+ - getFormatters - return an array of classes that inherit from the Formatter class
360
+
361
+ For rules and formatters, each array entry can be either declarative or
362
+ programmatic. See the descriptions below about declarative and programmatic
363
+ plugins.
364
+
365
+ When returning programmatic plugins, make sure to return the actual class
366
+ itself instead of instances of the class. The linter will need to create
367
+ multiple instances of each class during the run of the program.
368
+
369
+ ### Parsers
370
+
371
+ The job of the parser is to convert a source file into an intermediate representation
372
+ that rules can easily digest. There are two standard representations that many
373
+ rules use, but your parser and rules can use their own representation, as
374
+ long as the parser and the rules agree on what that format is. Typically, the
375
+ parser will produce something like an abstract syntax tree (AST) that the rules
376
+ know how to traverse and interpret.
377
+
378
+ The two standard representations are:
379
+
380
+ - resources - the file is converted into a set of Resource instances
381
+ - lines - the file in converted into a simple array of lines
382
+
383
+ The resources representation is intended to represent entries in resource files
384
+ such as xliff files, gnu po files, or java properties files. Each entry in the
385
+ resource file is represented as an instance of one of the standard resource
386
+ classes all defined in the
387
+ [ilib-tools-common](https://github.com/ilib-js/ilib-tools-common)
388
+ project:
389
+
390
+ - ResourceString - the resource is a single string
391
+ - ResourceArray - the resource is an array of strings
392
+ - ResourcePlural - the resource is a plural string
393
+
394
+ The power of a resource file is that resources can contain both a source and a
395
+ target string, so the rules are able to check the source strings against the target
396
+ strings. Regularly, source files only have source strings in them (if any) so
397
+ the target translations cannot be checked.
398
+
399
+ Parsers should extend the `Parser` class from the `i18nlint-common` package. The constructor
400
+ for your class should define the following properties:
401
+
402
+ - `this.name` - a unique name for this parser
403
+ - `this.description` - a description of this type of parser to display to users
404
+
405
+ It should also override the
406
+ [parseData()](https://github.com/iLib-js/i18nlint-common/blob/main/src/Parser.js)
407
+ method which parses a string, and the
408
+ [parse()](https://github.com/iLib-js/i18nlint-common/blob/main/src/Parser.js)
409
+ method, which loads data from the file and then parses it.
410
+
411
+ You can see an example of a parser plugin by looking at the gnu PO file parser at
412
+ [ilib-lint-python-gnu](https://github.com/ilib-js/ilib-lint-python-gnu/blob/main/src/POParser.js).
413
+ That parser interprets a .po file as a resource file and returns a set of
414
+ Resource instances.
415
+
416
+ ### Rules
417
+
418
+ Rules interpret the intermediate representation of a file produced by a Parser
419
+ and produce a single
420
+ [Result](https://github.com/iLib-js/i18nlint-common/blob/main/src/Result.js)
421
+ instance, an array of Result instances, one for each problem found, or undefined
422
+ if there are no problems found.
423
+
424
+ There are two types of rules, declarative and programmatic.
425
+
426
+ Declarative rules are simply a list of regular expressions with metadata. The
427
+ linter searches for matches to those regular expressions and produces Result
428
+ instances when found. (Or when they are not found in some cases!)
429
+
430
+ These can be declared in the config file. (See the example config file above.)
431
+
432
+ Each declarative rule should have the following properties:
433
+
434
+ * type (String) - the type of this rule. This may be any of the
435
+ following:
436
+ * resource-matcher - check resources in a resource file. The
437
+ regular expressions that match in the
438
+ source strings must also match in the target string
439
+ * resource-source - check resources in a resource file. If
440
+ the regular expressions match in the source string of a
441
+ resource, a result will be generated
442
+ * resource-target - check resources in a resource file. If
443
+ the regular expressions match in the target string of a
444
+ resource, a result will be generated
445
+ * sourcefile - Check the text in a source file, such as a
446
+ java file or a python file. Regular expressions that match
447
+ in the source file will generate results
448
+ * name (String) - a unique dash-separated name of this rule.
449
+ eg. "resource-url-match",
450
+ * description (String) - a description of what this rule is trying
451
+ to do. eg. "Ensure that URLs that appear in the source string are
452
+ also used in the translated string"
453
+ * note (String) - string to use when the regular expression check fails.
454
+ eg. "URL '{matchString}' from the source string does not appear in
455
+ the target string"
456
+ Note that you can use `{matchString}` to show the user the string
457
+ that the regular expression matched in the source but not in the target.
458
+ * regexps (Array.<String>) - an array of regular expressions to match
459
+ in the source and target strings. If any one of those expressions
460
+ matches in the source, but not the target, the rule will create
461
+ a Result that will be formatted for the user.
462
+
463
+ Programmatic rules are used when the requirements for the rules are more complicated
464
+ than a simple regular expression string can handle. For example, a rule that checks
465
+ if the target string of a resource has the correct quote style for the target
466
+ locale first needs to look up what the correct quote style even is in
467
+ order to apply the rule.
468
+
469
+ In order to create a rule instance, create a class that extends the
470
+ [Rule](https://github.com/ilib-js/i18nlint-common/blob/main/src/Rule.js)
471
+ class in the [i18nlint-common](https://github.com/ilib-js/i18nlint-common/] project.
472
+ The constructor of this class should define the following properties:
473
+
474
+ - `this.name` - a unique name for this rule
475
+ - `this.description` - a description of this type of rule to display to users
476
+
477
+ There are no rules for what to name your Rule, but the Rules written by the ilib-js
478
+ organization generally follow some conventions. Resource checkers start with
479
+ "resource-" and source file checkers start with "source-". For resource checkers,
480
+ the word "match" is used at the end when checking both the source and target,
481
+ "source" is used at the end when checking only the source string, and "target"
482
+ when checking only the target string. So, "resource-urls-match" is a Rule that
483
+ checks resource files for URLs in both the source and target. You are free to name
484
+ your rules anything you like or to follow the conventions above. The important
485
+ part is that the name should uniquely identify your Rule so that you can use it
486
+ in config files.
487
+
488
+ The rule should also override and implement the getRuleType() method and the
489
+ [match()](https://github.com/iLib-js/i18nlint-common/blob/main/src/Rule.js) method,
490
+ which takes an intermediate representation as a parameter and returns either
491
+ a single Result, an array of Result, or undefined.
492
+
493
+ If you would like to see an example rule plugin, see the definition of
494
+ the built-in ICU plural matcher rule:
495
+ [resource-icu-plurals](https://github.com/ilib-js/i18nlint/blob/main/src/rules/ResourceICUPlurals.js)
496
+ which checks resources to make sure that plurals in source and target strings
497
+ have the correct syntax for ICU and formatjs.
498
+
499
+ ### Formatters
500
+
501
+ Formatters transform a [Result object](https://github.com/iLib-js/i18nlint-common/blob/main/src/Result.js) into a format that the consumer can use. For the most part, the consumer
502
+ is a human, so the result should be formatted in text so that the user can read
503
+ it easily. Other times, the consumer is another program, so the result should be
504
+ formatted for easy parsing. Formatters can formats the results in any way necessary.
505
+
506
+ There are two types of formatters, declarative and programmatic.
507
+
508
+ Declarative formatters are simply a template string where properties of the Result
509
+ instances are formatted into it. These can be declared in the config file. (See the
510
+ example config file above.)
511
+
512
+ Programmatic formatters are used when the requirements for formatting are more complicated
513
+ than a simple template string can handle. For example, a CSV formatter would have to make
514
+ sure that fields in a CSV file are escaped properly to conform to CSV syntax, and would
515
+ include escaping code in it.
516
+
517
+ In order to create a formatter instance, create a class that extends the
518
+ [Formatter](https://github.com/ilib-js/i18nlint-common/blob/main/src/Formatter.js)
519
+ class in the [i18nlint-common](https://github.com/ilib-js/i18nlint-common/] project.
520
+ The constructor of this class should define the following properties:
521
+
522
+ - `this.name` - a unique name for this formatter
523
+ - `this.description` - a description of this type of formatter to display to users
524
+
525
+ The formatter should also override and implement the
526
+ [format()](https://github.com/iLib-js/i18nlint-common/blob/main/src/Formatter.js) method,
527
+ which takes a Result instance as a parameter and returns a formatted string.
528
+
529
+ If you would like to look at an example formatter plugin, see the definition of
530
+ the built-in default formatter
531
+ [ansi-console-formatter](https://github.com/ilib-js/i18nlint/blob/main/src/formatters/AnsiConsoleFormatter.js)
532
+ which formats a Result for colorful output on an ANSI console.
533
+
534
+ ## Example Plugin
535
+
536
+ You can take a look at the [ilib-lint-python](https://github.com/ilib-js/ilib-lint-python)
537
+ plugin as a working example of ilib-lint plugin. It implements some rules that
538
+ check the various types of substitution parameters that python/django and
539
+ gnu gettext support.
540
+
541
+ Additionally, there is a [sample python project](https://github.com/ilib-js/ilib-samples/lint)
542
+ that uses the ilib-lint-python plugin. It has purposeful errors built into it which
543
+ violate the rules implemented in the plugin so that the linter will produce some output.
544
+ Clone the project, cd to the lint directory, run `npm install`, and then `npm run lint`
545
+ to see the results.
546
+
207
547
  ## License
208
548
 
209
- Copyright © 2022, JEDLSoft
549
+ Copyright © 2022-2023, JEDLSoft
210
550
 
211
551
  Licensed under the Apache License, Version 2.0 (the "License");
212
552
  you may not use this file except in compliance with the License.
@@ -223,6 +563,13 @@ limitations under the License.
223
563
 
224
564
  ## Release Notes
225
565
 
566
+ ### v1.1.0
567
+
568
+ - added support for plugins
569
+ - added count at the end of the output
570
+ - added the --list option to show what things are available to
571
+ put in the config file
572
+
226
573
  ### v1.0.0
227
574
 
228
575
  - initial version