From 0e306d631ba9583b3a71fc2f6c4da9f553c1ea6a Mon Sep 17 00:00:00 2001 From: "pa.trickjlp@gmail.com" Date: Sat, 13 May 2023 07:39:40 +0200 Subject: [PATCH 1/2] support gts autofix --- lib/preprocessors/glimmer.js | 10 ++++++ lib/utils/document.js | 3 +- .../gjs-gts-processor-test.js | 35 +++++++++++++++++-- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/lib/preprocessors/glimmer.js b/lib/preprocessors/glimmer.js index 91986cd11b..1a4c5d721f 100644 --- a/lib/preprocessors/glimmer.js +++ b/lib/preprocessors/glimmer.js @@ -128,6 +128,7 @@ function mapRange(messages, filename) { return flattened; } + const originalDoc = new DocumentLines(original); const transformedDoc = new DocumentLines(transformed); const lines = transformed.split('\n'); @@ -193,6 +194,14 @@ function mapRange(messages, filename) { }); return filtered.map((message) => { + if (message.fix) { + const [start, end] = message.fix.range; + const startPos = transformedDoc.offsetToPosition(start); + const endPos = transformedDoc.offsetToPosition(end); + const fix = message.fix; + fix.range = [originalDoc.positionToOffset(startPos), originalDoc.positionToOffset(endPos)]; + } + // 1. handle eslint diagnostics on single lines. if (message.line === message.endLine) { const originalLine = originalLines[message.line - 1]; @@ -295,4 +304,5 @@ function mapRange(messages, filename) { module.exports = { preprocess: gjs, postprocess: mapRange, + supportsAutofix: true, }; diff --git a/lib/utils/document.js b/lib/utils/document.js index c21ac0ea8b..3e22618668 100644 --- a/lib/utils/document.js +++ b/lib/utils/document.js @@ -1,5 +1,3 @@ -/* istanbul ignore file */ - /** * @typedef {{ line: number; character: number }} Position */ @@ -75,6 +73,7 @@ function computeLineStarts(text) { return result; } +/* istanbul ignore next */ /** * @param {number} ch * @return {boolean} diff --git a/tests/lib/rules-preprocessor/gjs-gts-processor-test.js b/tests/lib/rules-preprocessor/gjs-gts-processor-test.js index a63607f537..6fb7f5b56f 100644 --- a/tests/lib/rules-preprocessor/gjs-gts-processor-test.js +++ b/tests/lib/rules-preprocessor/gjs-gts-processor-test.js @@ -35,6 +35,7 @@ function initESLint(options) { plugins: ['ember'], extends: ['plugin:ember/recommended'], rules: { + quotes: ['error', 'single'], semi: ['error', 'always'], 'object-curly-spacing': ['error', 'always'], 'lines-between-class-members': 'error', @@ -173,7 +174,7 @@ const invalid = [ import Component from '@glimmer/component'; export default class MyComponent extends Component { - foo = "bar"; + foo = 'bar'; } `, @@ -193,7 +194,7 @@ const invalid = [ import Component from '@glimmer/component'; export default class MyComponent extends Component { - foo = "bar"; + foo = 'bar'; } @@ -208,6 +209,36 @@ const invalid = [ }, ], }, + { + filename: 'my-component.gjs', + code: ` + import Component from "@glimmer/component"; + export default class MyComponent extends Component { + foo = 'bar'; + + } + `, + errors: [ + { + message: 'Strings must use singlequote.', + line: 2, + endLine: 2, + endColumn: 49, + column: 29, + fix: { + range: [29, 49], + }, + }, + { + message: 'Expected blank line between class members.', + line: 5, + endLine: 6, + column: 9, + endColumn: 20, + }, + ], + }, ]; describe('template-vars', () => { From 1a19067349dd0bba6c062aa259d386af794e4d63 Mon Sep 17 00:00:00 2001 From: Patrick Pircher Date: Mon, 15 May 2023 16:16:52 +0200 Subject: [PATCH 2/2] add comment about fix range --- lib/preprocessors/glimmer.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/preprocessors/glimmer.js b/lib/preprocessors/glimmer.js index 1a4c5d721f..22888d12db 100644 --- a/lib/preprocessors/glimmer.js +++ b/lib/preprocessors/glimmer.js @@ -194,6 +194,11 @@ function mapRange(messages, filename) { }); return filtered.map((message) => { + // we need to adjust the fix range provided by eslint to the correct range in the original content + // the lines & columns did not change, except for the template part. but the total offset did, as the template content changed + // therefore we need to remap the range correctly: + // 1. convert range in transformed content to line & column. We know this is the same, but offsets are different + // 2. convert line & column back to offsets in the original content if (message.fix) { const [start, end] = message.fix.range; const startPos = transformedDoc.offsetToPosition(start);