Repository: cordova-lib Updated Branches: refs/heads/master 9c49fedbf -> 2aef8c4a7
Added identifier checking for app id, searches for java+C# reserved words Project: http://git-wip-us.apache.org/repos/asf/cordova-lib/repo Commit: http://git-wip-us.apache.org/repos/asf/cordova-lib/commit/894d4767 Tree: http://git-wip-us.apache.org/repos/asf/cordova-lib/tree/894d4767 Diff: http://git-wip-us.apache.org/repos/asf/cordova-lib/diff/894d4767 Branch: refs/heads/master Commit: 894d476799465e6e6a320912c2b7c987ffbd6ffb Parents: 9c49fed Author: Jesse MacFadyen <[email protected]> Authored: Tue Nov 25 15:06:47 2014 -0800 Committer: Jesse MacFadyen <[email protected]> Committed: Tue Nov 25 15:06:47 2014 -0800 ---------------------------------------------------------------------- .../spec-cordova/valid-identifiers-spec.js | 76 ++++++++++++++++++++ cordova-lib/src/cordova/create.js | 9 ++- cordova-lib/src/util/valid-identifier.js | 47 ++++++++++++ 3 files changed, 131 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/894d4767/cordova-lib/spec-cordova/valid-identifiers-spec.js ---------------------------------------------------------------------- diff --git a/cordova-lib/spec-cordova/valid-identifiers-spec.js b/cordova-lib/spec-cordova/valid-identifiers-spec.js new file mode 100644 index 0000000..5f456ab --- /dev/null +++ b/cordova-lib/spec-cordova/valid-identifiers-spec.js @@ -0,0 +1,76 @@ +/** + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +*/ + +var Q = require('q'), + validateIdentifier = require('../src/util/valid-identifier'), + cordova = require('../src/cordova/cordova'); + +describe('valid-identifier',function(done){ + + it('should allow valid identifiers', function(done) { + expect(validateIdentifier("LeadingCap")).toBe(true); + expect(validateIdentifier("camelCase")).toBe(true); + expect(validateIdentifier("ALLCAPS")).toBe(true); + expect(validateIdentifier("x")).toBe(true); + expect(validateIdentifier("_underscore")).toBe(true); + expect(validateIdentifier("$dollarSign")).toBe(true); + expect(validateIdentifier("_8bit")).toBe(true); + expect(validateIdentifier("LeadingCap.camelCase.ALLCAPS._underscore.$dollarSign")).toBe(true); + done(); + }); + + it('should not allow invalid identifiers', function(done) { + expect(validateIdentifier("3numberstart")).toBe(false); + expect(validateIdentifier("has space")).toBe(false); + expect(validateIdentifier("x + y")).toBe(false); + expect(validateIdentifier("O'Mega")).toBe(false); + expect(validateIdentifier("amp&ersand")).toBe(false); + done(); + }); + + it('should not allow reserved words', function(done) { + expect(validateIdentifier("foreach")).toBe(false); + expect(validateIdentifier("valid.foreach")).toBe(false); + expect(validateIdentifier("win.in.g")).toBe(false); + expect(validateIdentifier("ends.with.")).toBe(false); + expect(validateIdentifier(".starts.with")).toBe(false); + expect(validateIdentifier("double..dot")).toBe(false); + done(); + }); + +}); + +describe('cordova create checks valid-identifier', function(done) { + + it('should reject reserved words from start of id', function(done) { + cordova.raw.create("projectPath", "int.bob", "appName") + .fail(function(err) { + expect(err.message).toBe("Identifier contains a reserved word."); + }) + .fin(done); + }); + + it('should reject reserved words from end of id', function(done) { + cordova.raw.create("projectPath", "bob.class", "appName") + .fail(function(err) { + expect(err.message).toBe("Identifier contains a reserved word."); + }) + .fin(done); + }); +}); \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/894d4767/cordova-lib/src/cordova/create.js ---------------------------------------------------------------------- diff --git a/cordova-lib/src/cordova/create.js b/cordova-lib/src/cordova/create.js index c8cb694..7be9470 100644 --- a/cordova-lib/src/cordova/create.js +++ b/cordova-lib/src/cordova/create.js @@ -30,7 +30,8 @@ var path = require('path'), Q = require('q'), CordovaError = require('../CordovaError'), ConfigParser = require('../configparser/ConfigParser'), - cordova_util = require('./util'); + cordova_util = require('./util'), + validateIdentifier = require('../util/valid-identifier'); var DEFAULT_NAME = 'HelloCordova', DEFAULT_ID = 'io.cordova.hellocordova'; @@ -51,6 +52,12 @@ function create(dir, id, name, cfg) { )); } + if(id && !validateIdentifier(id)) { + return Q.reject(new CordovaError( + "App id contains a reserved word, or is not a valid identifier." + )); + } + // Massage parameters if (typeof cfg == 'string') { cfg = JSON.parse(cfg); http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/894d4767/cordova-lib/src/util/valid-identifier.js ---------------------------------------------------------------------- diff --git a/cordova-lib/src/util/valid-identifier.js b/cordova-lib/src/util/valid-identifier.js new file mode 100644 index 0000000..1b5b0bc --- /dev/null +++ b/cordova-lib/src/util/valid-identifier.js @@ -0,0 +1,47 @@ +/** + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +*/ + +var reserved = [ + "abstract","as","base","bool ","break","byte","case","catch","char", + "checked","class","const","continue","decimal","default","delegate", + "do","double","else","enum","event","explicit","extern","false","finally", + "fixed","float","for","foreach","goto","if","implicit","in","int", + "interface","internal","is","lock","long","namespace","new","null", + "object","operator","out","override","params","private","protected", + "public","readonly","ref","return","sbyte","sealed","short","sizeof", + "stackalloc","static","string","struct","switch","this","throw","true", + "try","typeof","uint","ulong","unchecked","unsafe","ushort","using", + "virtual","void","volatile","while","assert","package","synchronized", + "boolean","implements","import"]; + +var regX = /([a-zA-Z_$][a-zA-Z\d_$]*\.)*[a-zA-Z_$][a-zA-Z\d_$]*/; + +module.exports = function (nsPackage) { + var match = nsPackage.match(regX); + var isValid = match ? match[0] == nsPackage : false; + if(isValid) { + nsPackage.split(".").every(function(val){ + if(reserved.indexOf(val) > -1) { + isValid = false; + } + return isValid; + }); + } + return isValid; +}; --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
