From e7607d0d3d0509af594bff4c57b7c98bf0c5f51a Mon Sep 17 00:00:00 2001 From: Artem Sapegin Date: Fri, 28 Jun 2024 07:29:49 +0200 Subject: [PATCH] fix: Ignore file extensions by default Thus not require custom look behinds in the terms file. Another approach to https://github.com/jargonLint/jargonLint/issues/24 --- index.js | 22 +++++++++------ terms.jsonc | 14 +++++----- test.js | 80 ++++++++++++++++++----------------------------------- 3 files changed, 48 insertions(+), 68 deletions(-) diff --git a/index.js b/index.js index 76ea1dd..eec7219 100644 --- a/index.js +++ b/index.js @@ -12,6 +12,7 @@ const DEFAULT_OPTIONS = { exclude: [], }; const sentenceStartRegExp = /\w+[.?!]\)? $/; +const punctuation = '[\\.,;:!?\'"’”)]'; function reporter(context, opts = {}) { const options = { ...DEFAULT_OPTIONS, ...opts }; @@ -54,7 +55,7 @@ function reporter(context, opts = {}) { let replacement = getReplacement(pattern, replacements, matched); - // Capitalize word in the beginning of a sentense if the original word was capitalized + // Capitalize word in the beginning of a sentence if the original word was capitalized const textBeforeMatch = text.substring(0, index); const isSentenceStart = index === 0 || sentenceStartRegExp.test(textBeforeMatch); @@ -90,7 +91,8 @@ function getTerms(defaultTerms, terms, exclude) { ? loadJson(path.resolve(__dirname, 'terms.jsonc')) : []; const extras = typeof terms === 'string' ? loadJson(terms) : terms; - // Order matters, the first term to match is used. We prioritize user 'extras' before defaults + // Order matters, the first term to match is used. We prioritize user + // 'extras' before defaults const listTerms = [...(Array.isArray(extras) ? extras : []), ...defaults]; // Filter on all terms @@ -133,12 +135,16 @@ function readTermsFile(filepath) { * @param {string} pattern */ function getExactMatchRegExp(pattern) { - const punctuation = '[\\.,;:!?\'"’”)]'; return new RegExp( - // 1. Beginning of the string, or any character that isn't "-" or alphanumeric - // 2. Exact match of the pattern - // 3. Space, punctuation + space, punctuation + punctuation, or punctuation at the end of the string, end of the string - `(?<=^|[^-\\w])\\b${pattern}\\b(?= |${punctuation} |${punctuation}${punctuation}|${punctuation}$|$)`, + // 1. Beginning of the string, or any character that isn't "-" + // or alphanumeric + // 2. Not a dot "." (to make it ignore file extensions) + // 3. Word boundary + // 4. Exact match of the pattern + // 5. Word boundary + // 6. Space, punctuation + space, punctuation + punctuation, + // or punctuation at the end of the string, end of the string + `(?<=^|[^-\\w])(? { tester.run('textlint-rule-terminology', rule, { valid: [ - { - text: 'My JavaScript is good', - }, - { - // Should skip code examples - text: 'My `javascript` is good', - }, - { - // Should skip URLs - text: 'My [code](http://example.com/javascript) is good', - }, - { - // Should keep a capital letter at the beginning of a sentense - text: 'Webpack is good', - }, + { text: 'My JavaScript is good' }, + // Should skip code examples + { text: 'My `javascript` is good' }, + // Should skip URLs + { text: 'My [code](http://example.com/javascript) is good' }, + // Should keep a capital letter at the beginning of a sentense + { text: 'Webpack is good' }, // Should not warn when incorrect term is used as a part of another word - { - text: 'Your javascriptish code', - }, - { - text: 'javascriptish', - }, - { - text: 'Your uberjavascript code', - }, - { - text: 'uberjavascript', - }, + { text: 'Your javascriptish code' }, + { text: 'javascriptish' }, + { text: 'Your uberjavascript code' }, + { text: 'uberjavascript' }, // Should not warn when incorrect term is used as a part of a hyphenates word - { - text: 'Install javascript-some-plugin here', - }, - { - text: 'javascript-some-plugin', - }, - { - text: 'Install some-plugin-javascript here', - }, - { - text: 'some-plugin-javascript', - }, - { - // Should not warn about file names - text: 'Filetype.md', - }, - { - text: 'I think Internet Explorer 6 is the best browser!', - }, - { - // Should ignore `http` in the middle of a word - text: 'We should all use XMLHttpRequest everywhere', - }, - { - text: 'foo.yaml', - }, + { text: 'Install javascript-some-plugin here' }, + { text: 'javascript-some-plugin' }, + { text: 'Install some-plugin-javascript here' }, + { text: 'some-plugin-javascript' }, + { text: 'I think Internet Explorer 6 is the best browser!' }, + // Should ignore `http` in the middle of a word + { text: 'We should all use XMLHttpRequest everywhere' }, + // Filenames with .yaml extensions are valid + { text: 'foo.yaml' }, + // Filenames with `yaml` inside are valid + { text: 'foo-yaml.txt' }, + { text: 'foo_yaml.txt' }, + { text: 'foo-yaml-bar.txt' }, + { text: 'foo_yaml_bar.txt' }, + { text: 'fooyaml.txt' }, + { text: 'fooyaml.txt' }, ], invalid: [ {