Skip to content

Commit

Permalink
fix: Ignore file extensions by default
Browse files Browse the repository at this point in the history
Thus not require custom look behinds in the terms file.

Another approach to jargonLint/jargonLint#24
  • Loading branch information
sapegin committed Jun 28, 2024
1 parent 2817f5c commit e7607d0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 68 deletions.
22 changes: 14 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const DEFAULT_OPTIONS = {
exclude: [],
};
const sentenceStartRegExp = /\w+[.?!]\)? $/;
const punctuation = '[\\.,;:!?\'"’”)]';

function reporter(context, opts = {}) {
const options = { ...DEFAULT_OPTIONS, ...opts };
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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])(?<!\\.)\\b${pattern}\\b(?= |${punctuation} |${punctuation}${punctuation}|${punctuation}$|$)`,
'igm'
);
}
Expand All @@ -153,7 +159,7 @@ function getMultipleWordRegExp(words) {

/**
* Match pattern on word boundaries in the middle of the text unless the pattern
* has look behinds
* has look behinds or look aheads
* @param {string} pattern
*/
function getAdvancedRegExp(pattern) {
Expand Down
14 changes: 7 additions & 7 deletions terms.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -112,25 +112,25 @@
["API['’]?s", "APIs"],
"CDN",
"CORS",
["(?<![\\.-])css\\b", "CSS"],
"CSS",
"DNS",
"DOM",
["(?<![\\.-])gif\\b", "GIF"],
"GIF",
"GUI",
["(?<![\\.-])html\\b", "HTML"],
"HTML",
["HTTP[ /]2(?:\\.0)?", "HTTP/2"],
["\bHTTP(?!:)", "HTTP"],
["\bHTTPS(?!:)", "HTTPS"],
"IDE",
"IoT",
"I/O",
["I-O", "I/O"],
["(?<![\\.-])jpeg\\b", "JPEG"],
"JPEG",
"MIME",
"OK",
"PaaS",
["(?<![\\.-])pdf\\b", "PDF"],
["(?<![\\.-])png\\b", "PNG"],
"PDF",
"PNG",
"SaaS",
"URI",
"URIs",
Expand All @@ -144,7 +144,7 @@
["wi[- ]?fi", "Wi-Fi"],
"WYSIWYG",
"XML",
["(?<![\\.-])yaml\\b", "YAML"],
"YAML",
"ZIP",

// Names
Expand Down
80 changes: 27 additions & 53 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,61 +243,35 @@ describe('getReplacement', () => {

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: [
{
Expand Down

0 comments on commit e7607d0

Please sign in to comment.