Skip to content

Commit

Permalink
lint: sort rules
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Nov 4, 2024
1 parent 3f0ff6a commit f3962cd
Showing 1 changed file with 107 additions and 107 deletions.
214 changes: 107 additions & 107 deletions tsslint.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function getDefaultRules(): Rules {
* }
* ```
*/
'interface-property-semicolon'({ typescript: ts, sourceFile, reportWarning }) {
'format/interface-property-semicolon'({ typescript: ts, sourceFile, reportWarning }) {
const { text } = sourceFile;
ts.forEachChild(sourceFile, function visit(node) {
if (ts.isInterfaceDeclaration(node)) {
Expand Down Expand Up @@ -56,7 +56,7 @@ export function getDefaultRules(): Rules {
* + }
* ```
*/
'braces-around-statements'({ typescript: ts, sourceFile, reportWarning }) {
'format/braces-around-statements'({ typescript: ts, sourceFile, reportWarning }) {
ts.forEachChild(sourceFile, function visit(node) {
if (ts.isIfStatement(node)) {
if (!ts.isBlock(node.thenStatement)) {
Expand Down Expand Up @@ -115,60 +115,14 @@ export function getDefaultRules(): Rules {
=== ts.getLineAndCharacterOfPosition(sourceFile, node.parent.getEnd()).line;
}
},
'missing-dependency'({ typescript: ts, sourceFile, reportError, languageServiceHost }) {
const { noEmit } = languageServiceHost.getCompilationSettings();
if (noEmit) {
return;
}
const packageJsonPath = ts.findConfigFile(sourceFile.fileName, ts.sys.fileExists, 'package.json');
if (!packageJsonPath) {
return;
}
const packageJson = JSON.parse(ts.sys.readFile(packageJsonPath) ?? '');
const parentPackageJsonPath = ts.findConfigFile(path.dirname(path.dirname(packageJsonPath)), ts.sys.fileExists, 'package.json');
const parentPackageJson = !!parentPackageJsonPath && parentPackageJsonPath !== packageJsonPath
? JSON.parse(ts.sys.readFile(parentPackageJsonPath) ?? '')
: {};
ts.forEachChild(sourceFile, function visit(node) {
if (
ts.isImportDeclaration(node)
&& !node.importClause?.isTypeOnly
&& ts.isStringLiteral(node.moduleSpecifier)
&& !node.moduleSpecifier.text.startsWith('./')
&& !node.moduleSpecifier.text.startsWith('../')
) {
let moduleName = node.moduleSpecifier.text.split('/')[0];
if (moduleName.startsWith('@')) {
moduleName += '/' + node.moduleSpecifier.text.split('/')[1];
}
if (
(
packageJson.devDependencies?.[moduleName]
|| parentPackageJson.dependencies?.[moduleName]
|| parentPackageJson.devDependencies?.[moduleName]
|| parentPackageJson.peerDependencies?.[moduleName]
)
&& !packageJson.dependencies?.[moduleName]
&& !packageJson.peerDependencies?.[moduleName]
) {
reportError(
`Module '${moduleName}' should be in the dependencies.`,
node.getStart(sourceFile),
node.getEnd()
);
}
}
ts.forEachChild(node, visit);
});
},
/**
* @example
* ```diff
* - const foo = (bar) => {};
* + const foo = bar => {};
* ```
*/
'arrow-parens'({ typescript: ts, sourceFile, reportWarning }) {
'format/arrow-parens'({ typescript: ts, sourceFile, reportWarning }) {
ts.forEachChild(sourceFile, function visit(node) {
if (
ts.isArrowFunction(node)
Expand Down Expand Up @@ -215,7 +169,7 @@ export function getDefaultRules(): Rules {
ts.forEachChild(node, visit);
});
},
'need-format'({ typescript: ts, sourceFile, languageService, reportWarning }) {
'format/need-format'({ typescript: ts, sourceFile, languageService, reportWarning }) {
const textChanges = languageService.getFormattingEditsForDocument(sourceFile.fileName, {
...ts.getDefaultFormatCodeSettings(),
convertTabsToSpaces: false,
Expand Down Expand Up @@ -258,6 +212,109 @@ export function getDefaultRules(): Rules {
}
}
},
'format/no-trailing-comma-in-function'({ typescript: ts, sourceFile, reportWarning }) {
const { text } = sourceFile;
ts.forEachChild(sourceFile, function visit(node) {
if (ts.isFunctionDeclaration(node) || ts.isArrowFunction(node) || ts.isMethodDeclaration(node)) {
const parameters = node.parameters;
if (parameters.length > 0) {
const lastParameter = parameters[parameters.length - 1];
const nextCharIndex = lastParameter.end;
if (text[nextCharIndex] === ',') {
reportWarning(
`The last parameter of a function should not have a trailing comma.`,
lastParameter.getStart(sourceFile),
lastParameter.getEnd()
).withFix(
'Remove trailing comma',
() => [{
fileName: sourceFile.fileName,
textChanges: [{
span: { start: nextCharIndex, length: 1 },
newText: ''
}]
}]
);
}
}
}
ts.forEachChild(node, visit);
});
},
'format/no-trailing-comma-in-function-call'({ typescript: ts, sourceFile, reportWarning }) {
const { text } = sourceFile;
ts.forEachChild(sourceFile, function visit(node) {
if (ts.isCallExpression(node)) {
if (node.arguments.length > 0) {
const lastArgument = node.arguments[node.arguments.length - 1];
const nextCharIndex = lastArgument.end;
if (text[nextCharIndex] === ',') {
reportWarning(
`The last argument of a function call should not have a trailing comma.`,
lastArgument.getStart(sourceFile),
lastArgument.getEnd()
).withFix(
'Remove trailing comma',
() => [{
fileName: sourceFile.fileName,
textChanges: [{
span: { start: nextCharIndex, length: 1 },
newText: ''
}]
}]
);
}
}
}
ts.forEachChild(node, visit);
});
},
'missing-dependency'({ typescript: ts, sourceFile, reportError, languageServiceHost }) {
const { noEmit } = languageServiceHost.getCompilationSettings();
if (noEmit) {
return;
}
const packageJsonPath = ts.findConfigFile(sourceFile.fileName, ts.sys.fileExists, 'package.json');
if (!packageJsonPath) {
return;
}
const packageJson = JSON.parse(ts.sys.readFile(packageJsonPath) ?? '');
const parentPackageJsonPath = ts.findConfigFile(path.dirname(path.dirname(packageJsonPath)), ts.sys.fileExists, 'package.json');
const parentPackageJson = !!parentPackageJsonPath && parentPackageJsonPath !== packageJsonPath
? JSON.parse(ts.sys.readFile(parentPackageJsonPath) ?? '')
: {};
ts.forEachChild(sourceFile, function visit(node) {
if (
ts.isImportDeclaration(node)
&& !node.importClause?.isTypeOnly
&& ts.isStringLiteral(node.moduleSpecifier)
&& !node.moduleSpecifier.text.startsWith('./')
&& !node.moduleSpecifier.text.startsWith('../')
) {
let moduleName = node.moduleSpecifier.text.split('/')[0];
if (moduleName.startsWith('@')) {
moduleName += '/' + node.moduleSpecifier.text.split('/')[1];
}
if (
(
packageJson.devDependencies?.[moduleName]
|| parentPackageJson.dependencies?.[moduleName]
|| parentPackageJson.devDependencies?.[moduleName]
|| parentPackageJson.peerDependencies?.[moduleName]
)
&& !packageJson.dependencies?.[moduleName]
&& !packageJson.peerDependencies?.[moduleName]
) {
reportError(
`Module '${moduleName}' should be in the dependencies.`,
node.getStart(sourceFile),
node.getEnd()
);
}
}
ts.forEachChild(node, visit);
});
},
/**
* TODO: fix the case
* let foo: Foo;
Expand Down Expand Up @@ -362,63 +419,6 @@ export function getDefaultRules(): Rules {
ts.forEachChild(node, visit);
});
},
'no-trailing-comma-in-function'({ typescript: ts, sourceFile, reportWarning }) {
const { text } = sourceFile;
ts.forEachChild(sourceFile, function visit(node) {
if (ts.isFunctionDeclaration(node) || ts.isArrowFunction(node) || ts.isMethodDeclaration(node)) {
const parameters = node.parameters;
if (parameters.length > 0) {
const lastParameter = parameters[parameters.length - 1];
const nextCharIndex = lastParameter.end;
if (text[nextCharIndex] === ',') {
reportWarning(
`The last parameter of a function should not have a trailing comma.`,
lastParameter.getStart(sourceFile),
lastParameter.getEnd()
).withFix(
'Remove trailing comma',
() => [{
fileName: sourceFile.fileName,
textChanges: [{
span: { start: nextCharIndex, length: 1 },
newText: ''
}]
}]
);
}
}
}
ts.forEachChild(node, visit);
});
},
'no-trailing-comma-in-function-call'({ typescript: ts, sourceFile, reportWarning }) {
const { text } = sourceFile;
ts.forEachChild(sourceFile, function visit(node) {
if (ts.isCallExpression(node)) {
if (node.arguments.length > 0) {
const lastArgument = node.arguments[node.arguments.length - 1];
const nextCharIndex = lastArgument.end;
if (text[nextCharIndex] === ',') {
reportWarning(
`The last argument of a function call should not have a trailing comma.`,
lastArgument.getStart(sourceFile),
lastArgument.getEnd()
).withFix(
'Remove trailing comma',
() => [{
fileName: sourceFile.fileName,
textChanges: [{
span: { start: nextCharIndex, length: 1 },
newText: ''
}]
}]
);
}
}
}
ts.forEachChild(node, visit);
});
},
'no-async-without-await'({ typescript: ts, sourceFile, reportWarning }) {
ts.forEachChild(sourceFile, function visit(node) {
if (ts.isFunctionDeclaration(node) || ts.isArrowFunction(node) || ts.isMethodDeclaration(node)) {
Expand Down

0 comments on commit f3962cd

Please sign in to comment.