Skip to content

Commit

Permalink
feat: utils
Browse files Browse the repository at this point in the history
  • Loading branch information
lzear committed Nov 4, 2023
1 parent f6fe552 commit 745d91e
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/sample-code/presets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface PresetConfig {
config: unknown
name: string
}
114 changes: 114 additions & 0 deletions src/sample-code/webstorm-js-sample.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
export const webstormJsSample = `
class Foo extends BarComponent {
}
function buzz() {
return 0;
}
var x = 1, y = 2,
foregroundColor = 'transparent',
highlightColor = 'lime',
font = 'Arial';
/*
Multiline
C-style
Comment
*/
var myLink = {img: "btn.gif"},
local = true,
initial = -1;
width = 400
height = 300
var foo = {
numbers: ['one', 'two', 'three', 'four', 'five', 'six'],
data: {
a: {
id: 123,
type: "String",
isAvailable: true
},
b: {id: 456, type: "Int"}
},
// fBar : function (x,y);
fOne: function (a, b, c, d, e, f, g, h) {
var x = a + b + c + d + e + f + g + h;
fTwo(a, b, c, fThree(d, e, f, g, h));
var z = a == 'Some string' ? 'yes' : 'no';
z = a == 10 ? 'yes' : 'no';
var colors = ['red', 'green', 'blue', 'black', 'white', 'gray'];
for (j = 0; j < 2; j++) i = a;
for (var i = 0; i < colors.length; i++)
var colorString = this.numbers[i];
},
chainedCallSample: function (a, b, c, d, e, f) {
chainRoot.firstCall(a, b, c, d, e, f, g).secondCall(a, b, c, d).thirdCall(a, b, c, d).fourthCall().q(a).r(a, b).s();
chainRoot.x().y()
.z();
},
callArgumentsSample: function () {
this.fTwo("one", "two", "three", "four");
this.fThree({
strA: 'a',
strB: 'b',
strC: 'c',
strD: 'd'
}, 'strE');
[1, 2, 3].reduce(function (previousValue, currentValue) {
return previousValue + currentValue;
}, 10);
},
/**
* Function JSDoc. Long lines can be wrapped with 'Comments'/'Wrap at right margin' option
* @param {string} a Parameter A description.
* @param {string} b Parameter B description. Can extend beyond the right margin.
*/
fTwo: function (a, b, c, d) {
foo(a, b, c, d); // Line comment which can be wrapped if long.
if (true)
return c;
if (a == 'one' && (b == 'two' || c == 'three')) {
return a + b + c + d;
} else return strD;
if (a == 'one') {
return 1;
} else if (a == 'two') {
return 2;
}
var number = -10;
while (x < 0) {
number = number + 1;
}
do {
number = number + 1;
} while (number < 10);
return d;
},
fThree: function ({
strA,
strB,
strC,
strD
}, strE) {
var number = prompt("Enter a number:", 0);
switch (number) {
case 0 :
alert("Zero");
break;
case 1:
alert("One");
break;
}
try {
a[2] = 10;
} catch (e) {
alert("Failure: " + e.message);
}
return strA + strB + strC + strD + strE;
}
};
`
14 changes: 14 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import plugin from '../index.js'
// import {z} from "zod";

export type RuleName = keyof typeof plugin.rules
export const ruleNames = Object.keys(plugin.rules) as RuleName[]
export const isRuleName = (rule: string): rule is RuleName =>
ruleNames.includes(rule as RuleName)

// export const reqSchema = z.object({
// code: z.string(),
// rule: z.string(),
// level: z.enum(['warn', 'error'] as const),
// options: z.any().optional(),
// })
38 changes: 38 additions & 0 deletions utils/report-difference.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type { Difference } from 'prettier-linter-helpers'

import { showInvisibles } from 'prettier-linter-helpers'

import type { RuleContext } from './eslint-types/Rule.js'

export interface Diff {
deleteText: string
insertText: string
offset: number
operation: 'delete' | 'insert' | 'replace'
}

export const reportDifference = <O extends readonly unknown[]>(
context: RuleContext<'delete' | 'insert' | 'replace', O>,
difference: Difference,
extraData: Record<string, string> = {},
) => {
const { deleteText = '', insertText = '', offset, operation } = difference
const range = /** @type {Range} */ [
offset,
offset + deleteText.length,
] as const
const [start, end] = range.map(index =>
context.getSourceCode().getLocFromIndex(index),
)

context.report({
data: {
deleteText: showInvisibles(deleteText),
insertText: showInvisibles(insertText),
...extraData,
},
fix: fixer => fixer.replaceTextRange(range, insertText),
loc: { end, start },
messageId: operation,
})
}

0 comments on commit 745d91e

Please sign in to comment.