Skip to content

Commit

Permalink
feat: add support for @typescript-eslint/parser and TS code in general
Browse files Browse the repository at this point in the history
  • Loading branch information
revelt committed Feb 9, 2021
1 parent 4122e13 commit 53deb16
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 167 deletions.
63 changes: 12 additions & 51 deletions packages/eslint-plugin-row-num/dist/eslint-plugin-row-num.cjs.js
@@ -1,46 +1,31 @@
/**
* eslint-plugin-row-num
* ESLint plugin to update row numbers on each console.log
* Version: 1.4.2
* Version: 1.4.3
* Author: Roy Revelt, Codsen Ltd
* License: MIT
* Homepage: https://codsen.com/os/eslint-plugin-row-num/
*/

'use strict';

require('json-stringify-safe');
var jsRowNum = require('js-row-num');

// import stringify from "json-stringify-safe";

var create = function create(context) {
// console.log(
// `007 ${`\u001b[${33}m${`███████████████████████████████████████`}\u001b[${39}m`}`
// );
return {
CallExpression: function CallExpression(node) {
// console.log(stringify(node, null, 4));
// console.log(`012 node.callee.type = ${node.callee.type}`);

/* istanbul ignore else */

if (node.callee && node.callee.type === "MemberExpression" && node.callee.object && node.callee.object.type === "Identifier" && node.callee.object.name === "console" && node.callee.property && node.callee.property.type === "Identifier" && node.callee.property.name === "log" && node.arguments && Array.isArray(node.arguments) && node.arguments.length) {
node.arguments.forEach(function (arg) {
// console.log(`029 arg.raw: ${arg.raw}`);
// console.log(
// `031 ${`\u001b[${35}m${`██`}\u001b[${39}m`} ${stringify(
// arg,
// null,
// 4
// )}`
// );
// if the updated console.log contents are different from what we
node.arguments.forEach(function (arg) { // if the updated console.log contents are different from what we
// have now, latter needs to be updated.

if (arg.type === "Literal" && typeof arg.raw === "string" && arg.raw !== jsRowNum.fixRowNums(arg.raw, {
overrideRowNum: arg.loc.start.line,
returnRangesOnly: false,
extractedLogContentsWereGiven: true
})) {
// console.log(`050 we have console.log with single or double quotes`);
context.report({
node: node,
messageId: "correctRowNum",
Expand All @@ -49,31 +34,16 @@ var create = function create(context) {
overrideRowNum: arg.loc.start.line,
returnRangesOnly: true,
extractedLogContentsWereGiven: true
}); // console.log(
// `061 ${`\u001b[${33}m${`ranges`}\u001b[${39}m`} = ${JSON.stringify(
// ranges,
// null,
// 4
// )}`
// );
// console.log(
// `068 ${`\u001b[${33}m${`arg.start`}\u001b[${39}m`} = ${JSON.stringify(
// arg.start,
// null,
// 4
// )} (type ${typeof arg.start})`
// );
// console.log(`074 arg.start = ${arg.start}`);
});

if (ranges) {
var preppedRanges = [arg.start + ranges[0][0], arg.start + ranges[0][1]]; // console.log(
// `080 ${`\u001b[${33}m${`preppedRanges`}\u001b[${39}m`} = ${JSON.stringify(
// preppedRanges,
// null,
// 4
// )}`
// );
var offset = arg.start;

if (!offset && arg.range && typeof arg.range[0] === "number") {
offset = arg.range[0];
}

var preppedRanges = [offset + ranges[0][0], offset + ranges[0][1]];
return fixerObj.replaceTextRange(preppedRanges, ranges[0][2]);
}
}
Expand All @@ -83,15 +53,6 @@ var create = function create(context) {
returnRangesOnly: false,
extractedLogContentsWereGiven: true
})) {
// console.log(`103 we have console.log with backticks`);
// console.log(`R1: ${arg.quasis[0].value.raw}`);
// console.log(
// `R2: ${fixRowNums(arg.quasis[0].value.raw, {
// overrideRowNum: arg.loc.start.line,
// returnRangesOnly: true,
// extractedLogContentsWereGiven: true
// })}`
// );
context.report({
node: node,
messageId: "correctRowNum",
Expand Down
63 changes: 12 additions & 51 deletions packages/eslint-plugin-row-num/dist/eslint-plugin-row-num.esm.js
@@ -1,44 +1,29 @@
/**
* eslint-plugin-row-num
* ESLint plugin to update row numbers on each console.log
* Version: 1.4.2
* Version: 1.4.3
* Author: Roy Revelt, Codsen Ltd
* License: MIT
* Homepage: https://codsen.com/os/eslint-plugin-row-num/
*/

import 'json-stringify-safe';
import { fixRowNums } from 'js-row-num';

// import stringify from "json-stringify-safe";

const create = context => {
// console.log(
// `007 ${`\u001b[${33}m${`███████████████████████████████████████`}\u001b[${39}m`}`
// );
return {
CallExpression(node) {
// console.log(stringify(node, null, 4));
// console.log(`012 node.callee.type = ${node.callee.type}`);

/* istanbul ignore else */

if (node.callee && node.callee.type === "MemberExpression" && node.callee.object && node.callee.object.type === "Identifier" && node.callee.object.name === "console" && node.callee.property && node.callee.property.type === "Identifier" && node.callee.property.name === "log" && node.arguments && Array.isArray(node.arguments) && node.arguments.length) {
node.arguments.forEach(arg => {
// console.log(`029 arg.raw: ${arg.raw}`);
// console.log(
// `031 ${`\u001b[${35}m${`██`}\u001b[${39}m`} ${stringify(
// arg,
// null,
// 4
// )}`
// );
// if the updated console.log contents are different from what we
node.arguments.forEach(arg => { // if the updated console.log contents are different from what we
// have now, latter needs to be updated.

if (arg.type === "Literal" && typeof arg.raw === "string" && arg.raw !== fixRowNums(arg.raw, {
overrideRowNum: arg.loc.start.line,
returnRangesOnly: false,
extractedLogContentsWereGiven: true
})) {
// console.log(`050 we have console.log with single or double quotes`);
context.report({
node,
messageId: "correctRowNum",
Expand All @@ -47,31 +32,16 @@ const create = context => {
overrideRowNum: arg.loc.start.line,
returnRangesOnly: true,
extractedLogContentsWereGiven: true
}); // console.log(
// `061 ${`\u001b[${33}m${`ranges`}\u001b[${39}m`} = ${JSON.stringify(
// ranges,
// null,
// 4
// )}`
// );
// console.log(
// `068 ${`\u001b[${33}m${`arg.start`}\u001b[${39}m`} = ${JSON.stringify(
// arg.start,
// null,
// 4
// )} (type ${typeof arg.start})`
// );
// console.log(`074 arg.start = ${arg.start}`);
});

if (ranges) {
const preppedRanges = [arg.start + ranges[0][0], arg.start + ranges[0][1]]; // console.log(
// `080 ${`\u001b[${33}m${`preppedRanges`}\u001b[${39}m`} = ${JSON.stringify(
// preppedRanges,
// null,
// 4
// )}`
// );
let offset = arg.start;

if (!offset && arg.range && typeof arg.range[0] === "number") {
offset = arg.range[0];
}

const preppedRanges = [offset + ranges[0][0], offset + ranges[0][1]];
return fixerObj.replaceTextRange(preppedRanges, ranges[0][2]);
}
}
Expand All @@ -81,15 +51,6 @@ const create = context => {
returnRangesOnly: false,
extractedLogContentsWereGiven: true
})) {
// console.log(`103 we have console.log with backticks`);
// console.log(`R1: ${arg.quasis[0].value.raw}`);
// console.log(
// `R2: ${fixRowNums(arg.quasis[0].value.raw, {
// overrideRowNum: arg.loc.start.line,
// returnRangesOnly: true,
// extractedLogContentsWereGiven: true
// })}`
// );
context.report({
node,
messageId: "correctRowNum",
Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-plugin-row-num/dist/eslint-plugin-row-num.mjs
@@ -1,10 +1,10 @@
/**
* eslint-plugin-row-num
* ESLint plugin to update row numbers on each console.log
* Version: 1.4.2
* Version: 1.4.3
* Author: Roy Revelt, Codsen Ltd
* License: MIT
* Homepage: https://codsen.com/os/eslint-plugin-row-num/
*/

import{fixRowNums as e}from"js-row-num";var r={configs:{recommended:{plugins:["row-num"],rules:{"no-console":"off","row-num/correct-row-num":"error"}}},rules:{"correct-row-num":{create:r=>({CallExpression(t){t.callee&&"MemberExpression"===t.callee.type&&t.callee.object&&"Identifier"===t.callee.object.type&&"console"===t.callee.object.name&&t.callee.property&&"Identifier"===t.callee.property.type&&"log"===t.callee.property.name&&t.arguments&&Array.isArray(t.arguments)&&t.arguments.length&&t.arguments.forEach((a=>{"Literal"===a.type&&"string"==typeof a.raw&&a.raw!==e(a.raw,{overrideRowNum:a.loc.start.line,returnRangesOnly:!1,extractedLogContentsWereGiven:!0})?r.report({node:t,messageId:"correctRowNum",fix:r=>{const t=e(a.raw,{overrideRowNum:a.loc.start.line,returnRangesOnly:!0,extractedLogContentsWereGiven:!0});if(t){return r.replaceTextRange([a.start+t[0][0],a.start+t[0][1]],t[0][2])}}}):"TemplateLiteral"===a.type&&Array.isArray(a.quasis)&&a.quasis.length&&"object"==typeof a.quasis[0]&&a.quasis[0].value&&a.quasis[0].value.raw&&a.quasis[0].value.raw!==e(a.quasis[0].value.raw,{overrideRowNum:a.loc.start.line,returnRangesOnly:!1,extractedLogContentsWereGiven:!0})&&r.report({node:t,messageId:"correctRowNum",fix:r=>{const t=e(a.quasis[0].value.raw,{overrideRowNum:a.loc.start.line,returnRangesOnly:!0,extractedLogContentsWereGiven:!0});if(t){return r.replaceTextRange([a.start+1+t[0][0],a.start+1+t[0][1]],t[0][2])}}})}))}}),meta:{type:"suggestion",messages:{correctRowNum:"Update the row number."},fixable:"code"}}}};export default r;
import"json-stringify-safe";import{fixRowNums as e}from"js-row-num";var r={configs:{recommended:{plugins:["row-num"],rules:{"no-console":"off","row-num/correct-row-num":"error"}}},rules:{"correct-row-num":{create:r=>({CallExpression(t){t.callee&&"MemberExpression"===t.callee.type&&t.callee.object&&"Identifier"===t.callee.object.type&&"console"===t.callee.object.name&&t.callee.property&&"Identifier"===t.callee.property.type&&"log"===t.callee.property.name&&t.arguments&&Array.isArray(t.arguments)&&t.arguments.length&&t.arguments.forEach((a=>{"Literal"===a.type&&"string"==typeof a.raw&&a.raw!==e(a.raw,{overrideRowNum:a.loc.start.line,returnRangesOnly:!1,extractedLogContentsWereGiven:!0})?r.report({node:t,messageId:"correctRowNum",fix:r=>{const t=e(a.raw,{overrideRowNum:a.loc.start.line,returnRangesOnly:!0,extractedLogContentsWereGiven:!0});if(t){let e=a.start;!e&&a.range&&"number"==typeof a.range[0]&&(e=a.range[0]);return r.replaceTextRange([e+t[0][0],e+t[0][1]],t[0][2])}}}):"TemplateLiteral"===a.type&&Array.isArray(a.quasis)&&a.quasis.length&&"object"==typeof a.quasis[0]&&a.quasis[0].value&&a.quasis[0].value.raw&&a.quasis[0].value.raw!==e(a.quasis[0].value.raw,{overrideRowNum:a.loc.start.line,returnRangesOnly:!1,extractedLogContentsWereGiven:!0})&&r.report({node:t,messageId:"correctRowNum",fix:r=>{const t=e(a.quasis[0].value.raw,{overrideRowNum:a.loc.start.line,returnRangesOnly:!0,extractedLogContentsWereGiven:!0});if(t){return r.replaceTextRange([a.start+1+t[0][0],a.start+1+t[0][1]],t[0][2])}}})}))}}),meta:{type:"suggestion",messages:{correctRowNum:"Update the row number."},fixable:"code"}}}};export default r;
5 changes: 3 additions & 2 deletions packages/eslint-plugin-row-num/package.json
Expand Up @@ -78,7 +78,8 @@
},
"dependencies": {
"@babel/runtime": "^7.12.13",
"js-row-num": "^4.0.3"
"js-row-num": "^4.0.3",
"json-stringify-safe": "^5.0.1"
},
"devDependencies": {
"@babel/cli": "^7.12.13",
Expand Down Expand Up @@ -116,4 +117,4 @@
"tslib": "^2.1.0",
"typescript": "^4.1.3"
}
}
}
111 changes: 62 additions & 49 deletions packages/eslint-plugin-row-num/src/rules/correct-row-num.ts
@@ -1,19 +1,19 @@
// import stringify from "json-stringify-safe";
import stringify from "json-stringify-safe";
import { fixRowNums } from "js-row-num";
// console.log(`\n\n\n005 ███████████████████████████████████████`);
console.log(`003 ███████████████████████████████████████`);

export interface Obj {
[key: string]: any;
}

const create = (context: Obj): Obj => {
// console.log(
// `007 ${`\u001b[${33}m${`███████████████████████████████████████`}\u001b[${39}m`}`
// );
console.log(
`011 ${`\u001b[${33}m${`███████████████████████████████████████`}\u001b[${39}m`}`
);
return {
CallExpression(node: Obj) {
// console.log(stringify(node, null, 4));
// console.log(`012 node.callee.type = ${node.callee.type}`);
console.log(stringify(node, null, 4));
console.log(`016 node.callee.type = ${node.callee.type}`);

/* istanbul ignore else */
if (
Expand All @@ -29,15 +29,16 @@ const create = (context: Obj): Obj => {
Array.isArray(node.arguments) &&
node.arguments.length
) {
console.log(`032 ██ `);
node.arguments.forEach((arg) => {
// console.log(`029 arg.raw: ${arg.raw}`);
// console.log(
// `031 ${`\u001b[${35}m${`██`}\u001b[${39}m`} ${stringify(
// arg,
// null,
// 4
// )}`
// );
console.log(`034 arg.raw: ${arg.raw}`);
console.log(
`036 ${`\u001b[${35}m${`██`}\u001b[${39}m`} ${stringify(
arg,
null,
4
)}`
);

// if the updated console.log contents are different from what we
// have now, latter needs to be updated.
Expand All @@ -51,7 +52,9 @@ const create = (context: Obj): Obj => {
extractedLogContentsWereGiven: true,
})
) {
// console.log(`050 we have console.log with single or double quotes`);
console.log(
`056 ${`\u001b[${32}m${`we have console.log with single or double quotes`}\u001b[${39}m`}`
);
context.report({
node,
messageId: "correctRowNum",
Expand All @@ -61,33 +64,41 @@ const create = (context: Obj): Obj => {
returnRangesOnly: true, // <------ now we request ranges
extractedLogContentsWereGiven: true,
});
// console.log(
// `061 ${`\u001b[${33}m${`ranges`}\u001b[${39}m`} = ${JSON.stringify(
// ranges,
// null,
// 4
// )}`
// );
// console.log(
// `068 ${`\u001b[${33}m${`arg.start`}\u001b[${39}m`} = ${JSON.stringify(
// arg.start,
// null,
// 4
// )} (type ${typeof arg.start})`
// );
// console.log(`074 arg.start = ${arg.start}`);
console.log(
`066 ${`\u001b[${33}m${`ranges`}\u001b[${39}m`} = ${JSON.stringify(
ranges,
null,
4
)}`
);
console.log(
`073 ${`\u001b[${33}m${`arg.start`}\u001b[${39}m`} = ${JSON.stringify(
arg.start,
null,
4
)} (type ${typeof arg.start})`
);
if (ranges) {
let offset = arg.start;
if (
!offset &&
arg.range &&
typeof arg.range[0] === "number"
) {
offset = arg.range[0];
}

const preppedRanges = [
arg.start + ranges[0][0],
arg.start + ranges[0][1],
offset + ranges[0][0],
offset + ranges[0][1],
];
// console.log(
// `080 ${`\u001b[${33}m${`preppedRanges`}\u001b[${39}m`} = ${JSON.stringify(
// preppedRanges,
// null,
// 4
// )}`
// );
console.log(
`087 ${`\u001b[${33}m${`preppedRanges`}\u001b[${39}m`} = ${JSON.stringify(
preppedRanges,
null,
4
)}`
);
return fixerObj.replaceTextRange(preppedRanges, ranges[0][2]);
}
},
Expand All @@ -106,15 +117,17 @@ const create = (context: Obj): Obj => {
extractedLogContentsWereGiven: true,
})
) {
// console.log(`103 we have console.log with backticks`);
// console.log(`R1: ${arg.quasis[0].value.raw}`);
// console.log(
// `R2: ${fixRowNums(arg.quasis[0].value.raw, {
// overrideRowNum: arg.loc.start.line,
// returnRangesOnly: true,
// extractedLogContentsWereGiven: true
// })}`
// );
console.log(
`112 ${`\u001b[${32}m${`we have console.log with backticks`}\u001b[${39}m`}`
);
console.log(`R1: ${arg.quasis[0].value.raw}`);
console.log(
`R2: ${fixRowNums(arg.quasis[0].value.raw, {
overrideRowNum: arg.loc.start.line,
returnRangesOnly: true,
extractedLogContentsWereGiven: true,
})}`
);
context.report({
node,
messageId: "correctRowNum",
Expand Down

0 comments on commit 53deb16

Please sign in to comment.