Skip to content

Commit c4ffd43

Browse files
committed
refactor eslint rule
1 parent 99f710b commit c4ffd43

File tree

1 file changed

+53
-57
lines changed

1 file changed

+53
-57
lines changed

eslint-local-rules/require-platform-declaration.js

Lines changed: 53 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ module.exports = {
4242
type: 'problem',
4343
docs: {
4444
description: 'Require __platforms export with valid platform values in all source files',
45-
category: 'Best Practices',
45+
category: 'Possible Problems',
4646
recommended: true,
4747
},
4848
messages: {
@@ -62,66 +62,62 @@ module.exports = {
6262
return {
6363
ExportNamedDeclaration(node) {
6464
// Check for: export const __platforms = [...]
65-
if (node.declaration &&
66-
node.declaration.type === 'VariableDeclaration') {
65+
if (!node.declaration || node.declaration.type !== 'VariableDeclaration') return;
66+
67+
for (const declarator of node.declaration.declarations) {
68+
if (declarator.id.type !== 'Identifier' || declarator.id.name !== '__platforms') continue;
6769

68-
for (const declarator of node.declaration.declarations) {
69-
if (declarator.id.type === 'Identifier' &&
70-
declarator.id.name === '__platforms') {
71-
72-
hasPlatformExport = true;
73-
74-
// Validate it's an array expression
75-
let init = declarator.init;
76-
77-
// Handle TSAsExpression: [...] as const
78-
if (init && init.type === 'TSAsExpression') {
79-
init = init.expression;
80-
}
81-
82-
// Handle TSTypeAssertion: <const>[...]
83-
if (init && init.type === 'TSTypeAssertion') {
84-
init = init.expression;
85-
}
86-
87-
if (!init || init.type !== 'ArrayExpression') {
88-
context.report({
89-
node: declarator,
90-
messageId: 'notArray',
91-
});
92-
return;
93-
}
94-
95-
// Check if array is empty
96-
if (init.elements.length === 0) {
70+
hasPlatformExport = true;
71+
72+
// Validate it's an array expression
73+
let init = declarator.init;
74+
75+
// Handle TSAsExpression: [...] as const
76+
if (init && init.type === 'TSAsExpression') {
77+
init = init.expression;
78+
}
79+
80+
// Handle TSTypeAssertion: <const>[...]
81+
if (init && init.type === 'TSTypeAssertion') {
82+
init = init.expression;
83+
}
84+
85+
if (!init || init.type !== 'ArrayExpression') {
86+
context.report({
87+
node: declarator,
88+
messageId: 'notArray',
89+
});
90+
return;
91+
}
92+
93+
// Check if array is empty
94+
if (init.elements.length === 0) {
95+
context.report({
96+
node: init,
97+
messageId: 'emptyArray',
98+
});
99+
return;
100+
}
101+
102+
// Validate each array element is a valid platform string
103+
for (const element of init.elements) {
104+
if (element && element.type === 'Literal' && typeof element.value === 'string') {
105+
if (!VALID_PLATFORMS.includes(element.value)) {
97106
context.report({
98-
node: init,
99-
messageId: 'emptyArray',
100-
});
101-
return;
102-
}
103-
104-
// Validate each array element is a valid platform string
105-
for (const element of init.elements) {
106-
if (element && element.type === 'Literal' && typeof element.value === 'string') {
107-
if (!VALID_PLATFORMS.includes(element.value)) {
108-
context.report({
109-
node: element,
110-
messageId: 'invalidValues',
111-
data: {
112-
value: element.value,
113-
validPlatforms: VALID_PLATFORMS.map(p => `'${p}'`).join(', ')
114-
}
115-
});
107+
node: element,
108+
messageId: 'invalidValues',
109+
data: {
110+
value: element.value,
111+
validPlatforms: VALID_PLATFORMS.map(p => `'${p}'`).join(', ')
116112
}
117-
} else {
118-
// Not a string literal
119-
context.report({
120-
node: element || init,
121-
messageId: 'notLiterals',
122-
});
123-
}
113+
});
124114
}
115+
} else {
116+
// Not a string literal
117+
context.report({
118+
node: element || init,
119+
messageId: 'notLiterals',
120+
});
125121
}
126122
}
127123
}

0 commit comments

Comments
 (0)