Skip to content

Commit

Permalink
fix: apply style fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Oct 26, 2021
1 parent d8eb65c commit ce73d75
Show file tree
Hide file tree
Showing 39 changed files with 258 additions and 34 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ node_js:
- "12.20.0"

before_install:
- export PATH="./node_modules/.bin:$PATH"
- npm config set depth 0
install:
- echo "Avoid Travis's npm auto-install"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"chai": "^4.3.4",
"cross-env": "^7.0.3",
"eslint": "^8.1.0",
"eslint-config-canonical": "^32.6.0",
"eslint-config-canonical": "^32.10.0",
"gitdown": "^3.1.4",
"glob": "^7.2.0",
"husky": "^7.0.4",
Expand Down
2 changes: 2 additions & 0 deletions src/alignTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,11 @@ const alignTransform = ({
if (!nothingAfter.tag) {
tokens.postTag = space(width.tag - tokens.tag.length + spacings.postTag);
}

if (!nothingAfter.type) {
tokens.postType = space(width.type - tokens.type.length + spacings.postType);
}

if (!nothingAfter.name) {
// If post name is empty for all lines (name width 0), don't add post name spacing.
tokens.postName = width.name === 0 ? '' : space(width.name - tokens.name.length + spacings.postName);
Expand Down
3 changes: 2 additions & 1 deletion src/bin/generateReadme.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import glob from 'glob';
import _ from 'lodash';

const trimCode = (code) => {
let lines = code.replace(/^\n/u, '').trimEnd().split('\n');
let lines = code.replace(/^\n/u, '').trimEnd()
.split('\n');

const firsLineIndentation = lines[0].match(/^\s+/u);
const lastLineIndentation = lines[lines.length - 1].match(/^\s+/u);
Expand Down
3 changes: 3 additions & 0 deletions src/bin/generateRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const recommended = options.includes('--recommended');

return;
}

if ((/[A-Z]/u).test(ruleName)) {
console.error('Please ensure the rule has no capital letters');

Expand Down Expand Up @@ -161,10 +162,12 @@ export default iterateJsdoc(({
) {
item.offset = 0;
}

if (!item) {
item = offsets.pop();
item.offset += item.matchedLine.length;
}

if (alreadyIncluded) {
console.log(`Rule name is already present in ${checkName}.`);
} else {
Expand Down
104 changes: 85 additions & 19 deletions src/exportParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const getSymbolValue = function (symbol) {
/* istanbul ignore next */
return null;
}

/* istanbul ignore next */
if (symbol.type === 'literal') {
return symbol.value.value;
Expand Down Expand Up @@ -59,10 +60,13 @@ let createSymbol = null;
const getSymbol = function (node, globals, scope, opt) {
const opts = opt || {};
/* istanbul ignore next */
// eslint-disable-next-line default-case
switch (node.type) {
case 'Identifier': {
return getIdentifier(node, globals, scope, opts);
} case 'MemberExpression': {
}

case 'MemberExpression': {
const obj = getSymbol(node.object, globals, scope, opts);
const propertySymbol = getSymbol(node.property, globals, scope, {simpleIdentifier: !node.computed});
const propertyValue = getSymbolValue(propertySymbol);
Expand All @@ -87,6 +91,7 @@ const getSymbol = function (node, globals, scope, opt) {
/* istanbul ignore next */
return null;
}

case 'TSTypeAliasDeclaration':
case 'TSEnumDeclaration': case 'TSInterfaceDeclaration':
case 'ClassDeclaration': case 'ClassExpression':
Expand All @@ -99,20 +104,27 @@ const getSymbol = function (node, globals, scope, opt) {
val.value = node;

return val;
} case 'AssignmentExpression': {
}

case 'AssignmentExpression': {
return createSymbol(node.left, globals, node.right, scope, opts);
} case 'ClassBody': {
}

case 'ClassBody': {
const val = createNode();
for (const method of node.body) {
val.props[method.key.name] = createNode();
val.props[method.key.name].type = 'object';
val.props[method.key.name].value = method.value;
}

val.type = 'object';
val.value = node;

return val;
} case 'ObjectExpression': {
}

case 'ObjectExpression': {
const val = createNode();
val.type = 'object';
for (const prop of node.properties) {
Expand All @@ -125,6 +137,7 @@ const getSymbol = function (node, globals, scope, opt) {
].includes(prop.type)) {
continue;
}

const propVal = getSymbol(prop.value, globals, scope, opts);
/* istanbul ignore next */
if (propVal) {
Expand All @@ -133,7 +146,9 @@ const getSymbol = function (node, globals, scope, opt) {
}

return val;
} case 'Literal': {
}

case 'Literal': {
const val = createNode();
val.type = 'literal';
val.value = node;
Expand All @@ -156,6 +171,7 @@ const createBlockSymbol = function (block, name, value, globals, isGlobal) {
createSymbol = function (node, globals, value, scope, isGlobal) {
const block = scope || globals;
let symbol;
// eslint-disable-next-line default-case
switch (node.type) {
case 'FunctionDeclaration':
/* istanbul ignore next */
Expand All @@ -168,9 +184,12 @@ createSymbol = function (node, globals, value, scope, isGlobal) {
if (node.id && node.id.type === 'Identifier') {
return createSymbol(node.id, globals, node, globals);
}

/* istanbul ignore next */
break;
} case 'Identifier': {
}

case 'Identifier': {
if (value) {
const valueSymbol = getSymbol(value, globals, block);
/* istanbul ignore next */
Expand All @@ -179,16 +198,20 @@ createSymbol = function (node, globals, value, scope, isGlobal) {

return block.props[node.name];
}

/* istanbul ignore next */
debug('Identifier: Missing value symbol for %s', node.name);
} else {
createBlockSymbol(block, node.name, createNode(), globals, isGlobal);

return block.props[node.name];
}

/* istanbul ignore next */
break;
} case 'MemberExpression': {
}

case 'MemberExpression': {
symbol = getSymbol(node.object, globals, block);

const propertySymbol = getSymbol(node.property, globals, block, {simpleIdentifier: !node.computed});
Expand All @@ -198,6 +221,7 @@ createSymbol = function (node, globals, value, scope, isGlobal) {

return symbol.props[propertyValue];
}

/* istanbul ignore next */
debug('MemberExpression: Missing symbol: %s', node.property.name);
break;
Expand All @@ -209,16 +233,22 @@ createSymbol = function (node, globals, value, scope, isGlobal) {

// Creates variables from variable definitions
const initVariables = function (node, globals, opts) {
// eslint-disable-next-line default-case
switch (node.type) {
case 'Program': {
for (const childNode of node.body) {
initVariables(childNode, globals, opts);
}

break;
} case 'ExpressionStatement': {
}

case 'ExpressionStatement': {
initVariables(node.expression, globals, opts);
break;
} case 'VariableDeclaration': {
}

case 'VariableDeclaration': {
for (const declaration of node.declarations) {
// let and const
const symbol = createSymbol(declaration.id, globals, null, globals);
Expand All @@ -227,11 +257,15 @@ const initVariables = function (node, globals, opts) {
globals.props.window.props[declaration.id.name] = symbol;
}
}

break;
} case 'ExportNamedDeclaration': {
}

case 'ExportNamedDeclaration': {
if (node.declaration) {
initVariables(node.declaration, globals, opts);
}

break;
}
}
Expand All @@ -248,40 +282,57 @@ const mapVariables = function (node, globals, opt, isExport) {
if (opts.ancestorsOnly) {
return false;
}

for (const childNode of node.body) {
mapVariables(childNode, globals, opts);
}

break;
} case 'ExpressionStatement': {
}

case 'ExpressionStatement': {
mapVariables(node.expression, globals, opts);
break;
} case 'AssignmentExpression': {
}

case 'AssignmentExpression': {
createSymbol(node.left, globals, node.right);
break;
} case 'VariableDeclaration': {
}

case 'VariableDeclaration': {
for (const declaration of node.declarations) {
const isGlobal = opts.initWindow && node.kind === 'var' && globals.props.window;
const symbol = createSymbol(declaration.id, globals, declaration.init, globals, isGlobal);
if (symbol && isExport) {
symbol.exported = true;
}
}

break;
} case 'FunctionDeclaration': {
}

case 'FunctionDeclaration': {
/* istanbul ignore next */
if (node.id.type === 'Identifier') {
createSymbol(node.id, globals, node, globals, true);
}

break;
} case 'ExportDefaultDeclaration': {
}

case 'ExportDefaultDeclaration': {
const symbol = createSymbol(node.declaration, globals, node.declaration);
if (symbol) {
symbol.exported = true;
} else if (!node.id) {
globals.ANONYMOUS_DEFAULT = node.declaration;
}

break;
} case 'ExportNamedDeclaration': {
}

case 'ExportNamedDeclaration': {
if (node.declaration) {
if (node.declaration.type === 'VariableDeclaration') {
mapVariables(node.declaration, globals, opts, true);
Expand All @@ -293,21 +344,30 @@ const mapVariables = function (node, globals, opt, isExport) {
}
}
}

for (const specifier of node.specifiers) {
mapVariables(specifier, globals, opts);
}

break;
} case 'ExportSpecifier': {
}

case 'ExportSpecifier': {
const symbol = getSymbol(node.local, globals, globals);
/* istanbul ignore next */
if (symbol) {
symbol.exported = true;
}

break;
} case 'ClassDeclaration': {
}

case 'ClassDeclaration': {
createSymbol(node.id, globals, node.body, globals);
break;
} default: {
}

default: {
/* istanbul ignore next */
return false;
}
Expand All @@ -322,6 +382,7 @@ const findNode = function (node, block, cache) {
if (!block || blockCache.includes(block)) {
return false;
}

blockCache = blockCache.slice();
blockCache.push(block);

Expand Down Expand Up @@ -356,6 +417,7 @@ const getExportAncestor = function (nde) {
if (exportTypes.has(node.type)) {
return node;
}

node = node.parent;
}

Expand All @@ -381,6 +443,7 @@ const isExportByAncestor = function (nde) {
if (!canExportedByAncestorType.has(nde.type)) {
return false;
}

let node = nde.parent;
while (node) {
if (exportTypes.has(node.type)) {
Expand All @@ -390,6 +453,7 @@ const isExportByAncestor = function (nde) {
if (!canExportChildrenType.has(node.type)) {
return false;
}

node = node.parent;
}

Expand All @@ -401,6 +465,7 @@ const findExportedNode = function (block, node, cache) {
if (block === null) {
return false;
}

const blockCache = cache || [];
const {props} = block;
for (const propval of Object.values(props)) {
Expand Down Expand Up @@ -459,6 +524,7 @@ const parse = function (ast, node, opt) {
globalVars.props.module.props.exports = createNode();
globalVars.props.exports = globalVars.props.module.props.exports;
}

if (opts.initWindow) {
globalVars.props.window = createNode();
globalVars.props.window.special = true;
Expand Down

0 comments on commit ce73d75

Please sign in to comment.