Skip to content

Commit

Permalink
fix(npm-tools): make Prettier overrides work with TS
Browse files Browse the repository at this point in the history
So, I thought I had this working in:

    #101

(see the test plan), but when I got this into liferay-portal in:

    liferay-frontend/liferay-portal#415

I found it wasn't working (the `else` formatting wasn't applying). So
I made the edits in this commit directly in the node_modules folder to
realllllly see it working.

First step: switch in the TS parser when dealing with a TS file.

Second step: observe the same problem we saw (and fixed) in
d8b0308 — namely, that the TS parser doesn't put `start` and
`end` properties on its tokens. We can switch to `range[0]` and
`range[1]` respectively, making the rule work with both parsers.
  • Loading branch information
wincent committed Oct 2, 2020
1 parent d76baf4 commit 50a4d99
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
44 changes: 30 additions & 14 deletions projects/npm-tools/packages/npm-scripts/src/prettier/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* to version).
*/

const typescriptEslint = require('@typescript-eslint/parser');
const babelEslint = require('babel-eslint');
const {CLIEngine, Linter} = require('eslint');
const path = require('path');
Expand All @@ -26,25 +27,36 @@ const {ID_END, ID_START} = require('../jsp/getPaddedReplacement');
const {SCRIPTLET_CONTENT} = require('../jsp/substituteTags');
const {BLOCK_CLOSE, BLOCK_OPEN} = require('../jsp/tagReplacements');
const {FILLER_CHAR, SPACE_CHAR, TAB_CHAR} = require('../jsp/toFiller');
const rule = require('./rules/newline-before-block-statements');

const EXTENSIONS = {
'.js': 'babel-eslint',
'.jsp': 'babel-eslint',
'.jspf': 'babel-eslint',
'.ts': '@typescript-eslint/parser',
'.tsx': '@typescript-eslint/parser',
};

const EXTENSIONS = new Set(['.js', '.jsp', '.jspf', '.ts', '.tsx']);

const linter = new Linter();

/* eslint-disable @liferay/liferay/no-require-and-call */
const LINTERS = {
'@typescript-eslint/parser': {
linter: new Linter(),
parser: typescriptEslint,
},
'babel-eslint': {
linter: new Linter(),
parser: babelEslint,
},
};

/**
* Custom rule because ESLint's `'brace-style': ['error', 'stroustrup']` ignores
* indentation (ie. it puts the "else" on a new line in column 0).
*/
linter.defineRule(
'newline-before-block-statements',
require('./rules/newline-before-block-statements')
);

/* eslint-enable @liferay/liferay/no-require-and-call */
for (const [name, {linter, parser}] of Object.entries(LINTERS)) {
linter.defineRule('newline-before-block-statements', rule);

linter.defineParser('babel-eslint', babelEslint);
linter.defineParser(name, parser);
}

const cli = new CLIEngine({
...eslintConfig,
Expand Down Expand Up @@ -110,10 +122,14 @@ function format(source, options) {

const extension = path.extname(filename);

if (!EXTENSIONS.has(extension)) {
const parser = EXTENSIONS[extension];

if (!parser) {
return formatted;
}

const linter = LINTERS[parser].linter;

const {output} = linter.verifyAndFix(
formatted,
{
Expand All @@ -123,7 +139,7 @@ function format(source, options) {
// is an absolute path; make it a name that matches the
// parser we defined with `defineParser()` above.

parser: 'babel-eslint',
parser,
},

{allowInlineConfig: false, filename}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module.exports = {
}

return fixer.replaceTextRange(
[last.end, keyword.start],
[last.range[1], keyword.range[0]],
`\n${indent}`
);
},
Expand Down

0 comments on commit 50a4d99

Please sign in to comment.