Skip to content

Commit

Permalink
lib: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny committed Jun 17, 2016
1 parent 89c6282 commit 6e62d60
Show file tree
Hide file tree
Showing 21 changed files with 317 additions and 293 deletions.
27 changes: 27 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module.exports = {
"env": {
"browser": true,
"commonjs": true,
"es6": true,
"node": true
},
"extends": "eslint:recommended",
"rules": {
"indent": [
"error",
2
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"always"
]
}
};
46 changes: 46 additions & 0 deletions .jscsrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"disallowKeywordsOnNewLine": [ "else" ],
"disallowMixedSpacesAndTabs": true,
"disallowMultipleLineStrings": true,
"disallowMultipleVarDecl": true,
"disallowNewlineBeforeBlockStatements": true,
"disallowQuotedKeysInObjects": true,
"disallowSpaceAfterObjectKeys": true,
"disallowSpaceAfterPrefixUnaryOperators": true,
"disallowSpaceBeforePostfixUnaryOperators": true,
"disallowSpacesInCallExpression": true,
"disallowTrailingComma": true,
"disallowTrailingWhitespace": true,
"disallowYodaConditions": true,

"requireCommaBeforeLineBreak": true,
"requireOperatorBeforeLineBreak": true,
"requireSpaceAfterBinaryOperators": true,
"requireSpaceAfterKeywords": [ "if", "for", "while", "else", "try", "catch" ],
"requireSpaceAfterLineComment": true,
"requireSpaceBeforeBinaryOperators": true,
"requireSpaceBeforeBlockStatements": true,
"requireSpaceBeforeKeywords": [ "else", "catch" ],
"requireSpaceBeforeObjectValues": true,
"requireSpaceBetweenArguments": true,
"requireSpacesInAnonymousFunctionExpression": {
"beforeOpeningCurlyBrace": true
},
"requireSpacesInFunctionDeclaration": {
"beforeOpeningCurlyBrace": true
},
"requireSpacesInFunctionExpression": {
"beforeOpeningCurlyBrace": true
},
"requireSpacesInConditionalExpression": true,
"requireSpacesInForStatement": true,
"requireSpacesInsideArrayBrackets": "all",
"requireSpacesInsideObjectBrackets": "all",
"requireDotNotation": false,

"maximumLineLength": 80,
"validateIndentation": 2,
"validateLineBreaks": "LF",
"validateParameterSeparator": ", ",
"validateQuoteMarks": "'"
}
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
SRC_FILES=
SRC_FILES+= lib/*.js
SRC_FILES+= lib/**/*.js
SRC_FILES+= lib/**/**/*.js
SRC_FILES+= lib/**/**/**/*.js

SRC_FILES+= bin/gyp
SRC_FILES+= test/*.js

lint:
eslint $(SRC_FILES)

format:
eslint --fix $(SRC_FILES)

.PHONY: lint format
119 changes: 10 additions & 109 deletions lib/gyp.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function debugOutput(mode, message) {
throw new Error('');
} catch (e) {
const prefix = e.stack.split('\n')[2].replace(/^\s*at\s*/g, '');
console.log(`${mode.toUpperCase()}:${prefix} ${message}`);
gyp.bindings.log(`${mode.toUpperCase()}:${prefix} ${message}`);
}
}
}
Expand Down Expand Up @@ -79,7 +79,7 @@ function load(buildFiles, format, defaultVariables = {}, includes = [],
* named WITH_CAPITAL_LETTERS to provide a distinct "best practice" namespace,
* avoiding collisions with user and automatic variables.
*/
defaultVariables['GENERATOR'] = format
defaultVariables['GENERATOR'] = format;
defaultVariables['GENERATOR_FLAVOR'] = params['flavor'] || '';

/* Format can be a custom JS file, or by default the name of a module
Expand Down Expand Up @@ -136,7 +136,7 @@ function load(buildFiles, format, defaultVariables = {}, includes = [],
buildFiles, defaultVariables, includes.slice(),
depth, generatorInputInfo, check, circularCheck,
duplicateBasenameCheck,
params['root_targets'])
params['root_targets']);
return [ generator ].concat(result);
}

Expand Down Expand Up @@ -173,105 +173,6 @@ function shlexEnv(envName) {
return flags || [];
}

function formatOpt(opt, value) {
if (/^--/.test(opt))
return `${opt}=${value}`;
return opt + value;
}

function regenerateAppendFlag(flag, values, predicate, envName, options) {
/*
* Regenerate a list of command line flags, for an option of action='append'.
* The |env_name|, if given, is checked in the environment and used to
* generate an initial list of options, then the options that were specified
* on the command line (given in |values|) are appended. This matches the
* handling of environment variables and command line flags where command line
* flags override the environment, while not requiring the environment to be
* set when the flags are used again.
*/

const flags = [];
if (options.use_environment && envName) {
const s = shlexEnv(envName);
for (let i = 0; i < s.length;i++) {
const flagValue = s[i];
const value = formatOpt(flag, predicate(flagValue));

const index = flags.indexOf(value);
if (index !== -1)
flags.splice(index, 1);

flags.push(value);
}
}
if (values) {
for (let i = 0; i < values.length; i++) {
const flagValue = values[i];
flags.push(formatOpt(flag, predicate(flagValue)));
}
}
return flags;
}

