Skip to content

Commit

Permalink
fix: Lint & snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
d3xter666 committed Mar 21, 2024
1 parent 8785f76 commit 25b1aa9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
37 changes: 18 additions & 19 deletions src/detectors/typeChecker/FileLinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,15 +211,16 @@ export default class FileLinter {
messageDetails: deprecationInfo.messageDetails,
});
}

/**
* Extracts & builds object literal
*
* @param node
* @param node
* @returns {object}
*/
extractPropsRecursive = (node: ts.ObjectLiteralExpression) => {
const properties: Record<string,any> = Object.create(null);
type propsType = Record<string, {value: null | boolean | string | propsType; node: ts.Node}>;
const properties = Object.create(null) as propsType;

node.properties?.forEach((prop) => {
if (!ts.isPropertyAssignment(prop) || !prop.name) {
Expand All @@ -231,30 +232,28 @@ export default class FileLinter {
properties[key] = {value: false, node: prop.initializer};
} else if (prop.initializer.kind === ts.SyntaxKind.NullKeyword) {
properties[key] = {value: null, node: prop.initializer};
} if (ts.isObjectLiteralExpression(prop.initializer) && prop.initializer.properties) {
properties[key] = { value: this.extractPropsRecursive(prop.initializer), node: prop.initializer };
} else if (
(ts.isIdentifier(prop.initializer) ||
ts.isNumericLiteral(prop.initializer) ||
ts.isStringLiteral(prop.initializer))

&& prop.initializer.text) {
} else if (ts.isObjectLiteralExpression(prop.initializer) && prop.initializer.properties) {
properties[key] = {value: this.extractPropsRecursive(prop.initializer), node: prop.initializer};
} else if ((ts.isIdentifier(prop.initializer) ||
ts.isNumericLiteral(prop.initializer) ||
ts.isStringLiteral(prop.initializer)) &&
prop.initializer.text) {
properties[key] = {value: prop.initializer.getText(), node: prop.initializer};
}
});
return properties;
}
};

analyzeLibInitCall(node: ts.CallExpression) {
const nodeExp = node.expression as ts.PropertyAccessExpression;
const {symbol} = this.#checker.getTypeAtLocation(nodeExp);
const methodName = symbol && symbol.getName();
const methodName = symbol?.getName();

// TS parser uses some intermediate types that are not available as definitions.
// In this case SymbolObject which is a ts.Symbol + ts.Node and that's
// why we need these ugly type castings
const importDeclaration =
((((symbol as unknown) as ts.Node)?.parent?.parent as unknown) as ts.Symbol)?.getName() as string;
((((symbol as unknown) as ts.Node)?.parent?.parent as unknown) as ts.Symbol)?.getName();

if (importDeclaration !== "\"sap/ui/core/Lib\"" || methodName !== "init") {
return;
Expand All @@ -269,10 +268,10 @@ export default class FileLinter {
} else {
const apiKeyProp = this.extractPropsRecursive(initArg);

if (!apiKeyProp["apiVersion"]) {
nodeToHighlight = node;
} else if (apiKeyProp["apiVersion"].value !== "2") { // String value would be "\"2\""
nodeToHighlight = apiKeyProp["apiVersion"].node;
if (!apiKeyProp.apiVersion) {
nodeToHighlight = node;
} else if (apiKeyProp.apiVersion.value !== "2") { // String value would be "\"2\""
nodeToHighlight = apiKeyProp.apiVersion.node;
}
}

Expand Down
18 changes: 13 additions & 5 deletions test/lib/linter/snapshots/linter.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,7 @@ Generated by [AVA](https://avajs.dev).
},
{
coverageInfo: [],
errorCount: 8,
errorCount: 9,
fatalErrorCount: 0,
filePath: 'src/main/js/library.js',
messages: [
Expand Down Expand Up @@ -937,25 +937,33 @@ Generated by [AVA](https://avajs.dev).
severity: 2,
},
{
column: 3,
column: 15,
fatal: undefined,
line: 21,
message: 'Call to Library.init() must be declared with property {apiVersion: 2}',
ruleId: 'ui5-linter-no-partially-deprecated-api',
severity: 2,
},
{
column: 3,
column: 15,
fatal: undefined,
line: 24,
message: 'Call to Library.init() must be declared with property {apiVersion: 2}',
ruleId: 'ui5-linter-no-partially-deprecated-api',
severity: 2,
},
{
column: 15,
fatal: undefined,
line: 27,
message: 'Call to Library.init() must be declared with property {apiVersion: 2}',
ruleId: 'ui5-linter-no-partially-deprecated-api',
severity: 2,
},
{
column: 2,
fatal: undefined,
line: 38,
line: 41,
message: 'Call to deprecated function \'initLibrary\' of class \'Core\'',
messageDetails: 'Deprecated test message',
ruleId: 'ui5-linter-no-deprecated-api',
Expand All @@ -964,7 +972,7 @@ Generated by [AVA](https://avajs.dev).
{
column: 2,
fatal: undefined,
line: 38,
line: 41,
message: 'Call to deprecated function \'getCore\' (sap.ui.getCore)',
messageDetails: 'Deprecated test message',
ruleId: 'ui5-linter-no-deprecated-api',
Expand Down

0 comments on commit 25b1aa9

Please sign in to comment.