Skip to content

Commit

Permalink
support gts autofix
Browse files Browse the repository at this point in the history
  • Loading branch information
patricklx committed May 13, 2023
1 parent 558dab3 commit 0e306d6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
10 changes: 10 additions & 0 deletions lib/preprocessors/glimmer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -295,4 +304,5 @@ function mapRange(messages, filename) {
module.exports = {
preprocess: gjs,
postprocess: mapRange,
supportsAutofix: true,
};
3 changes: 1 addition & 2 deletions lib/utils/document.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* istanbul ignore file */

/**
* @typedef {{ line: number; character: number }} Position
*/
Expand Down Expand Up @@ -75,6 +73,7 @@ function computeLineStarts(text) {
return result;
}

/* istanbul ignore next */
/**
* @param {number} ch
* @return {boolean}
Expand Down
35 changes: 33 additions & 2 deletions tests/lib/rules-preprocessor/gjs-gts-processor-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -173,7 +174,7 @@ const invalid = [
import Component from '@glimmer/component';
export default class MyComponent extends Component {
foo = "bar";
foo = 'bar';
<template>"hi"</template>
}
`,
Expand All @@ -193,7 +194,7 @@ const invalid = [
import Component from '@glimmer/component';
export default class MyComponent extends Component {
foo = "bar";
foo = 'bar';
<template>"hi"
</template>
}
Expand All @@ -208,6 +209,36 @@ const invalid = [
},
],
},
{
filename: 'my-component.gjs',
code: `
import Component from "@glimmer/component";
export default class MyComponent extends Component {
foo = 'bar';
<template>"hi"
</template>
}
`,
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', () => {
Expand Down

0 comments on commit 0e306d6

Please sign in to comment.