Skip to content

Commit fc01635

Browse files
authored
Merge pull request #110 from liferay/wincent/ts-overrides-again
fix(npm-tools): make Prettier overrides work with TS
2 parents 07d3683 + 21bc341 commit fc01635

File tree

3 files changed

+391
-363
lines changed

3 files changed

+391
-363
lines changed

projects/npm-tools/packages/npm-scripts/src/prettier/index.js

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* to version).
1717
*/
1818

19+
const typescriptEslint = require('@typescript-eslint/parser');
1920
const babelEslint = require('babel-eslint');
2021
const {CLIEngine, Linter} = require('eslint');
2122
const path = require('path');
@@ -26,25 +27,36 @@ const {ID_END, ID_START} = require('../jsp/getPaddedReplacement');
2627
const {SCRIPTLET_CONTENT} = require('../jsp/substituteTags');
2728
const {BLOCK_CLOSE, BLOCK_OPEN} = require('../jsp/tagReplacements');
2829
const {FILLER_CHAR, SPACE_CHAR, TAB_CHAR} = require('../jsp/toFiller');
30+
const rule = require('./rules/newline-before-block-statements');
31+
32+
const EXTENSIONS = {
33+
'.js': 'babel-eslint',
34+
'.jsp': 'babel-eslint',
35+
'.jspf': 'babel-eslint',
36+
'.ts': '@typescript-eslint/parser',
37+
'.tsx': '@typescript-eslint/parser',
38+
};
2939

30-
const EXTENSIONS = new Set(['.js', '.jsp', '.jspf', '.ts', '.tsx']);
31-
32-
const linter = new Linter();
33-
34-
/* eslint-disable @liferay/liferay/no-require-and-call */
40+
const LINTERS = {
41+
'@typescript-eslint/parser': {
42+
linter: new Linter(),
43+
parser: typescriptEslint,
44+
},
45+
'babel-eslint': {
46+
linter: new Linter(),
47+
parser: babelEslint,
48+
},
49+
};
3550

3651
/**
3752
* Custom rule because ESLint's `'brace-style': ['error', 'stroustrup']` ignores
3853
* indentation (ie. it puts the "else" on a new line in column 0).
3954
*/
40-
linter.defineRule(
41-
'newline-before-block-statements',
42-
require('./rules/newline-before-block-statements')
43-
);
44-
45-
/* eslint-enable @liferay/liferay/no-require-and-call */
55+
for (const [name, {linter, parser}] of Object.entries(LINTERS)) {
56+
linter.defineRule('newline-before-block-statements', rule);
4657

47-
linter.defineParser('babel-eslint', babelEslint);
58+
linter.defineParser(name, parser);
59+
}
4860

4961
const cli = new CLIEngine({
5062
...eslintConfig,
@@ -110,10 +122,14 @@ function format(source, options) {
110122

111123
const extension = path.extname(filename);
112124

113-
if (!EXTENSIONS.has(extension)) {
125+
const parser = EXTENSIONS[extension];
126+
127+
if (!parser) {
114128
return formatted;
115129
}
116130

131+
const linter = LINTERS[parser].linter;
132+
117133
const {output} = linter.verifyAndFix(
118134
formatted,
119135
{
@@ -123,7 +139,7 @@ function format(source, options) {
123139
// is an absolute path; make it a name that matches the
124140
// parser we defined with `defineParser()` above.
125141

126-
parser: 'babel-eslint',
142+
parser,
127143
},
128144

129145
{allowInlineConfig: false, filename}

projects/npm-tools/packages/npm-scripts/src/prettier/rules/newline-before-block-statements.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ module.exports = {
3131
}
3232

3333
return fixer.replaceTextRange(
34-
[last.end, keyword.start],
34+
[last.range[1], keyword.range[0]],
3535
`\n${indent}`
3636
);
3737
},

0 commit comments

Comments
 (0)