function regenerateFlags(options) {
/* Given a parsed options object, and taking the environment variables into
* account, returns a list of flags that should regenerate an equivalent
* options object (even in the absence of the environment variables.)
*
* Any path options will be normalized relative to depth.
*
* The format flag is not included, as it is assumed the calling generator
* will set that as appropriate.
*/
function fixPath(path) {
path = gyp.common.fixIfRelativePath(path, options.depth);

// PORT: os.path.curdir
if (!path)
return '.';
return path;
}

function noop(value) {
return value;
}

// We always want to ignore the environment when regenerating, to avoid
// duplicate or changed flags in the environment at the time of regeneration.
const flags = [ '--ignore-environment' ];
const meta = options._regenerationMetadata;
Object.keys(meta).forEach((name) => {
const metadata = meta[name];

const opt = metadata['opt'];
const value = options.name;
const valuePredicate = metadata['type'] === 'path' ? fixPath : noop;
const action = metadata['action'];
const envName = metadata['envName'];
if (action === 'append') {
flags = flags.concat(regenerateAppendFlag(opt, value, valuePredicate,
envName, options));
} else if (action === 'store' || !action) {
if (value)
flags.push(formatOpt(opt, valuePredicate(value)));
else if (options.use_environment && envName && process.env[envName])
flags.push(formatOpt(opt, valuePredicate(process.env[envName])));
} else if (action === 'store_true' || action === 'store_false') {
if (action === 'store_true' && value ||
action === 'store_false' && !value) {
flags.append(opt);
} else if (options.use_environment && envName) {
console.error(`Warning: environment regeneration unimplemented for ` +
`${action} flag "${opt}" env_name "${envName}"`);
}
} else {
console.error(`Warning: regeneration unimplemented for action ` +
`${action} flag "${opt}"`);
}
});
return flags;
}

function RegeneratableOptionParser() {
this.options = {};
this.letters = {};
Expand Down Expand Up @@ -333,7 +234,7 @@ RegeneratableOptionParser.prototype.setUsage = function setUsage(usage) {
};

RegeneratableOptionParser.prototype.printHelp = function printHelp() {
console.error(this.usage);
gyp.bindings.error(this.usage);
let max = 0;

Object.keys(this.options).forEach((name) => {
Expand All @@ -356,7 +257,7 @@ RegeneratableOptionParser.prototype.printHelp = function printHelp() {
desc += ' ';

desc += `${option.help}`;
console.error(desc);
gyp.bindings.error(desc);
});
process.exit(0);
};
Expand Down Expand Up @@ -498,9 +399,9 @@ gyp.main = function main(args) {
* deprecated.
*/
parser.addOption('no-duplicate-basename-check', {
dest: 'duplicate_basename_check', action: 'store_false',
default: true, regenerate: false,
help: 'don\'t check for duplicate basenames' });
dest: 'duplicate_basename_check', action: 'store_false',
default: true, regenerate: false,
help: 'don\'t check for duplicate basenames' });
parser.addOption('suffix', 'S', { dest: 'suffix', default: '',
help: 'suffix to add to generated files' });
parser.addOption('toplevel-dir', { dest: 'toplevel_dir', action: 'store',
Expand Down Expand Up @@ -564,7 +465,7 @@ gyp.main = function main(args) {

// Do an extra check to avoid work when we're not debugging.
if (gyp.debug[DEBUG_GENERAL]) {
debugOutput(DEBUG_GENERAL, 'running with these options:')
debugOutput(DEBUG_GENERAL, 'running with these options:');
Object.keys(options).sort().forEach((option) => {
const value = options[option];
if (option[0] === '_')
Expand Down Expand Up @@ -609,7 +510,7 @@ gyp.main = function main(args) {
if (homeDotGyp) {
const defaultInclude = path.join(homeDotGyp, 'include.gypi');
if (fs.existsSync(defaultInclude)) {
console.log(`Using overrides found in ${defaultInclude}`);
gyp.bindings.log(`Using overrides found in ${defaultInclude}`);
includes.push(defaultInclude);
}
}
Expand Down
9 changes: 8 additions & 1 deletion lib/gyp/bindings.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

const util = require('util');
const path = require('path');
const fs = require('fs');
const execSync = require('child_process').execSync;
Expand Down Expand Up @@ -34,3 +33,11 @@ exports.process = {
platform: process.platform,
exit: (code) => process.exit(code)
};

exports.log = function log(message) {
process.stdout.write(message + '\n');
};

exports.error = function error(message) {
process.stderr.write(message + '\n');
};
16 changes: 9 additions & 7 deletions lib/gyp/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,32 +232,34 @@ function parseQualifiedTarget(target) {
// NOTE: rsplit is used to disambiguate the Windows drive letter separator.
let buildFile;
if (/:/.test(target)) {
let _;
[ _, buildFile, target ] = target.match(/^(.*?)(?::([^:]*))?$/);
const match = target.match(/^(.*?)(?::([^:]*))?$/);
buildFile = match[1];
target = match[2];
} else {
buildFile = undefined;
}

let toolset;
if (/#/.test(target)) {
let _;
[ _, target, toolset ] = target.match(/^(.*?)(?:#([^:]*))?$/);
const match = target.match(/^(.*?)(?:#([^:]*))?$/);
target = match[1];
toolset = match[2];
} else {
toolset = undefined;
}

return [ buildFile, target, toolset ];
};
}
exports.parseQualifiedTarget = parseQualifiedTarget;

exports.findQualifiedTargets = function findQualifiedTargets(target, flatList) {
exports.findQualifiedTargets = function findQualifiedTargets() {
throw new Error('Not implemented');
};

exports.buildFile = function (fullyQualifiedTarget) {
// Extracts the build file from the fully qualified target.
return parseQualifiedTarget(fullyQualifiedTarget)[0];
}
};

//
// Python shims
Expand Down
Loading

0 comments on commit 6e62d60

Please sign in to comment.