@okta/eslint-plugin-okta
Advanced tools
Comparing version 0.4.0 to 0.5.0
@@ -25,2 +25,5 @@ /*! | ||
processor: '@okta/okta/properties', | ||
rules: { | ||
'@okta/okta/no-missing-i18n-comments': 2, | ||
} | ||
}, | ||
@@ -39,2 +42,5 @@ ], | ||
processor: '@okta/okta/properties', | ||
rules: { | ||
'@okta/okta/no-missing-i18n-comments': 2, | ||
} | ||
}, | ||
@@ -53,2 +59,3 @@ { | ||
'no-exclusive-language': require('./lib/rules/no-exclusive-language'), | ||
'no-missing-i18n-comments': require('./lib/rules/no-missing-i18n-comments'), | ||
'no-unlocalized-text-in-templates': require('./lib/rules/no-unlocalized-text-in-templates'), | ||
@@ -55,0 +62,0 @@ 'no-unlocalized-text': require('./lib/rules/no-unlocalized-text'), |
@@ -21,3 +21,3 @@ /*! | ||
* | ||
* @param {*} propertiesText: raw properties file content | ||
* @param {*} propertiesText: Raw properties file content | ||
* @param {*} json: Output of properties file conversion to JSON | ||
@@ -27,2 +27,3 @@ */ | ||
propertiesMap = {}; | ||
let extras = []; | ||
propertiesText | ||
@@ -35,12 +36,14 @@ .split(/\r?\n/) | ||
// We won't always have a key specified (comments, multi-line values, and extra newlines) | ||
// We won't always have a key specified (comments and extra newlines) | ||
if (key && json[key]) { | ||
let jsonIndex = Object | ||
.keys(json) | ||
.findIndex(jsonKey => jsonKey === key); | ||
propertiesMap[key] = { | ||
value: key, | ||
column: 0, | ||
jsonLine: jsonIndex + 1, // Account for "module.exports" | ||
line: index, | ||
line: index + 1, // non-zero index | ||
extras, | ||
}; | ||
// Reset the comments array to account for multiple comment blocks | ||
extras = []; | ||
} else { | ||
extras.push(key); | ||
} | ||
@@ -56,7 +59,22 @@ }); | ||
const preprocess = (text, filename) => { | ||
if (!text) return; | ||
if (!text) return [ text, filename ]; | ||
const json = properties(text); | ||
linkPropertiesToJson(text, json); | ||
let objectText = 'module.exports = {'; | ||
// Walk through our generated JSON and prepend parsed comments and newlines | ||
Object.keys(json).forEach(key => { | ||
if (!key) return; | ||
const extras = propertiesMap[key] ? propertiesMap[key].extras : []; | ||
extras.forEach(extras => { | ||
objectText += `\n ${extras.replace('#', '//')}`; | ||
}); | ||
const value = json[key].replace(/"/g, '\\"'); // Escape double quotes | ||
objectText += `\n "${key}": "${value}",`; | ||
}); | ||
objectText += '\n};'; | ||
return [{ | ||
text: `module.exports = ${JSON.stringify(json, null, 2)};`, | ||
text: objectText, | ||
filename, | ||
@@ -69,3 +87,2 @@ }]; | ||
* @param {*} messages | ||
* @param {*} filename | ||
*/ | ||
@@ -75,14 +92,12 @@ const postprocess = (messages) => { | ||
.map(msg => { | ||
// Converting from properties files to JS will remove newlines and comments | ||
// Converting from properties files to JS will add additional lines. | ||
// We keep a map so we can display the correct line number | ||
const reference = Object | ||
.values(propertiesMap) | ||
.find(value => value.jsonLine === msg.line); | ||
.find(value => value.line === msg.line - 1); // Account for 'module.exports' | ||
// In some cases, we're unable to find a reference to JSON object | ||
// To avoid a confusing message - remove the line numbers. | ||
return Object.assign( | ||
msg, | ||
reference | ||
? { line: reference.line + 1, column: reference.column } // Non-zero index | ||
? { line: reference.line, column: 0 } | ||
: { line: 0, column: 0 }, | ||
@@ -89,0 +104,0 @@ ); |
@@ -8,2 +8,5 @@ const processor = require('./properties'); | ||
}); | ||
it('should not crash when empty file content is given', () => { | ||
processor.preprocess(''); | ||
}); | ||
it('should return an array', () => { | ||
@@ -15,5 +18,22 @@ const pre = processor.preprocess('hello = there'); | ||
const pre = processor.preprocess('hello = there'); | ||
const expectedText = 'module.exports = {\n "hello": "there"\n};'; | ||
const expectedText = 'module.exports = {\n "hello": "there",\n};'; | ||
expect(pre[0].text).toEqual(expectedText); | ||
}); | ||
it('should return comments and properties content as JS', () => { | ||
const pre = processor.preprocess('# Comment 1\nhello = there'); | ||
const expectedText = 'module.exports = {\n // Comment 1\n "hello": "there",\n};'; | ||
expect(pre[0].text).toEqual(expectedText); | ||
}); | ||
it('should return multi-line comments and properties content as JS', () => { | ||
const pre = processor.preprocess('# Comment 1\n#Comment 2\nhello = there\nkey = value\n# Final Comment\nanother = one'); | ||
const expectedText = `module.exports = { | ||
// Comment 1 | ||
//Comment 2 | ||
"hello": "there", | ||
"key": "value", | ||
// Final Comment | ||
"another": "one", | ||
};`; | ||
expect(pre[0].text).toEqual(expectedText); | ||
}); | ||
it('should return filename', () => { | ||
@@ -34,3 +54,3 @@ const filepath = 'filepath'; | ||
[ | ||
{ line: 1, column: 0, message: 'Hello, world!', ruleId: 'testRule' }, | ||
{ line: 1, column: 0, message: 'Some ESLint message', ruleId: 'testRule' }, | ||
], | ||
@@ -40,3 +60,3 @@ ]); | ||
[ | ||
{ line: 1, column: 0, message: 'Hello, world!', ruleId: 'testRule' } | ||
{ line: 0, column: 0, message: 'Some ESLint message', ruleId: 'testRule' } | ||
] | ||
@@ -52,3 +72,3 @@ ); | ||
[ | ||
{ line: 2, column: 0, message: 'Hello, world!', ruleId: 'testRule' }, | ||
{ line: 4, column: 0, message: 'Some ESLint message', ruleId: 'testRule' }, | ||
], | ||
@@ -58,3 +78,3 @@ ]); | ||
[ | ||
{ line: 3, column: 0, message: 'Hello, world!', ruleId: 'testRule' } | ||
{ line: 3, column: 0, message: 'Some ESLint message', ruleId: 'testRule' } | ||
] | ||
@@ -71,3 +91,3 @@ ); | ||
[ | ||
{ line: 2, column: 0, message: 'Hello, world!', ruleId: 'testRule' }, | ||
{ line: 5, column: 0, message: 'Some ESLint message', ruleId: 'testRule' }, | ||
], | ||
@@ -77,3 +97,3 @@ ]); | ||
[ | ||
{ line: 4, column: 0, message: 'Hello, world!', ruleId: 'testRule' } | ||
{ line: 4, column: 0, message: 'Some ESLint message', ruleId: 'testRule' } | ||
] | ||
@@ -80,0 +100,0 @@ ); |
@@ -5,3 +5,3 @@ { | ||
"license": "Apache-2.0", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"main": "./index.js", | ||
@@ -29,6 +29,6 @@ "scripts": { | ||
"okta": { | ||
"commitSha": "24e9d3d336091bcb3dfa5120ba61e3506fb88579", | ||
"fullVersion": "0.4.0-g24e9d3d", | ||
"testedSha": "a55b2ad55f2fbe73b2ceccf5558e669b64d5ff77" | ||
"commitSha": "5eea2af3fc9fe12015d8b7796fbf43df5c03cd26", | ||
"fullVersion": "0.5.0-g5eea2af", | ||
"testedSha": "b3e8adeeab47950faea585f32302a24333110ee4" | ||
} | ||
} |
@@ -52,7 +52,8 @@ # ESLINT-PLUGIN-OKTA | ||
| Rule | Description | | ||
| -- | -- | | ||
| [no-exclusive-language](docs/rules/no-exclusive-language.md) | Disallow exclusionary words | | ||
| [no-unlocalized-text](docs/rules/no-unlocalized-text.md) | disallow hardcoded English text in Courage components | | ||
| [no-unlocalized-text-in-templates](docs/rules/no-unlocalized-text-in-templates.md) | disallow hardcoded English text in templates | ||
| Rule | Description | | ||
|------------------------------------------------------------------------------------|---------------------------------------------------------------------------| | ||
| [no-exclusive-language](docs/rules/no-exclusive-language.md) | Disallow exclusionary words | | ||
| [no-mission-i18n-comments](docs/rules/no-missing-i18n-comments.md) | Disallow messages without comments for arguments and single-word messages | | ||
| [no-unlocalized-text](docs/rules/no-unlocalized-text.md) | Disallow hardcoded English text in Courage components | | ||
| [no-unlocalized-text-in-templates](docs/rules/no-unlocalized-text-in-templates.md) | Disallow hardcoded English text in templates | | ||
@@ -59,0 +60,0 @@ ## Processors |
52435
21
1236
88